How to Change The Navigationview's Item Text Size

How can I change the NavigationView's item text size?

Since Android Support Library 22.2.1, Google has changed default textSize of items in NavigationView from 16sp to 14sp, which suits Material Design guideline well. However, in some cases(for example, when you want to support Chinese language), it seems larger textSize is better. Solution is simple:

  1. add app:theme="@style/yourStyle.Drawer" to your NavigationView in your layout.xml
  2. in styles.xml, add android:textSize="16sp" in style yourStyle.Drawer

How can change textsize of menu item?

Instead of setting the theme for every element, set the theme to your Navigationview. Like,

<style name="NavDrawerTextStyle">
<item name="android:textSize">50sp</item>
</style>

to Navigationview

<android.support.design.widget.NavigationView
...
android:theme="@style/NavDrawerTextStyle"
/>

Changing the item text size in navigation view

Create a style and apply it to NavigationView using app:theme

<style name="NavigationViewStyle">
<item name="android:textSize">20sp</item> <!-- menu item text size-->
<item name="android:listPreferredItemHeightSmall">40dp</item><!-- menu item height-->
</style>

And then as said, apply this style to NavigationView using app:theme

<android.support.design.widget.NavigationView
...
...
app:theme="@style/NavigationViewStyle"
...
...

</android.support.design.widget.NavigationView>

How to change the font and the size of a NavigationView menu item?

You can use a SpannableString:

val item: MenuItem = nav.menu.findItem(R.id.nav_user)
val spannableString = SpannableString(item.title.toString())
spannableString.setSpan(RelativeSizeSpan(1.5f), 0, spannableString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableString.setSpan(TypefaceSpan("font_name"), 0, spannableString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
item.title = spannableString

the RelativeSizeSpan value 1.5f is a relative and not absolute value for the text size, so adjust it as you wish.

font size of items inside navigation drawer not changing

You must apply this attribute:

android:theme="@style/DrawerTextStyle"

to the NavigationView and not to the menu,

but first change dp to sp:

<style name="DrawerTextStyle" >
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textSize">40sp</item>
<item name="android:textStyle">bold</item>
</style>

Custom TextSize of BottomNavigationView support android

Unfortunately this first version of BottomNavigationView came with a lot of limitations. And for now you can't change titles size just using the support design API. So to solve this limitation while google doesn't implement it, you can do:

In your dimen.xml you can put:

    <dimen name="design_bottom_navigation_text_size" tools:override="true">30sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">30sp</dimen>

Doing this you are overriding the default value of dimen that the internal classes of BottomNavigationView use. So be carreful.

Adding font size to NavigationView item in android

Try like below

    //put this in main Activity
private void setFontToMenuItem(MenuItem mi) {

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/customfont.ttf");
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("" , font), 0 , mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}

Use like this

navigationView = (NavigationView) findViewById(R.id.navigation_view);
Menu m = navigationView.getMenu();
for (int i=0;i<m.size();i++) {
MenuItem item = m.getItem(i);

//for aapplying a font to subMenu ...
SubMenu subMenu = item.getSubMenu();
if (subMenu!=null && subMenu.size() >0 ) {
for (int j=0; j <subMenu.size();j++) {
MenuItem subMenuItem = subMenu.getItem(j);
setFontToMenuItem(subMenuItem);
}
}

//the method we have create in activity
setFontToMenuItem(item);
}


Related Topics



Leave a reply



Submit