How to change edittext font style in android programmatically

How to set EditText entered / inside text font to different font style in android dynamically.

In this tutorial we are creating 4 editText inside our activity_main.xml layout file then programmatically change their demand text font style using setTypeface() method. So here is the complete step by step tutorial for How to change edittext font style in android programmatically.

android-project-download-code-button

How to change edittext font style in android programmatically.

Code for MainActivity.java file.

 package com.edittextfontchange_android_examples.com;

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

public class MainActivity extends Activity {

 EditText one,two,three,four;
 Button click;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 one = (EditText)findViewById(R.id.editText1);
 two = (EditText)findViewById(R.id.editText2);
 three = (EditText)findViewById(R.id.editText3);
 four = (EditText)findViewById(R.id.editText4);
 click = (Button)findViewById(R.id.button1);
 
 click.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //setting up font styles.
 two.setTypeface(Typeface.MONOSPACE);
 three.setTypeface(Typeface.SANS_SERIF);
 four.setTypeface(Typeface.SERIF);
 
 //Setting up text.
 one.setText("Default font");
 two.setText("MONOSPACE FONT");
 three.setText("SANS SERIF FONT");
 four.setText("SERIF FONT");
 
 }
 });
 
 
 
 
 }
}

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.edittextfontchange_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="166dp"
 android:ems="10"
 android:hint="EditText normal font" >

 <requestFocus />
 </EditText>

 <EditText
 android:id="@+id/editText2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="15dp"
 android:ems="10"
 android:hint="MONOSPACE font." />

 <EditText
 android:id="@+id/editText3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:ems="10"
 android:hint="SANS_SERIF font" />
 
 <EditText
 android:id="@+id/editText4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText3"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:ems="10"
 android:hint="SERIF font" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:text="Click here to apply font and set text for example" />

</RelativeLayout>

Screenshot:

How to change edittext font style in android programmatically

Click here to download How to change edittext font style in android programmatically project.

Leave a Reply

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