Set button text programmatically on activity run time through button click.
Button above text font style can be easily changeable via setTypeface(Typeface.FontName); function and with the use of this function developer can change button font family via application run time. For example if app user want to change their apps button font to be changed as his requirement. So here is the complete step by step tutorial for Change button text font style programmatically in android .
How to Change button text font style programmatically in android .
Code for MainActivity.java file.
package com.android_examples.com.buttonfontchangeproprogrammatically; 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 { Button b1,b2,b3,b4,b5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 =(Button)findViewById(R.id.button1); b2 =(Button)findViewById(R.id.button2); b3 =(Button)findViewById(R.id.button3); b4 =(Button)findViewById(R.id.button4); b5 =(Button)findViewById(R.id.button5); b5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { b1.setTypeface(Typeface.SANS_SERIF); b2.setTypeface(Typeface.MONOSPACE); b3.setTypeface(Typeface.SERIF); b4.setTypeface(Typeface.DEFAULT); } }); } }
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.buttonfontchangeproprogrammatically.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="114dp" android:text="SANS_SERIF button font" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button4" android:layout_centerHorizontal="true" android:text="Click Here to apply different fonts on buttons" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:text="MONOSPACE button font" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" android:text="SERIF Button font" /> <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:text="DEFAULT button font" /> </RelativeLayout>
Screenshot: