Is there any way to change the Item color programmatically of the selected item
in the navigation drawer?
I'am able to use the app:itemTextColor, but the problem is if i used this,
the checked in the menu item will not highlight.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="@+id/nav_search"
android:icon="@drawable/ic_search_black"
android:title="Search Location" />
<item
android:id="@+id/nav_fav"
android:icon="@drawable/ic_favorite"
android:title="Favorites" />
<item
android:id="@+id/nav_route"
android:icon="@drawable/ic_place"
android:title="Route" />
<item
android:id="@+id/nav_recent"
android:icon="@drawable/ic_nav_route"
android:title="Recent Location" />
</group>
<item android:title="Others">
<menu>
<item
android:id="@+id/nav_settings"
android:checked="true"
android:checkable="true"
android:icon="@drawable/ic_settings"
android:title="Settings" />
<item
android:id="@+id/nav_about"
android:icon="@android:drawable/ic_menu_send"
android:title="About" />
</menu>
</item>
</menu>
So let’s see together how we can use the Visual State manager to easily change the background color of the selected item in our CollectionView (it works with a ListView as well) Let’s say that we have the following CollectionView in our Xaml. To select the background color of our selected item, we now need to add the Visual State Manager.
The settings item is defined here along with its icon, so modifying it is quite easy. Not so much with the text, however. It turns out the control sets the NavigationViewItem.Content in its code to provide a localized Settings text. The proper solution to this problem involves deriving a custom class from NavigationView .
By default, the NavigationView pane uses a different background depending on the display mode: the pane is a solid grey color when expanded on the left, side-by-side with the content (in Left mode). the pane uses in-app acrylic when open as an overlay on top of content (in Top, Minimal, or Compact mode).
While NavigationView control provides a settings navigation item by default, it is a bit hard to customize it. Luckily we can customize the control itself and set it up to look fit our needs ideally. Hi, I'm Martin and I love mobile development!
create a selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primary" android:state_checked="true" />
<item android:drawable="@android:color/transparent" />
</selector>
and set
app:itemBackground="@drawable/nav_view_item_background"
then the selected item will be highlighted.
if you want to change the text color then set
app:itemTextColor="@drawable/nav_view_item_textcolor"
and create a selector for it like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_checked="true" />
<item android:color="@color/primary" />
</selector>
Note : Yasoda's answer not worked for me, which is true in most of the cases.
Though I set the state_checked
is true and give it a drawable color, it seems the item is never checked.
For more search, I found the issue was happened with menu item. I hadn't given checked item to true in menu item in menu file.
Then I have to add a tag android:checkable=true
to it.
and it works like charm.
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