6

I am developing an Android app. In my app, I am adding menu item to navigation view programmatically. But I am having a problem with designing that items when checked.My problem is now only text color is changed when item is checked. But what want is I want item highlighted with background like below.

enter image description here

I am adding menu item programmatically like this

menu.add(CATEGORY_MENU_GROUP_ID, itemId, i + 1, title).setIcon(dIcon).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {

                        item.setChecked(true);
                        return true;
                    }
                });

I followed this question Android - Navigation View item menu background color

So I created background item like this (nav_item_bg.xml)

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- view background color -->
    <solid android:color="@color/lightGray" >
    </solid>

    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="@color/lightGray" >
    </stroke>

    <!-- Here is the corner radius -->

</shape>

My navigation view xml

<android.support.design.widget.NavigationView
        app:itemIconTint="@drawable/drawer_item"
        app:itemTextColor="@drawable/drawer_item"
        android:id="@+id/left_nv_view"

        app:headerLayout="@layout/header_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

This is my drawer_item.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorAccent"  android:state_enabled="true" android:state_checked="true" />
    <!--state is enabled and not checked-->
    <item android:color="@color/black" android:state_enabled="true" android:state_checked="false" />
    <!--state (menu item) is disabled -->
    <item android:state_enabled="false" android:color="@color/lightGray"  />
</selector>

When I run, background of all menu item in navigation view are changed even they are not checked. What I want is just changed when selected. How can I achieve it?

I tried changing the nav_item_bg.xml to this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/lightGray"  android:state_enabled="true" android:state_checked="true" />
    <!--state is enabled and not checked-->
    <item android:color="@color/white" android:state_enabled="true" android:state_checked="false" />
    <!--state (menu item) is disabled -->
    <item android:state_enabled="false" android:color="@color/white"  />
</selector>

But it is not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • try to change this property `app:itemIconTint="@drawable/drawer_item" app:itemTextColor="@drawable/drawer_item"` and give color which are apply. – Harshad Pansuriya Jul 19 '16 at 08:59
  • I also had the same problem. You can try this library it has tons of features, and highlighting is enabled by default https://github.com/mikepenz/MaterialDrawer – Mustansir Jul 19 '16 at 09:00
  • I changed. But not just working @Ironman – Wai Yan Hein Jul 19 '16 at 09:18
  • @WaiYanHein Can you tell me why are you setting `Menu` programatically because in UpDate Version The complete Navigation Drawer is ready to work. So Check if your not using Update Version Just Update the Version it will give you directly all of this .. – Harshad Pansuriya Jul 19 '16 at 09:20
  • I am setting programmatically cause all data from API. To use this https://github.com/mikepenz/MaterialDrawer I have to change the whole activity. – Wai Yan Hein Jul 19 '16 at 09:27

1 Answers1

4

After add new menu, just need to set checkAble value for it:

menu.add(CATEGORY_MENU_GROUP_ID, itemId, i + 1, title).setIcon(dIcon).setCheckable(true);
anna
  • 41
  • 4
  • 1
    `menu.setCheckable(true);` - this enables the background color of the menu item, then `menu.setChecked(true)` - this colors the menu item text. – LiuWenbin_NO. Jan 01 '19 at 22:06