Advertisement
Advertisement


How to have a transparent ImageButton: Android


Question

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

2010/08/04
1
499
8/4/2010 5:15:18 AM

Accepted Answer

Try using null for the background ...

android:background="@null"
2010/08/04
1001
8/4/2010 5:17:43 AM


You can also use a transparent color:

android:background="@android:color/transparent"
2011/07/09

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.

2013/07/23

in run time, you can use following code

btn.setBackgroundDrawable(null);
2012/09/05

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)

2016/01/28

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.

2010/08/04

Source: https://stackoverflow.com/questions/3402787
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]