Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android removing navigation menu item highlighting

Tags:

android

When you click a menu item in my navigation drawer, it becomes slightly darker and remains that way until another item is clicked. I'm trying to keep it the same colour no matter what state it's in.

Is it some kind of overlay? I've think I've covered all possible states in drawer_items.xml.
How do I prevent this behaviour?

activity_main.xml

<android.support.design.widget.NavigationView
  .
  .
  android:background="@drawable/drawer_items"
  .
  app:menu="@menu/activity_main_drawer" />

drawer_items.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/grey" android:state_activated="true" />
  <item android:drawable="@color/grey" android:state_selected="true" />
  <item android:drawable="@color/grey" android:state_focused="true"  />
  <item android:drawable="@color/grey" android:state_pressed="true" />
  <item android:drawable="@color/grey" android:state_checked="true" />
  <item android:drawable="@color/grey" />
</selector>

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single"
           android:id="@+id/arbitrary_id_1">
      <item
        android:id="@+id/nav_refresh"
        android:icon="@drawable/ic_refresh_24dp"
        android:title="Try Again/Refresh" />
    </group>

    <group android:checkableBehavior="single"
           android:id="@+id/arbitrary_id_2">
      <item
        android:id="@+id/nav_back"
        android:icon="@drawable/ic_fast_rewind_24dp"
        android:title="Go Back" />
    </group>

    <group android:checkableBehavior="single"
           android:id="@+id/arbitrary_id_3">
      <item
        android:id="@+id/nav_3" />
    </group>

</menu>
like image 481
Bazley Avatar asked Jul 05 '16 14:07

Bazley


People also ask

How to highlight menu item?

You can highlight a menu by adding a different background color, text color etc to the particular menu item using custom CSS. To apply custom CSS you need to add CSS class for the menu.

How to use navigation drawer in Android app?

Add a navigation drawerThe drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen. Figure 3. An open drawer displaying a navigation menu. The drawer icon is displayed on all top-level destinations that use a DrawerLayout .


2 Answers

Maybe you can use something like this,
to prevent item from changing its color when clicked, you can try to change in activity_main_drawer.xml:

<group android:checkableBehavior="single">

to

<group android:checkableBehavior="none">

Or you can do this way:

<group android:checkableBehavior="single">
    ...
    <item
        android:id="@+id/item_one"
        android:icon="@drawable/ic_item_one"
        android:title="Item One"/>
    ...
  </group>

To:

<item>
    <menu>
      ...
      <item
        android:id="@+id/item_one"
        android:icon="@drawable/ic_item_one"
        android:title="Item One"/>
      ...
    </menu>
  </item> 

Read more at Menus

like image 65
ישו אוהב אותך Avatar answered Sep 20 '22 15:09

ישו אוהב אותך


So the NavigationView is highlighted based on the colorControlHighlight property. You could create a theme for your drawer and make it transparent:

<style name="Drawer" parent="Theme.AppCompat.Light">
    <item name="colorControlHighlight">#0000</item>
</style>

<android.support.design.widget.NavigationView
    ...
    android:theme="@style/Drawer" />
like image 32
tachyonflux Avatar answered Sep 22 '22 15:09

tachyonflux