How To Change Font Style Of Textview Programmatically In Android

How To Change Font Style Of Textview Programmatically

In android studio default Font Style is provided , but if we are making our app with different design then we have to change the Font Style.  So that Font Style can match up with our app design. And Application get nice user interface .  There for we going are going to learn in this article that how we can change the Font Style in android studio .

To change the Font Style from default to our custom style , we have to download custom Font Style . which can be find on Google easily . After downloading the Font style just follow some steps before getting into the code .

Adding Font Style in android studio


The steps for adding Font Style in android studio are : -

1 – Right Click on Main Folder and Create Directory Name Assets.

How To Change Textstyle Of Textview Programmatically

2 – Right click on Assets and create Directory by name it Fonts.

How To Change Textstyle Of Textview Programmatically

3 – Copy Your Downloaded Font Style and copy them into Font Folder.

Code Used to set Custom Font Style


<..
.
 Typeface mycustomfont1 = Typeface.createFromAsset(getAssets(),"fonts/Font_name.ttf");
        text2.setTypeface(mycustomfont1);
.
..>

We have used this code used with three different type of Font Style on three textview. which are as follows .

XML code for textview


<?xml version="1.0" encoding="utf-8"?>
<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="softwoodcoder.com.test.textstyle">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Softwoodcoder"
        android:textSize="30dp"
        android:id="@+id/text1"
        android:textColor="#000000"
        android:paddingLeft="25dp"
        android:paddingTop="40dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Softwoodcoder"
        android:textSize="30dp"
        android:id="@+id/text2"
        android:paddingLeft="25dp"
        android:textColor="#000000"
        android:paddingTop="130dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Softwoodcoder"
        android:textSize="30dp"
        android:id="@+id/text3"
        android:paddingLeft="25dp"
        android:textColor="#000000"
        android:paddingTop="220dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Softwoodcoder"
        android:textSize="30dp"
        android:id="@+id/text4"
        android:paddingLeft="25dp"
        android:textColor="#000000"
        android:paddingTop="310dp"/>
</RelativeLayout>

Java Code for Adding Font Style with textview


package softwoodcoder.com.test;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class textstyle extends AppCompatActivity {

    TextView text1, text2, text3,text4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textstyle);

        text1 = (TextView) findViewById(R.id.text1);    //Textview having default textstyle  

        text2 = (TextView) findViewById(R.id.text2);
        Typeface mycustomfont1 = Typeface.createFromAsset(getAssets(),"fonts/android_7.ttf");
        text2.setTypeface(mycustomfont1);
        //second textview with custom textstyle.
        
        text3 = (TextView) findViewById(R.id.text3);
        Typeface mycustomfont2 = Typeface.createFromAsset(getAssets(),"fonts/Fonty.ttf");
        text3.setTypeface(mycustomfont2);
        //third textview with custom textstyle.

        text4 = (TextView) findViewById(R.id.text4);
        Typeface mycustomfont3 = Typeface.createFromAsset(getAssets(),"fonts/Regular.ttf");
        text4.setTypeface(mycustomfont3);
        //fourth textview with custom textstyle.
    }
}

snapshot of  Output :-


How To Change Textstyle Of Textview Programmatically

for any difficulties in changing Font Style in android studio , feel free to contact us  . we will happy to help your .

This Post Has Been Viewed 127 Times

Ajay Kumar

Ajay kumar is young indian boy who enjoy coding and passionate to make , break & Develop New Codes .

Leave a Reply

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