Bip901
Bip901

Reputation: 808

Using android:itemBackground with a ripple effect

I'm using the android:itemBackground XML attribute to change the background color of the items in my overflow menu (three dots menu). My styles.xml looks as follows:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:itemBackground">@color/colorItemBg</item>
</style>

This works, but makes the ripple effect on the items completely disappear. This effect is critical for my user experience. I tried setting the itemBackground to @drawable/custom_background, which is an XML drawable I defined like so:

<?xml version="1.0" encoding="utf-8" ?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/ripple_material_dark">
  <item android:id="@android:id/mask"
    android:drawable="@color/colorItemBg" />
</ripple>

But this ignores my custom background altogether and uses the default one. I also tried all answers to this question to no avail.

How can I change the background color of my menu items and keep the ripple effect?

Upvotes: 1

Views: 667

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14991

You could try to set actionOverflowMenuStyle like below :

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
  <item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
</style>


<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
   <item name="android:popupBackground">@color/colorItemBg</item>
</style>

Upvotes: 3

Related Questions