ImageButton doesn't highlight on click with Transparent background ImageButton doesn't highlight on click with Transparent background android android

ImageButton doesn't highlight on click with Transparent background


I came across this same problem. Finally, I got a sample of a code with that attribute:

android:background="?android:selectableItemBackground"

This attibute will give a transparent background with selectable highlight to any View (Button, ImageButton, TextView...) WITHOUT MORE CODING!!!


All you need to do is to set the proper background. If you want it to be transparent in normal state and blueish in pressed stated.

Create a StateListDrawable like this one in res/drawable directory.

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

This way the default background is transparent. When pressed the background has the color you specified (instead of color you can use any drawable here).


Just to add on Fernandez good answer:

If you want the effect to be Round and not rectangle use:

android:background="?android:selectableItemBackgroundBorderless"    

(*for V21 and up).