How to change the font of the box for navigation?

I want to use my own font for a box for navigation in android.I use the library comes with Android studio according to this answer: https://stackoverflow.com/a/416829/ But I do not know how to change the font and make it RTL. I searched a lot and I found how to make an RTL box. I am using this code:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 

and Android - is there a navigation box on the right side?

But, as you know, this only works in API 17 and above. Please help! How to change the font of the menu? How to configure RTL format ?!

Edited : My font file "TTF" is in the properties / fonts and I know how to set it for textview using java, but I don't know how to set it in the navigation box menu.

+7
android fonts right-to-left navigation-drawer
source share
4 answers

I found the answer: First create this class in your project:

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

Then add this method to your activity to change the font of the navigation box menu:

 private void applyFontToMenuItem(MenuItem mi) { Typeface font = Typeface.createFromAsset(getAssets(), "ds_digi_b.TTF"); SpannableString mNewTitle = new SpannableString(mi.getTitle()); mNewTitle.setSpan(new CustomTypefaceSpan("" , font), 0 , mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); mi.setTitle(mNewTitle); } 

and then add the method call that you just added to your activity:

 navView = (NavigationView) findViewById(R.id.navView); Menu m = navView.getMenu(); for (int i=0;i<m.size();i++) { MenuItem mi = m.getItem(i); //for aapplying a font to subMenu ... SubMenu subMenu = mi.getSubMenu(); if (subMenu!=null && subMenu.size() >0 ) { for (int j=0; j <subMenu.size();j++) { MenuItem subMenuItem = subMenu.getItem(j); applyFontToMenuItem(subMenuItem); } } //the method we have create in activity applyFontToMenuItem(mi); } 
+26
source share

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.

+4
source share

Thanks! I successfully changed the font in the navigation box based on @Amir H ​​post, but with the configuration (just add a few lines to your activity)

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); Menu m = navigationView .getMenu(); for (int i=0;i<m.size();i++) { MenuItem mi = m.getItem(i); //for applying a font to subMenu ... SubMenu subMenu = mi.getSubMenu(); if (subMenu!=null && subMenu.size() >0 ) { for (int j=0; j <subMenu.size();j++) { MenuItem subMenuItem = subMenu.getItem(j); SpannableString s = new SpannableString(subMenuItem.getTitle()); s.setSpan(new TypefaceSpan("fonts/yourfontname.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); subMenuItem.setTitle(s); } } } 

Maybe this will help someone :)

+2
source share

Only works for fonts.

  • First of all, add the color of your font (if you want to change) in the colors.xml file located in res->values->colors.xml , for example

     <color name="black">#000000</color> // it for black don't go for white color 
  • Then edit the style.xml file located in the same directory of values ​​(there are two files that edit this file that has your theme with the style name="your_theme" , or find the line in these two files

  • Here we must set the font property. Therefore, you must create a new style tag in the resource tag application. in my case i create

     <style name="MyText" parent="@android:style/TextAppearance.Medium"> <item name="android:textSize">20sp</item> //size of font <item name="android:textColor">@color/black</item> //color of font <item name="android:typeface">sans</item> // type how it appear </style> 

    Note that the Name for this tag is MyText . Now we must use this name in the first block of the style, whose name is your theme of the application.

  • Mentioned this new style in the appication tag style tag. In my case, it looks like

     <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:textViewStyle">@style/MyText</item> //MyText its custom style for font </style> 
+1
source share

All Articles