Home » Android » android – How do I get the ripple effect for menu items on my custom toolbar?

android – How do I get the ripple effect for menu items on my custom toolbar?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a toolbar with the following layout

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/primary_dark"
    android:theme="@style/ToolbarTheme"/>

and the theme I’m using

<style name="ToolbarTheme" parent="AppTheme">
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:popupBackground">@color/white</item>
        <item name="android:textColor">@color/black</item>
        <item name="actionMenuTextColor">@color/black</item>
        <item name="android:textColorSecondary">@color/white</item>
    </style>

Now on pre-lollipop devices the selectable background for the menu items works just fine but on lollipop there’s no background for tapping those items. What do I need to add to get the ripple effect for lollipop?

How to&Answers:

OK so for anyone interested I figured it out. You need to define a separate style for your toolbar in the ‘values-v21’ folder then add the following items to it:

<item name="selectableItemBackground">?android:selectableItemBackground</item>
<item name="android:colorControlHighlight">@color/ripple_material_dark</item>

By the way, my AppTheme inherits from Theme.AppCompat.Light.NoActionBar