How to change Action Bar/Title Bar font?

10,193

Solution 1

As some other users have suggested, you can use a toolbar, but if you don't want to or you have to keep using the action bar, then you can define the style of the action bar in your style like this:

<style name="baseStyle" parent="@style/Theme.MaterialComponents.Light.Bridge">
    <item name="actionBarStyle">@style/myActionBarStyle</item>
  ...
</style>

and inside myActionBarStyle define the font:

  <style name="myActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="fontFamily">@font/customFont</item>
    ...
  </style>

this will affect the font only in the action bar and not in the whole app

Solution 2

You can make a custom toolbar. below is code snapshot

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="17sp"
        android:textStyle="bold" />
</android.support.v7.widget.Toolbar>

and then you can access toolbar_title in your java class(Activity or Fragment) and set a custom font style.

Share:
10,193

Related videos on Youtube

nicoqueijo
Author by

nicoqueijo

Updated on June 04, 2022

Comments

  • nicoqueijo
    nicoqueijo almost 2 years

    Not sure if this is called the action bar or the title bar but I need to change the font to a font I have in my assets folder. How do I do this?

    enter image description here

  • nicoqueijo
    nicoqueijo about 7 years
    How do I access the toolbar variable? Doesn't exist in my activity when I start typing it.
  • John
    John about 7 years
    Do you use toolbar or action bar
  • nicoqueijo
    nicoqueijo about 7 years
    Not sure. It is the default when you create an empty activity. I'm not referencing it in my xml or java though.
  • nicoqueijo
    nicoqueijo about 7 years
    This adds another bar under the existing bar. I'm trying to edit the existing bar which comes as default when you create the project.
  • nicoqueijo
    nicoqueijo about 7 years
    When I apply the code you gave me titleview_2 shows up as red (Cannot resolve symbol 'titleview_2')
  • John
    John about 7 years
    Or try Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  • Braj Bhushan Singh
    Braj Bhushan Singh about 7 years
    Give your layout name as titleview_2.
  • Prasath
    Prasath about 4 years
    The Question is about Custom fonts but you wrote about app bat title.