Calligraphy icon indicating copy to clipboard operation
Calligraphy copied to clipboard

Calligraphy does not work with NavigationView

Open ghatasheh opened this issue 9 years ago • 21 comments

I tried to change it using Calligraphy configs but didn't work CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf") .setFontAttrId(R.attr.fontPath) .build());

Also, I tried to get the menu from the NavigationView navigationView.getMenu() and use CalligraphyTypefaceSpan to set the title with no luck!

Is there anyway to achieve that?

Thanks in advance

ghatasheh avatar Mar 09 '16 15:03 ghatasheh

I think the NavigationView isn't a textview. and doesn't inflate it's children.

Need to nudge Goog to make there support lib stuff use the LayoutInflater.

chrisjenx avatar Mar 11 '16 09:03 chrisjenx

I like this library a lot. And it works great. But can't get the right fonts in the NavigationView either.

I made a small app, to show that it isn't working. Items in a NavigationView are inflated and CalligraphyContextWrapper is used, but somehow the font isn't changed.

  • https://github.com/ajdevries/android-navigation

@chrisjenx any clue what is happening here?

I'm getting the fontPath value res/drawable/list_selected_background.xml in https://github.com/chrisjenx/Calligraphy/blob/master/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java#L192

When the NavigationMenuPresenter is inflating his Views

ajdevries avatar Mar 29 '16 18:03 ajdevries

Guessing there could be a resource conflict, never seen this before. :/

On Tue, 29 Mar 2016, 19:06 Albert-Jan de Vries, [email protected] wrote:

I like this library a lot. And it works great. But can't get the right fonts in the NavigationView either.

I made a small app, to show that it isn't working. Items in a NavigationView are inflated and CalligraphyContextWrapper is used, but somehow the font isn't changed.

  • https://github.com/ajdevries/android-navigation

@chrisjenx https://github.com/chrisjenx any clue what is happening here?

I'm getting the fontPath value res/drawable/list_selected_background.xml in https://github.com/chrisjenx/Calligraphy/blob/master/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java#L192

When the NavigationMenuPresenter is inflating his Views

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/258#issuecomment-203030491

chrisjenx avatar Mar 29 '16 18:03 chrisjenx

It would be great if you have a solution for this... Need this one for the project I'm working on. If I can help please let me know. A work around is also an option.

Debugging it many times today with my larger Android project, and isolated it to this small project.

ajdevries avatar Mar 29 '16 18:03 ajdevries

OK thanks for isolating, that helps. Have you tried the snapshot?

Thanks

On Tue, 29 Mar 2016, 19:33 Albert-Jan de Vries, [email protected] wrote:

It would be great if you have a solution for this... Need this one for the project I'm working on. If I can help please let me know. A work around is also an option.

Debugging it many times today with my larger Android project, and isolated it to this small project.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/258#issuecomment-203040660

chrisjenx avatar Mar 29 '16 19:03 chrisjenx

Yes. You can see it in the build file. https://github.com/ajdevries/android-navigation/blob/master/app/build.gradle#L26

ajdevries avatar Mar 29 '16 19:03 ajdevries

I have custom font with calligraphy working a-ok with <android.support.design.widget.NavigationView /> I wonder if there's an issue with app:itemTextAppearance="@style/AppTheme.NavigationView"? I am not using that, instead app:itemTextColor="@color/textColorBright". I've found with other android.support.design views the TextAppearance screws things up.

scottyab avatar Mar 30 '16 13:03 scottyab

Okay... And can you tell me how you changed the type face in the NavigationView?

ajdevries avatar Mar 30 '16 13:03 ajdevries

@ajdevries Sorry i wasn't clear. By using the default config.

init CalligraphyConfig in Application object

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath(getString(R.string.font_path_regular))
                .setFontAttrId(R.attr.fontPath)
                .build());

And attaching base context in activity

 @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

scottyab avatar Mar 30 '16 15:03 scottyab

Hmm, yeah. Maybe that is an solution, but for my current project difficult, because I'm using different fonts. When there is time I'll have a look what is happening here.

ajdevries avatar Mar 30 '16 15:03 ajdevries

Hi @chrisjenx its CheckedTextView, and inflated from xml file. There is no workarounds in the code.

ghatasheh avatar Mar 30 '16 21:03 ghatasheh

This is my workaround for now;

public class TypefaceSpan extends MetricAffectingSpan {
  private final Typeface typeface;

  public TypefaceSpan(Typeface typeface) {
    this.typeface = typeface;
  }

  private static void apply(Paint paint, Typeface typeface) {
    paint.setTypeface(typeface);
  }

  @Override
  public void updateDrawState(TextPaint textPaint) {
    apply(textPaint, typeface);
  }

  @Override
  public void updateMeasureState(TextPaint textPaint) {
    apply(textPaint, typeface);
  }
}

private void changeFont() {
    Menu menu = navigationView.getMenu();
    Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/customFont.ttf");

    for (int i = 0; i < menu.size(); i++) {
        MenuItem menuItem = menu.getItem(i);

            if (menuItem != null) {
            SpannableString spannableString = new SpannableString(menuItem.getTitle());
            spannableString.setSpan(new TypefaceSpan(typeface), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            menuItem.setTitle(spannableString);
        }
    }
}

ghatasheh avatar Mar 30 '16 21:03 ghatasheh

You know there is already a typeface span in Calligraphy?

On Wed, 30 Mar 2016, 22:50 Hisham Ghatasheh, [email protected] wrote:

This is my workaround for now;

public class TypefaceSpan extends MetricAffectingSpan { private final Typeface typeface;

public TypefaceSpan(Typeface typeface) { this.typeface = typeface; }

private static void apply(Paint paint, Typeface typeface) { paint.setTypeface(typeface); }

@Override public void updateDrawState(TextPaint textPaint) { apply(textPaint, typeface); }

@Override public void updateMeasureState(TextPaint textPaint) { apply(textPaint, typeface); } } private void changeFont() { Menu menu = navigationView.getMenu(); Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/customFont.ttf");

for (int i = 0; i < menu.size(); i++) {
    MenuItem menuItem = menu.getItem(i);

        if (menuItem != null) {
        SpannableString spannableString = new SpannableString(menuItem.getTitle());
        spannableString.setSpan(new TypefaceSpan(typeface), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        menuItem.setTitle(spannableString);
    }
}

}

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/258#issuecomment-203650352

chrisjenx avatar Mar 30 '16 23:03 chrisjenx

Extending the NavigationView and wrapping it's context on the constructor, I've managed to change the font only on the menu itens that DON'T have a image.

ss_nav On RED, the wrong font, on GREEN, the right one

here's the menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_account"
        android:icon="@drawable/ic_navigationdrawer_account"
        android:title="Account" />
    <item android:title="HARDWARE">
        <menu>
            <item
                android:id="@+id/nav_connect"
                android:icon="@drawable/ic_navigationdrawer_connect_hardware"
                android:title="Connect Hardware" />
            <item
                android:id="@+id/nav_calibrate"
                android:icon="@drawable/ic_navigationdrawer_calibrate_beta_monitor"
                android:title="Calibrate Beta Monitor" />
        </menu>
    </item>
</menu>

ghost avatar Apr 04 '16 12:04 ghost

Manage to get it working with the changeFont method posted by @ghatasheh, but I've used the CalligraphyTypefaceSpan instead of the custom TypefaceSpan. But it's a workarround

ghost avatar Apr 04 '16 12:04 ghost

Thanks for this; it has been taxing me all day. I used the Calligraphy Typeface Span also. I iterate through the drawer menu items and send them to this method below. (Sorry, haven't worked out how to indent code in these forums yet)

private void applyFontToMenuItem(MenuItem mi) { Typeface font = Typeface.createFromAsset(getAssets(), "fonts/TeXGyreAdventor-Bold.ttf"); SpannableString fontableTitle = new SpannableString(mi.getTitle()); fontableTitle.setSpan(new CalligraphyTypefaceSpan(font), 0, fontableTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); mi.setTitle(fontableTitle); }

taylors1512 avatar Aug 17 '16 16:08 taylors1512

as said bay @scottyab after use the app:itemTextAppearance="@style/AppTheme.NavigationView" the font went do default android value. I tried the SpannableString but didn't work. Any workaround?

jzeferino avatar Oct 17 '16 14:10 jzeferino

Scotty also put a work around on here. Does that not work?

On Mon, 17 Oct 2016, 08:28 jzeferino, [email protected] wrote:

as said bay @scottyab https://github.com/scottyab after use the app:itemTextAppearance="@style/AppTheme.NavigationView" the font went do default android value. I tried the SpannableString but didn't work. Any workaround?

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

chrisjenx avatar Oct 17 '16 14:10 chrisjenx

@chrisjenx what @scottyab said is that when using the app:itemTextAppearance="@style/AppTheme.NavigationView" the font DON'T use the calligraphy font. It uses the default. In my case I need to use the app:itemTextAppearance.

jzeferino avatar Oct 17 '16 14:10 jzeferino

I ended-up hacking the drawer menu to force the font when the DrawerStateChanged: See the source code here:

https://gist.github.com/jzeferino/797c5e6b72c666b6f87969f6146a2a6e

jzeferino avatar Nov 05 '16 13:11 jzeferino

@jzeferino Great tip! But, i realized that when the activity is resumed or when configuration changes, it uses the default font. In that case, it work when close and reopen the navigationview (eg. when the app is restarted or something related to configuration change while nav-view its opened).

Finally, i ended up using this approach:

(1) in strings.xml:

    <style name="AppTheme.NavigationView">
        <item name="android:textSize">16sp</item>
        <item name="fontPath">fonts/Custom-font.ttf</item>
    </style>

(2) in activity_home.xml:

<android.support.design.widget.NavigationView
        android:id="@+id/home_navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:theme="@style/AppTheme.NavigationView"
        app:menu="@menu/menu_drawer" />

Taken from here

marlonlom avatar Jan 28 '18 05:01 marlonlom