23

Something like the following, but it doesn't work. If I switch the drawable color to something like blue, it works.

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@android:color/transparent"/>
</ripple>
ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107

2 Answers2

84

It's necessary to add a mask:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
     <item android:id="@android:id/mask">
       <color android:color="@android:color/white" />
     </item>
</ripple>
nhaarman
  • 98,571
  • 55
  • 246
  • 278
JMPergar
  • 1,906
  • 1
  • 19
  • 21
2

Somehow, the @JMPergar 's answer didn't work for me.

However, I was able to think of this workaround: if a colour behind your transparent button is solid (not a gradient or a speckled picture) - you can use that color as a main button's unpressed one.

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

    <item>
        <shape>
            <corners android:radius="@dimen/profile_transparent_button_corner_radius" />
            <solid android:color="@color/profile_background" />
        </shape>
    </item>
</ripple>

where @color/profile_transparent_button_pressed is a colour to highlight a button, and @color/profile_background - the colour of a layout behind this button. It works exactly as expected, but still definitely is a workaround so you go try @JMPergar 's answer first.

Den Drobiazko
  • 1,077
  • 1
  • 13
  • 33
  • 1
    As he admits, this doesn't really answer the question, it's simply a *bad* workaround. While having a solid background color makes the ripple work, that's exactly what we want to avoid. Why? Because this background color that the user *never actually sees* creates unnecessary **overdraw** all the time, even when there's no ripple. Check by yourself: go to the developer options and enable overdraw (Debug GPU Overdraw -> Show overdraw areas). You'll see that the background is painted *twice* with the *same* color, which is a waste. Of course for an small button a bit of overdraw is acceptable. – Albert Vila Calvo Sep 03 '19 at 11:49
  • 1
    I've found how you can have a **ripple on a transparent shape** here: https://stackoverflow.com/a/41097020/4034572 – Albert Vila Calvo Sep 03 '19 at 12:50