Setting the background color for the programmatically added menu item "Navigation View" when installed in Android

I am developing an application for Android. In my application, I add a menu item to the navigation view programmatically. But I had a problem with the development of these elements when they are marked. My problem is that when checking an item, only the color of the text changes. But I want the element to be highlighted as shown below.

enter image description here

I add a 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 Android question - Navigation View menu item background color

So I created a background element 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 kind of xml navigation

<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 box_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>

, , . , , . ?

nav_item_bg.xml

<?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>

.

+4
1

checkAble:

menu.add(CATEGORY_MENU_GROUP_ID, itemId, i + 1, title).setIcon(dIcon).setCheckable(true);
+3

Source: https://habr.com/ru/post/1648344/


All Articles