How can I change the text of an Android Button widget within code and not the XML file?
To set Android Button text, we can assign android:text XML attribute for Button in layout file with the required Text value. To programmatically set or change Android Button text, we can pass specified string to the method Button. setText(new_string_value).
setText() method can set or change the text in a Label - AWT and Swing.
To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.
You can use the setText()
method. Example:
import android.widget.Button; Button p1_button = (Button)findViewById(R.id.Player1); p1_button.setText("Some text");
Also, just as a point of reference, Button extends TextView, hence why you can use setText()
just like with an ordinary TextView.
I was able to change the button's text like this:
import android.widget.RemoteViews; //grab the layout, then set the text of the Button called R.id.Counter: RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout); remoteViews.setTextViewText(R.id.Counter, "Set button text here");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With