515
votes
<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@drawable/transparent"></ImageButton>

This is what I tried to get a transparent ImageButton so as to place those buttons on a SurfaceView. But Eclipse, gives me an error in the project as soon as I include the transparent line in xml.

Please help.

20
just a side note: instead of your own transparent image, you could generally also just use @android:color/transparent - no need for your own custom transparent imageMathias Conradt
Please note that by giving the image button a transparent background, you're removing a visual feedback for button clicked (and probably disabled) state. This results in a slight decrease of usability.szeryf
In order to be sure that the ImageButton actually gets placed upon the SurfaceView it is also useful to call previousButton.bringToFront(), otherwise it may still be hidden behind the SurfaceViewJay Snayder
The Android API provides the correct attribute to create a transparent background without losing the visual feedback for button clicked or for the other states! Read my answer below!lory105

20 Answers

1035
votes

Try using null for the background ...

android:background="@null"
341
votes

DON'T USE A TRANSAPENT OR NULL LAYOUT because then the button (or the generic view) will no more highlight at click!!!

I had the same problem and finally I found the correct attribute from Android API to solve the problem. It can apply to any view.

Use this in the button specifications:

android:background="?android:selectableItemBackground"
142
votes

You can also use a transparent color:

android:background="@android:color/transparent"
113
votes

Setting the background to "@null" will make the button have no effect when clicked. This will be a better choice.

style="?android:attr/borderlessButtonStyle"

Later I found that using

android:background="?android:attr/selectableItemBackground"

is also a good solution. And you can inherit this attribute in your own style.

14
votes

in run time, you can use following code

btn.setBackgroundDrawable(null);
11
votes

I believe the accepted answer should be: android:background="?attr/selectableItemBackground"

This is the same as @lory105's answer but it uses the support library for maximum compatibility (the android: equivalent is only available for API >= 11)

8
votes

Remove this line :

android:background="@drawable/transparent">

And in your activity class set

ImageButton btn = (ImageButton)findViewById(R.id.previous);
btn.setAlpha(100);

You can set alpha level 0 to 255

o means transparent and 255 means opaque.

7
votes

Don't use null or transparent if you need a click animation. Better:

//Rectangular click animation
android:background="?attr/selectableItemBackground"

//Rounded click animation
android:background="?attr/selectableItemBackgroundBorderless"
5
votes

The best way is using the transparent color code

android:background="#00000000"

use the color code #00000000 for making any thing transparent

2
votes

Use ImageView... it have transparent background by default...

2
votes

I was already adding something to the background so , This thing worked for me:

   android:backgroundTint="@android:color/transparent"

(Android Studio 3.4.1)

EDIT: only works on android api level 21 and above. for compatibility, use this instead

   android:background="@android:color/transparent"
1
votes

Use this:

<ImageButton
 android:id="@+id/back"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@null"
 android:padding="10dp"
 android:src="@drawable/backbtn" />
1
votes

Set the background of the ImageButton as @null in XML

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@null"></ImageButton>
1
votes

Use "@null" . It worked for me.

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/bkash"
    android:id="@+id/bid1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@null" />
1
votes

It's android:background="@android:color/transparent"

<ImageButton
    android:id="@+id/imageButton"
    android:src="@android:drawable/ic_menu_delete"
    android:background="@android:color/transparent"
/>
1
votes

You can use the following code works just fine by setting the background to transparent:

<ImageButton 
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="transparent"></ImageButton>
0
votes

This is programatically set background color as transparent

 ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
 btn.setBackgroundColor(Color.TRANSPARENT);
0
votes

Programmatically it can be done by :

image_button.setAlpha(0f) // to make it full transparent
image_button.setAlpha(0.5f) // to make it half transparent
image_button.setAlpha(0.6f) // to make it (40%) transparent
image_button.setAlpha(1f) // to make it opaque
0
votes

In your XML set Background attribute to any colour White(#FFFFFF) shade or Black(#000000) shade.if you want transparency just put 80 before the actual hash code.

#80000000   
-2
votes
<ImageButton
    android:id="@+id/previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/media_skip_backward">
</ImageButton>

I used a transparent png for the ImageButton, and the ImageButton worked.