简体   繁体   中英

Edit G+ Sign in button text programmatically on Android

I'm developing android app with G+ as SSO (Single sign on) functionality following this docs https://developers.google.com/identity/sign-in/android/sign-in

In my case, I need to create sign in and sign out in single button. When the user signed in I need to change the button text into "Sign Out". But

SignInButton gPlusLoginButton = (SignInButton) findViewById(R.id.btGplus);
// login implementation
gPlusLoginButton.setText("Sign Out");

But SignInButton has no .setText method.

The following is a clever way of doing this that searches for the editable view within the object.

protected void setButtonText(SignInButton signInButton, String buttonText) {
    for (int i = 0; i < signInButton.getChildCount(); i++) {
        View v = signInButton.getChildAt(i);

        // if the view is instance of TextView then change the text SignInButton
        if (v instanceof TextView) {
            TextView tv = (TextView) v;
            tv.setText(buttonText);
            return;
        }
    }
}

So, to implement, just call:

SignInButton gPlusLoginButton = (SignInButton) findViewById(R.id.btGplus);
// login implementation
setButtonText(gPlusLoginButton, "Sign Out");

Hat tip to http://www.pcsalt.com/android/change-text-of-google-plus-signinbutton-android/

Try This code:

TextView textView = (TextView) signInButton.getChildAt(0);
textView.setText("Your Text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM