Adding to the answer risan.
I edited "mi" since these are my names in the drawer menu. Then I changed the s.setSpan 1st parameter to use the custom class CustomTypefaceSpan.
// Navigation View NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); Menu m = navigationView .getMenu(); Typeface tf1 = Typeface.createFromAsset(getAssets(), "font/Gotham Narrow Extra Light.otf"); for (int i=0;i<m.size();i++) { MenuItem mi = m.getItem(i); SpannableString s = new SpannableString(mi.getTitle()); s.setSpan(new CustomTypefaceSpan("", tf1), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mi.setTitle(s); }
CustomTypefaceSpan Class:
package my.app; import android.graphics.Paint; import android.graphics.Typeface; import android.text.TextPaint; import android.text.style.TypefaceSpan; public class CustomTypefaceSpan extends TypefaceSpan { private final Typeface newType; public CustomTypefaceSpan(String family, Typeface type) { super(family); newType = type; } @Override public void updateDrawState(TextPaint ds) { applyCustomTypeFace(ds, newType); } @Override public void updateMeasureState(TextPaint paint) { applyCustomTypeFace(paint, newType); } private static void applyCustomTypeFace(Paint paint, Typeface tf) { int oldStyle; Typeface old = paint.getTypeface(); if (old == null) { oldStyle = 0; } else { oldStyle = old.getStyle(); } int fake = oldStyle & ~tf.getStyle(); if ((fake & Typeface.BOLD) != 0) { paint.setFakeBoldText(true); } if ((fake & Typeface.ITALIC) != 0) { paint.setTextSkewX(-0.25f); } paint.setTypeface(tf); } }
I canβt believe how difficult it is to simply change the fonts for the menu.
williamsi
source share