3

I am trying to change the font of the toolbar's title. Here is my toolbar layout:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="#666666"
    app:titleTextAppearance="@style/MyText"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

And in my styles.xml

<style name="MyText">
    <item name="fontPath">fonts/myFont.ttf</item>
</style>

If I do not set app:titleTextAppearance, the toolbar uses the system font. When I set it, the font gets smaller, but it is still in the system font. Am I doing something wrong?

Any suggestion, comments or answers much appreciated.

Edit:

I tried moving the style to styles-text.xml but no luck

Edit2:

For now, I am using a SpannableString and TypefaceSpan to make this work. Hope it helps someone.

1
  • It doesn't change the font
    – luksfarris
    Dec 19, 2014 at 13:13

2 Answers 2

5

This works:

<android.support.v7.widget.Toolbar 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"

   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   app:titleTextAppearance="@style/ActionBarTitleStyle"/>

Then have this in your styles.xml

<style name="ActionBarTitle" parent="android:Widget.TextView">

    <!-- The action bar title font -->
    <item name="android:fontFamily">sans-serif-light</item>

    <!-- Customizes the color of the action bar title-->
    <item name="android:textColor">#FF0000</item>

    <!-- Customizes the size of the action bar title-->
    <item name="android:textSize">24sp</item>

</style>
1

In my case, the font:family is only applied if I use "android:titleTextAppearance" instead of "app:titleTextAppearance" but this only works for api 21 or higher

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.