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.

Was it helpful?

Solution

Try using null for the background ...

android:background="@null"

OTHER TIPS

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"

You can also use a transparent color:

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

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.

in run time, you can use following code

btn.setBackgroundDrawable(null);

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)

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.

The best way is using the transparent color code

android:background="#00000000"

use the color code #00000000 for making any thing transparent

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

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>

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" />

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

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

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

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

(Android Studio 3.4.1)

This is programatically set background color as transparent

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

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

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   

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top