Set Button Text style to Bold Italic in android programmatically

Apply different types of font styles on button widget in android.

There are by default three major types of font styles available for android button widget. Bold, Italic, ItalicBold . These styles can be also applied through activity_main.xml layout file but sometimes user buyer required different process so application developer can change button name text string dynamically using setTypeface() function. So here is the complete step by step tutorial for Set Button Text style to Bold Italic in android programmatically. On this project changing the button text style on another button click event.

android-project-download-code-button

Set Button Text style to Bold Italic in android programmatically.

Code for MainActivity.java file.

 package com.android_examples.com.buttontextbolditalic;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

 // Defining buttons name( You can make any button name ).
 Button SetFont,BOLD,ITALIC,BoldItalic;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 BOLD = (Button)findViewById(R.id.button1);
 ITALIC = (Button)findViewById(R.id.button2);
 BoldItalic = (Button)findViewById(R.id.button3);
 SetFont = (Button)findViewById(R.id.button4);
 
 SetFont.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 // SETTING UP FIRST BUTTON STYLE TO BOLD.
 BOLD.setTypeface(BOLD.getTypeface(),Typeface.BOLD);
 
 // SETTING UP SECONT BUTTON STYLE TO ITALIC.
 ITALIC.setTypeface(BOLD.getTypeface(),Typeface.ITALIC);
 
 // SETTING UP THIRD BUTTON STYLE TO BOLD-ITALIC.
 BoldItalic.setTypeface(BOLD.getTypeface(),Typeface.BOLD_ITALIC);
 
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.buttontextbolditalic.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="94dp"
 android:text="ANDROID-EXAMPLES.COM" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/button1"
 android:layout_below="@+id/button1"
 android:layout_marginTop="14dp"
 android:text="ANDROID-EXAMPLES.COM" />

 <Button
 android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/button2"
 android:layout_below="@+id/button2"
 android:layout_marginTop="18dp"
 android:text="ANDROID-EXAMPLES.COM" />

 <Button
 android:id="@+id/button4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button3"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="15dp"
 android:text="CLICK HERE FOR APPLY BOLD ITALIC FONT STYLES ON ABOVE BUTTONS." />

</RelativeLayout>

Screenshot :

Set Button Text style to Bold Italic in android programmatically

set-button-text

Click Here To Download Set Button Text style to Bold Italic in android programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *