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.
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.
xml which is under the values folder,then you should call the following: root. setBackgroundColor(getResources(). getColor(R.color.name));
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With