Use Custom font in title in toolbar(action bar) in Android

device-2016-08-21-191040
toolbar with custom font

When we use different font in our Android app, it is necessary to use the same font throughout the application. So you have to change the font in the toolbar(action bar).

For that you have to create TextView and set the TypeFace(font) of your choice to that TextView and set it to the action bar as custom view. Checkout the below code for the same.

TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText("Welcome!);
tv.setTextSize(20);
tv.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "Asap-Medium.otf");
tv.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(tv);

4 thoughts on “Use Custom font in title in toolbar(action bar) in Android

  1. Reda May 12, 2017 / 6:58 pm

    vielen dank freunde

  2. Shahu Ronghe May 9, 2019 / 5:59 am

    How to center the text in Action bar?

    • dakshbhatt May 9, 2019 / 6:17 pm

      Hello Shahu!
      You can use “addRule” to achieve this in above code. i.e. “lp.addRule(RelativeLayout.CENTER_HORIZONTAL);”
      Here you can use CENTER_HORIZONTAL or just CENTER

  3. dakshbhatt May 9, 2019 / 7:49 am

    Hello Shahu!
    You can use “addRule” to achieve this in above code. i.e. “lp.addRule(RelativeLayout.CENTER_HORIZONTAL);”
    Here you can use CENTER_HORIZONTAL or just CENTER

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s