3

I am attempting to replace what I thought was the default item background of the NavigationView. So I created a drawable to replace the background and applied it using the itemBackground attribute.

background_navigation_view_item.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/navigation_item_background_tint">
    <item android:id="@android:id/mask" android:top="4dp" android:bottom="4dp" android:left="8dp" android:right="8dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="3dp"/>
        </shape>
    </item>
    <item android:top="4dp" android:bottom="4dp" android:left="8dp" android:right="8dp">
        <selector>
            <item android:state_checked="true">
                <shape android:shape="rectangle">
                    <solid android:color="@color/navigation_item_background_tint"/>
                    <corners android:radius="3dp"/>
                </shape>
            </item>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/transparent"/>
                    <corners android:radius="3dp"/>
                </shape>
            </item>
        </selector>
    </item>
</ripple>

The item background drawable looks great when the item is selected ("Selection Controls" is the selected item in the image below). But when the item is being clicked ("Progress Indicators" below) it appears that both the default ripple drawable and the background_navigation_view_item.xml drawable are being used (only a rounded rectangle should be shown, not the rectangle surrounding it).

Navigation View Ripple Effect

I also tested the drawable as the background of a checkable view, and it works the way I would expect. So if it isn't an error in the code for my drawable, why isn't the original background drawable being replaced?

Bryan
  • 14,756
  • 10
  • 70
  • 125

1 Answers1

5

I have already answered this question here

Note: This code just removes the opacity of background default ripple

Anyways, I'll answer it again here.

Assuming your NavigationView code is:

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/your_menu"
app:itemBackground="@drawable/background_navigation_view_item.xml"/>

What I've done is, added a theme attribute as follows to the NavigationView:

android:theme="@style/NavigationItemNoRipple"

In styles.xml:

<style name="NavigationItemNoRipple">
    <item name="android:colorControlHighlight">@android:color/transparent</item>
</style>
Karthik
  • 111
  • 1
  • 8
  • Except that `?android:attr/colorControlHighlight` is the natural choice for the ripple color; which would make both the inherent ripple effect and the custom ripple effect transparent. Fortunately a simple solution to that would be to use another color for the custom effect, not relying on `colorControlHighlight`. – Bryan May 01 '19 at 15:52