Android widget: How to change the text of a button


Question

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

1
31
9/15/2013 6:26:19 PM

Accepted Answer

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");
20
9/15/2013 6:46:36 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon