[Android] Changing Button Text in Android

Code has been updated on 19 Aug 2008 to work with 0.9 beta

Lets say you have an Android (if you don’t know what Android is, have a look at the Android website) view with two buttons, like this view:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     android:id="@+id/backgroundView">
 7    <Button android:id="@+id/testButton"
 8         android:text="@string/buttonText"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content" />
11    <Button android:id="@+id/fakeButton"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:text="Fake" />
15 </LinearLayout>

Like in Swing, to capture focus and clicking events on the first button, you first create a listener that implements the Button.OnClickListener and Button.OnFocusChangeListener. The button’s text can be changed by calling the setText() method on the button. For example, to change the text when the focus is changed and when it is clicked:

 1  private class ButtonAction implements 
 2      Button.OnClickListener,
 3      Button.OnFocusChangeListener
 4  {
 5      @Override
 6      public void onClick(View arg0) 
 7      {
 8          counter++;
 9          testButton.setText("Times clicked: " 
10                  + Integer.toString(counter));
11      }
13      @Override
14      public void onFocusChange(View arg0,
15              boolean isFocused) 
16      {
17          if (isFocused == true)
18          {
19              testButton.setText("Now click!");
20          }
21          else
22          {
23              testButton.setText("Out of focus!");
24          }
25      }
26  }

Of course, you have to also add the listener to button! This is also fairly easy to do in Android:

 1    /** Called when the activity is first created. */
 2    @Override
 3    public void onCreate(Bundle icicle) 
 4    {
 5      super.onCreate(icicle);
 6      setContentView(R.layout.main);
 8      ButtonAction testListener = new ButtonAction();
 9        testButton = (Button) findViewById(R.id.testButton);
10        testButton.setOnClickListener(testListener);
11        testButton.setOnFocusChangeListener(testListener);
12        findViewById(R.id.backgroundView);
13    }


8 Responses to [Android] Changing Button Text in Android

  1. Pingback: Display Progress Bar After A Dialog In Android « Kah - The Developer

  2. Chandan says:

    Dear Kahgoh,

    I would like to set new text to the button subsequently during run time. Please help me to fix this.

    For ex: In a quiz application, i like to set options to the option button for every new question. I thought Buttons would be more appropriate to use.

    /*However i can do this with TextView without any issues.*/

    Advance thanks for any sort of effort in this regard.

  3. dixit says:

    thanx for this code…………..

  4. Zack says:

    I have a quick question. For some reason my app won’t work when I try to change a buttons text. Can you only change text if you click the button, or in onCreate() can you start off by changing a buttons text?

    • kahgoh says:

      I have no idea what your code looks like, but you should aim to change the text on the UI thread. Have you checked if you are changing the text on the UI thread? If you’re trying to change text from another thread, use Activity.runOnUiThread or post method on the button. See Process and threads in the Android dev guide for more details.

  5. Pingback: android buttonPhone News | Phone News

  6. Hi there, this weekend is nice for me, as this occasion i am reading this wonderful informative piece of writing here
    at my home.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: