InflationX / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
1.09k stars 83 forks source link

Font not applied to Material Design NavigationView menu items #50

Open narun2001 opened 4 years ago

narun2001 commented 4 years ago

Hi, I have an issue similar to what is mentioned in issue #47 where the font I've set to use is not getting applied to the menu items in NavigationView. Just wondering if it's a known problem with Calligraphy as I couldn't see any response in that issue thread or the Exceptions section in the readme. Could you please help me with this?

Thanks, Arun

Stevemaster92 commented 3 years ago

As a workaround, try creating a custom style using fontFamily instead of fontPath and applying it directly to the NavigationView. For example,

styles.xml

<style name="TextAppearance.MenuItem" parent="android:TextAppearance">
    <item name="fontFamily">@font/[your-custom-font]</item>
</style>

activity_main.xml (layout file where your NavigationView resides)

...
<com.google.android.material.navigation.NavigationView
    android:id="@+id/side_nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:itemTextAppearance="@style/TextAppearance.MenuItem"
    app:menu="@menu/side_nav_main" />
...

Note: Do not forget to add your custom font file to the resource folder src/main/res/font in order to use @font!

chrisjenx commented 3 years ago

The NavigationView creates Views instead of inflating them (dumb - google has a terrible habit of this). If it supports custom view you can inflate, that would be the easiest thing, as if there is no inflation there is no hook.

What we have done in the past in very hacky ways for things like Toolbar is we see if the class is that toolbar then manually interact with it by traversing it's tree.

If you want to throw up a PR to check if the class we are inflating is the NavMenu to do something similar please do and I'll try and get it released. :)