Friday, January 9, 2015

How to set Button text style to bold and italic programmatically in Android

MainActivity.java code snippet

//require to import View class
public void perform_action(View v)
{
 //require to import Button class
 Button btn1 = (Button) findViewById(R.id.push_button);
 //require to import Typeface class
 //set the button font, text style bold
 btn1.setTypeface(btn1.getTypeface(), Typeface.BOLD);
 btn1.setText("Button Text Style Bold");

 Button btn2 = (Button) findViewById(R.id.push_button2);
 //set the button font, text style bold
 btn2.setTypeface(btn2.getTypeface(), Typeface.ITALIC);
 btn2.setText("Button Text Style Italic");

 Button btn3 = (Button) findViewById(R.id.push_button3);
 //set the button font, text style bold and italic
 btn3.setTypeface(btn3.getTypeface(), Typeface.BOLD_ITALIC);
 btn3.setText("Button Text Style Bold Italic");
}
Imported Class

import android.view.View;
import android.widget.Button;
import android.graphics.Typeface;
More android examples