Set custom external font style on ActionBar inside title text in Android

How to create action bar title text with use of assets folder outsource font style.

There are hundreds of custom font styles freely available on internet and each font styles represents a category in which that particular font can be used. Setting up custom font is very easy on title text. So here is the complete step by step tutorial for Set custom external font style on ActionBar inside title text in Android.

android-project-download-code-button

 

Note: If you are facing any type of NullPointerException error then ready my this post to solve this error.

How to Set custom external font style on ActionBar inside title text in Android.

1. Download any font from internet which you want to use as Action bar title text style.

2. Now Create a new folder name ExternalFonts in assets folder and Paste all the downloaded fonts inside it.

load fonts in app
Code for MainActivity.java file.

 package com.customexternalfontstyleactionbar_android_examples.com;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

 String ExternalFontPath;
 Typeface FontLoaderTypeface;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ActionBar actionbar = getActionBar();

 TextView TextViewNewFont = new TextView(MainActivity.this);

 LayoutParams layoutparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
 
 TextViewNewFont.setLayoutParams(layoutparams);

 TextViewNewFont.setText("ActionBar Title");

 TextViewNewFont.setTextColor(Color.RED);
 
 TextViewNewFont.setGravity(Gravity.CENTER);

 TextViewNewFont.setTextSize(27);
 
 // Assests folder font folder path
 ExternalFontPath = "ExternalFonts/chopinscript.ttf";

 // Load Typeface font url String ExternalFontPath
 FontLoaderTypeface = Typeface.createFromAsset(getAssets(), ExternalFontPath);
 
 TextViewNewFont.setTypeface(FontLoaderTypeface);

 actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

 actionbar.setCustomView(TextViewNewFont);
 
 }
}

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


</RelativeLayout>

Screenshot:

Set custom external font style on ActionBar inside title text in Android

Click here to download Set custom external font style on ActionBar inside title text in Android project with source code.

Leave a Reply

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