87

In ImageButton I want to remove the standard button background image. In http://developer.android.com it is said, that one must define his\her own background image or set the background color to be transparent. I tried to set a black background, but it didn't make any effect...

Mudassir
  • 13,031
  • 8
  • 59
  • 87
lomza
  • 9,412
  • 15
  • 70
  • 85

11 Answers11

224

You can use android:background="@null" for your ImageButton.

See also:
    Android Developers official guide on Buttons

Mudassir
  • 13,031
  • 8
  • 59
  • 87
41

The best choice is not set a transparent background to your ImageButton.

Give to your user a feedback when the button is tapped.

android:background="?attr/selectableItemBackgroundBorderless"
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
Filipe Brito
  • 5,329
  • 5
  • 32
  • 42
24

ImageButton.setBackgroundResource(0)

Peter
  • 2,480
  • 6
  • 39
  • 60
  • Excellent! This is the solution for programmatically doing Mudassir's solution. Thank you! – SMBiggs Dec 09 '11 at 16:43
  • 3
    It's setBackgroundResource, not setBackgroundResources. Use it like this : ((ImageButton) findViewById(R.id.my_button)).setBackgroundResource(0); – Quentin S. Feb 07 '14 at 21:37
8

use the following property in your in the xml of ImageButton:

android:background="@drawable/icon"

where icon is the name of the image kept in your drawable.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
5

No, it has to be transparent, not black. Try color: #00FFFFFF

Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101
2

Dot't use button.setBackgroundResource(0); on some devices you will get:

android.content.res.Resources$NotFoundException: Resource ID #0x0

Better use button.setBackgroundColor(Color.TRANSPARENT);

SpyZip
  • 5,511
  • 4
  • 32
  • 53
1

Use:

android:background="@null"

in your layout xml.

digitaldaemon
  • 141
  • 1
  • 2
0
YourImageButton.setBackgroundColor(Color.TRANSPARENT);
AlBeebe
  • 8,101
  • 3
  • 51
  • 65
0
myButton.setBackgroundResource(0);
Leo
  • 3,003
  • 5
  • 38
  • 61
0

Using Kotlin, you can do this:

val myImageButton = ImageButton(context).apply({
    background = null

    // and if you need to add drawable, simply use:

    setImageDrawable(ContextCompat.getDrawable(context, 
                         R.drawable.ic_save_black_24px))
})
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
0

In the latest android version, the background image doesn't remove even when you set android:background="@null".

Setting android:minHeight="0dp" works for me.

Neela
  • 1,328
  • 4
  • 23
  • 45