How to change the text of a Button widget in Android?
How can I change the text of an Android Button widget within code and not t开发者_开发知识库he XML file?
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");
This is very easy
Button btn = (Button) findViewById(R.id.btn);
btn.setText("MyText");
I had a button in my layout.xml that was defined as a View as in:
final View myButton = findViewById(R.id.button1);
I was not able to change the text on it until I also defined it as a button:
final View vButton = findViewById(R.id.button1);
final Button bButton = (Button) findViewById(R.id.button1);
When I needed to change the text, I used the bButton.setText("Some Text");
and when I wanted to alter the view, I used the vButton.
Worked great!
use the exchange using java. setText = "...", for class java there are many more methods for implementation.
//button fechar
btnclose.setEnabled(false);
btnclose.setText("FECHADO");
View.OnClickListener close = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (btnclose.isClickable()) {
btnOpen.setEnabled(true);
btnOpen.setText("ABRIR");
btnclose.setEnabled(false);
btnclose.setText("FECHADO");
} else {
btnOpen.setEnabled(false);
btnOpen.setText("ABERTO");
btnclose.setEnabled(true);
btnclose.setText("FECHAR");
}
Toast.makeText(getActivity(), "FECHADO", Toast.LENGTH_SHORT).show();
}
};
btnclose.setOnClickListener(close);
This may be off topic, but for those who are struggling on how to exactly change also the font of the button text (that was my case and Skatephone's answer helped me) here's how I did it (if you made buttons ind design mode):
First we need to have the button's string name "converted" (it's a foul way to explain, but straightforward) into java from the xml, and so we paste the aforementioned code into our MainActivity.java
IMPORTANT! place the code under the OnCreate method!
import android.widget.RemoteViews;
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setTextViewText(R.id.Counter, "Set button text here");
Keep in mind:
my_layout
has to be substituted with the xml file where your buttons are
Counter
has to be substituted with the id name of your button ("@+id/ButtonName"
)
if you want to change the button text just insert the text in place of "Set button text here"
here comes the part where you change the font:
Now that you "converted" from xml to java, you can set a Typeface method for TextView. Paste the following code exactly under the previous one just described above
TextView txt = (TextView) findViewById(R.id.text_your_text_view_id);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/MyFontName.ttf");
txt.setTypeface(font);
where in place of text_your_text_view_id
you put your button's id name (like as previous code) and in place of MyFontName.ttf
you put your desired font
WARNING! This assumes you already put your desired font into the assets/font folder. e.g. assets/fonts/MyFontName.ttf
//text button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" text button" />
// color text button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text button"
android:textColor="@android:color/color text"/>
// background button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text button"
android:textColor="@android:color/white"
android:background="@android:color/ background button"/>
// text size button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text button"
android:textColor="@android:color/white"
android:background="@android:color/black"
android:textSize="text size"/>
精彩评论