Change the text on the button in the program

The text on my buttons is set in the resource files. For instance.

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

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

Thank.

+3
source share
3 answers
Button myButton = (Button) findViewById(R.id.up_button);
myButton.setText("my text");

maybe this will help.

+8
source

Or, if you have 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_labelis the identifier of your string.

+4
source

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

:

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

Source: https://habr.com/ru/post/1771428/


All Articles