Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android widget: How to change the text of a button

How can I change the text of an Android Button widget within code and not the XML file?

like image 698
Skatephone Avatar asked Oct 03 '10 20:10

Skatephone


People also ask

How to change text of a button in android?

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).

Which method can set or change text in a button?

setText() method can set or change the text in a Label - AWT and Swing.

How do I change the button style on my phone?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.


2 Answers

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.

like image 100
eldarerathis Avatar answered Sep 21 '22 02:09

eldarerathis


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"); 
like image 28
Skatephone Avatar answered Sep 22 '22 02:09

Skatephone