2

In navigationView when i select an item, the selected item should be in bold and increase in font size. How do i achieve this?

Basically, I want to add a selector for the navigation item which will display bold font on selected state and normal font on non-selected state.

Thanks

Divya
  • 121
  • 2
  • 11
  • try like this http://stackoverflow.com/questions/33140138/changing-the-item-text-size-in-navigation-view – Vishal Thakkar Feb 14 '17 at 04:46
  • I want to create a selector for the navigation item. I want different text size and styles for selected item and non-selected item – Divya Feb 14 '17 at 05:23
  • see this http://stackoverflow.com/questions/31221637/android-navigation-view-item-menu-background-color – Vishal Thakkar Feb 14 '17 at 06:46
  • Through above link we can only change the color of item in different states but I want to change the font size and style of item selected. Is there any way for it? – Divya Feb 14 '17 at 06:51
  • Then i think you have to code for it on item click. – Vishal Thakkar Feb 14 '17 at 07:09
  • like this https://blog.samsao.co/how-to-customize-the-text-appearance-of-a-selected-section-in-a-navigation-drawer-c6c372e27b66#.c2p91lwph – Vishal Thakkar Feb 14 '17 at 07:09
  • @VishalThakkar Yes this is possible when creating own listview and adapters But is it not possbile with NavigationView? – Divya Feb 14 '17 at 07:23

2 Answers2

4

Styles.xml

    <style name="NavDrawerTextStyle" parent="Base.TextAppearance.AppCompat">
    <item name="android:textColor">@color/colorPrimaryDark</item>
    <item name="android:textSize">30sp</item>
    <item name="android:textStyle">bold</item>
    </style>

and then in your NavigationView:

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    </android.support.v4.widget.DrawerLayout>
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • I want to create a selector for the navigation item. I want different text size and styles for selected item and non-selected item – Divya Feb 14 '17 at 04:44
3

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);
    }
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33