Calligraphy: AppCompat toolbar title font is not set

Hi! I am trying to style toolbar font, but nothing happens. I am able to set font color and style, but not the font itself. I am using the latest calligraphy (2.2.0), appcompat (23.4.0), Android 6.0.

<android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        app:title="@string/app_name"
        app:titleTextAppearance="@style/DancingWhite"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

...

<style name="DancingWhite" parent="android:TextAppearance">
        <item name="android:textSize">22sp</item>
        <item name="android:textColor">@color/colorTitle</item>
        <item name="android:textStyle">italic</item>
        <item name="fontPath">fonts/DancingScript-Regular.otf</item>
    </style>

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 23 (2 by maintainers)

Commits related to this issue

Most upvoted comments

The problem is that Toolbar creates TextViews for title and subtitle programmatically inside itself. It means that it doesn’t use wrapped LayoutInflater by Calligraphy. Instead it uses system Typefaces depending on fontFamily and fontStyle from textAppearence attribute.

But Calligraphy listens for GlobalLayout changes and tries to load style from theme.

So what I’ve done: Add activity theme and customize ActionBarStyle:

<style name="ActivityTheme" parent="AppTheme.Base">
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>

<style name="ActionBarStyle" parent="Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:titleTextStyle">@style/ToolbarTitleTextAppearance</item>
</style>

<style name="ToolbarTitleTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textSize">20sp</item>
        <item name="fontPath">fonts/Roboto-Medium.ttf</item>
</style>

+1 here: using calligraphy:2.2.0, appcompat-v7:25.1.1

Everything is set on the toolbar title from the style (color, size) except the font. And I added the attachBaseContext override in the activity.

    <style name="ToolbarTitleTextAppearance" parent="android:TextAppearance">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">22sp</item>
        <item name="fontPath">fonts/Roboto-Light.ttf</item>
    </style>

        <android.support.v7.widget.Toolbar
            android:id="@+id/activity_listview_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            app:titleTextAppearance="@style/ToolbarTitleTextAppearance"/>

Nothing worked for me so this is the function that I wrote to workaround the issue:

    private void setToolBarFont() {
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        for (int i = 0; i < toolbar.getChildCount(); i++) {
            final View child = toolbar.getChildAt(i);
            if (child instanceof TextView) {
                final TextView textView = (TextView) child;
                CalligraphyUtils.applyFontToTextView(textView, TypefaceUtils.load(getAssets(), "fonts/OpenSans-Semibold.ttf"));
            }
        }
    }

You can use android:titleTextAppearance you only need to compile above API 20 to use it.

On Tue, 28 Jun 2016 at 10:56 Kevin van Mierlo notifications@github.com wrote:

@chrisjenx https://github.com/chrisjenx I take it app:titleTextAppearance can’t be supported? I can’t use android:titleTextAppearance until I only want to support lollipop or higher

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/295#issuecomment-229005141, or mute the thread https://github.com/notifications/unsubscribe/ABHRsZjRfrAeMHSmoAXqrRRxfCI3e57jks5qQO_ggaJpZM4ItBgi .

some working sample code would be appreciated, tried to apply the above mentioned method but does not work

We don’t use the app:titleTextApperance but the android:titleTextApperance