87

I can set ImageButton background transparent in layout.xml using:

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

How I can acomplish same thing using java code? Something like ib.setBackgroundColor(???);

rptwsthi
  • 9,983
  • 10
  • 68
  • 107
Peter
  • 889
  • 1
  • 9
  • 9

7 Answers7

162

This is the simple only you have to set background color as transparent

    ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
    btn.setBackgroundColor(Color.TRANSPARENT);
jhoanna
  • 1,719
  • 25
  • 25
Parag Chauhan
  • 34,968
  • 13
  • 85
  • 95
33

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"/>
Gowthaman M
  • 7,600
  • 7
  • 31
  • 52
bsautner
  • 4,143
  • 32
  • 47
13

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

Chirag
  • 56,384
  • 29
  • 154
  • 197
Abhinav Manchanda
  • 6,452
  • 3
  • 38
  • 46
11

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"

This requires API 11

Zar E Ahmer
  • 32,807
  • 18
  • 222
  • 281
  • 2
    This is exactly what I needed. Using `null` or `transparent` disables the button feedback. – aks Jul 24 '16 at 06:03
4

Try like this

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 
duggu
  • 37,191
  • 12
  • 114
  • 111
Bunny
  • 1,026
  • 12
  • 23
3

simply use this in your imagebutton layout

android:background="@null"

using

 android:background="@android:color/transparent 

or

 btn.setBackgroundColor(Color.TRANSPARENT);

doesn't give perfect transparency

bourax webmaster
  • 750
  • 7
  • 18
2

If you want to use android R class

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

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

compile 'com.android.support:support-v4:23.3.0'
MarsPeople
  • 1,712
  • 18
  • 29