How to set transparent background for image button in code?

I can set the ImageButton wallpaper in layout.xml using:

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

How can I implement the same thing with java code? Something like ib.setBackgroundColor(???);

+50
android background transparent imagebutton
Jan 17 '11 at 4:25
source share
7 answers

It's simple, only you have to set the background color as transparent

  ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01); btn.setBackgroundColor(Color.TRANSPARENT); 
+101
Jan 17 2018-11-11T00:
source share

do it in your xml

 <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButtonSettings" android:layout_gravity="right|bottom" android:src="@drawable/tabbar_settings_icon" android:background="@android:color/transparent"/> 
+27
Dec 10 2018-12-12T00:
source share

This should work - imageButton.setBackgroundColor(android.R.color.transparent);

+8
Jan 17 2018-11-11T00:
source share

DO NOT USE TRANSFER OR NULL LAYOUT , because then the button (or general view) will no longer be highlighted when you click !!!

I had the same problem and finally I found the correct attribute from the Android API to solve the problem. It can be applied to any kind.

Use this in button specifications

 android:background="?android:selectableItemBackground" 

This requires API 11

+7
Feb 20 '15 at 20:53
source share

try it

 ImageButton imagetrans=(ImageButton)findViewById(R.id.ImagevieID); imagetrans.setBackgroundColor(Color.TRANSPARENT); 

OR

include this in your .xml file in res / layout

 android:background="@android:color/transparent 
+4
Jun 18 '14 at 9:27
source share

just use this in imagebutton layout

 android:background="@null" 

using

  android:background="@android:color/transparent 

or

  btn.setBackgroundColor(Color.TRANSPARENT); 

does not give perfect transparency

+3
Mar 13 '14 at 2:31
source share

If you want to use android R class

 textView.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.transparent)); 

and don't forget to add the support library to the Gradle file

 compile 'com.android.support:support-v4:23.3.0' 
+1
Apr 09 '16 at 16:51
source share



All Articles