How to use custom font in android toolbar title

In this android programming source code example, we are going to use custom font in android toolbar title.

Below is a step by step source code to use custom font in android toolbar title.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".toolbar.Toolbar2">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_top"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="@drawable/background_gradient"
        app:popupTheme="@style/AppTheme.PopupOverlay"  >

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.CoordinatorLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;


public class Toolbar2 extends AppCompatActivity {

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

        Toolbar toolbar = findViewById(R.id.toolbar_top);
        toolbar.setTitle("Custom Font");
        toolbar.setTitleTextAppearance(this, R.style.BitterTextAppearance);
        setSupportActionBar(toolbar);
    }
}

styles.xml

<style name="BitterTextAppearance">
        <item name="android:fontFamily">@font/font</item>
</style>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment