Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageButton doesn't highlight on click with Transparent background

I've set up an ImageButton to be transparent, so the icon matches the backgrond panel like the Android ActionBar. This looks fine as I want it to.

However, when the background is transparent, there isn't the blueish highlight you see as when you press a transparent button in the action bar.

Can I have an ImageButton that is transparent and also has the highlight flash when clicked?

<ImageButton     android:id="@+id/nextItemButton"     style="?android:attr/buttonStyleSmall"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentRight="true"     android:layout_alignParentTop="true"     android:background="@null"     android:src="@drawable/ic_media_ff" /> 
like image 258
Stealth Rabbi Avatar asked Oct 10 '12 18:10

Stealth Rabbi


2 Answers

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!!!

like image 188
Fernandez Avatar answered Sep 24 '22 17:09

Fernandez


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).

like image 23
Tomik Avatar answered Sep 21 '22 17:09

Tomik