chrisjenx / Calligraphy

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

Calligraphy does not work with NavigationView #258

Open ghatasheh opened 8 years ago

ghatasheh commented 8 years ago

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

chrisjenx commented 8 years ago

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.

ajdevries commented 8 years ago

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.

@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

chrisjenx commented 8 years ago

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

On Tue, 29 Mar 2016, 19:06 Albert-Jan de Vries, notifications@github.com 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.

@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

ajdevries commented 8 years ago

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.

chrisjenx commented 8 years ago

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

Thanks

On Tue, 29 Mar 2016, 19:33 Albert-Jan de Vries, notifications@github.com 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

ajdevries commented 8 years ago

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

scottyab commented 8 years ago

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.

ajdevries commented 8 years ago

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

scottyab commented 8 years ago

@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));
    }
ajdevries commented 8 years ago

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.

ghatasheh commented 8 years ago

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

ghatasheh commented 8 years ago

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);
        }
    }
}
chrisjenx commented 8 years ago

You know there is already a typeface span in Calligraphy?

On Wed, 30 Mar 2016, 22:50 Hisham Ghatasheh, notifications@github.com 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

ghost commented 8 years ago

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 commented 8 years ago

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

taylors1512 commented 8 years ago

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); }

jzeferino commented 8 years ago

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?

chrisjenx commented 8 years ago

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

On Mon, 17 Oct 2016, 08:28 jzeferino, notifications@github.com 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 .

jzeferino commented 8 years ago

@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 commented 8 years ago

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

marlonlom commented 6 years ago

@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