Yin Zhu
Yin Zhu

Reputation: 17119

Change the text on a button in the program

The text on my buttons are set in resource files. E.g.

            <Button android:id="@+id/up_button" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="@string/up_label" />

The above code defines a button with text @string/up_label. How can I change this text in my program during the application is running.

Thanks.

Upvotes: 5

Views: 11590

Answers (3)

Zwiebel
Zwiebel

Reputation: 1615

Button button = (Button) findViewById(R.id.up_button);

For example:

button.setText("This is how to change text at runtime");

Upvotes: 1

Marco Schmid
Marco Schmid

Reputation: 174

Button myButton = (Button) findViewById(R.id.up_button);
myButton.setText("my text");

maybe this will help.

Upvotes: 7

alex.zherdev
alex.zherdev

Reputation: 24164

Or, if you have the new text in resources (which I think you do), you do

Button myButton = (Button) findViewById(R.id.up_button);
myButton.setText(R.string.down_label);

where down_label is the id of your string.

Upvotes: 6

Related Questions