android - Change the color of a checked menu item in a navigation drawer in different menus -
this activity_main_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkablebehavior="single"> <item android:id="@+id/nav_hasiera" android:title="@string/nav_hasiera" /> <item android:id="@+id/nav_oharrak" android:title="@string/nav_oharrak" /> <item android:id="@+id/nav_instalazioak" android:title="@string/nav_instalazioak" /> <item android:id="@+id/nav_gustokoak" android:title="@string/nav_gustokoak" /> </group> <item android:title="@string/filtrar_por"> <menu> <item android:id="@+id/nav_lokalizazioa" android:title="@string/nav_lokalizazio" /> <item android:id="@+id/nav_ekintzak" android:title="@string/nav_ekintzak" /> <item android:id="@+id/nav_ordutegia" android:title="@string/nav_ordutegia" /> </menu> </item> <item android:title="@string/opciones"> <menu> <item android:id="@+id/nav_hizkuntza" android:title="@string/nav_hizkuntza" /> </menu> </item> </menu>
when select items
of group
tag, background color of item changes. ok.
but when select items
of filtrar por...
or opciones
, background color not change.
i checked this post , this not fix problem
edit: activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainactivity_linearlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/donostiakirola_fondo_pantalla" tools:context=".mainactivity"> <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"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/nav_header_main" android:background="@color/colorprimarydark" app:itemtextcolor="@color/white" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.drawerlayout> </linearlayout>
in res/color folder, add navigation_bg_color.xml file.
and add following code it:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/defaultbgcolor"/> <!-- default --> <item android:color="@color/selectedbackgroundcolor" android:state_selected="true" /> <item android:color="@color/selectedbackgroundcolor" android:state_checked="true" /> </selector>
and use following code activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainactivity_linearlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/donostiakirola_fondo_pantalla" tools:context=".mainactivity"> <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"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/nav_header_main" app:itemtextcolor="@color/white" app:itembackground="@color/navigation_bg_color" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.drawerlayout> </linearlayout>
and don't forget declare color values in colors.xml file: defaultbgcolor, selectedbackgroundcolor
Comments
Post a Comment