How to change fonts in Android

Custom Font's Example In Android

In this tutorial we will learn how to change the fonts with a simple example in Android Studio. 

In this example we will implement four types of fonts that we will show in the textview's.

Example:
  • Create a new project as Font.
  • Design four TextView's in main layout.
  • Create an assets folder under res folder, right click res -> New -> Folder -> and name it as fonts.
  • Download fonts from internet and store them in fonts folder.
File: activity_main.xml:
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.hss_24.font.MainActivity">

    <TextView
        android:id="@+id/txt_1"
        android:textSize="28sp"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textColor="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Android Lovers" />

    <TextView
        android:id="@+id/txt_2"
        android:textSize="28sp"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textColor="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Android Lovers" />

    <TextView
        android:id="@+id/txt_3"
        android:textSize="28sp"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Android Lovers" />

    <TextView
        android:id="@+id/txt_4"
        android:textSize="28sp"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Android Lovers" />

</LinearLayout>
  • Set the Typeface to the textview's in the MainActivity.java file.
File: MainActivity.java:
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.txt_1);
        TextView textView_2 = (TextView) findViewById(R.id.txt_2);
        TextView textView_3 = (TextView) findViewById(R.id.txt_3);
        TextView textView_4 = (TextView) findViewById(R.id.txt_4);

        //  set the typeface to the textview's
        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/CompletelyNonsense.ttf");
        textView.setTypeface(typeface);

        Typeface typeface_2 = Typeface.createFromAsset(getAssets(), "fonts/CornerDark.ttf");
        textView_2.setTypeface(typeface_2);

        Typeface typeface_3 = Typeface.createFromAsset(getAssets(), "fonts/LastFeast.otf");
        textView_3.setTypeface(typeface_3);

        Typeface typeface_4 = Typeface.createFromAsset(getAssets(), "fonts/masterofbreak.ttf");
        textView_4.setTypeface(typeface_4);
    }
}

  • Run the Application.
Output:



Download project here


Thank you!!!
Please like and share...

Comments