Cole
Cole

Reputation: 2815

Any way to remove the grey background from ImageButtons?

I have an ImageButton and I'd like to remove the ugly (IMHO) background that surrounds the Image. I could just add an ImageView, but they're very hard to get set perfectly in a Layout like the grey one pictured. [gravity "Center" doesn't make it go to the middle, just centers it Vertically.)

So any way to remove that?

enter image description here

Upvotes: 29

Views: 32534

Answers (11)

Michael Hamilton
Michael Hamilton

Reputation: 111

If using Xamarin Forms it can be done like this in the xaml view. Set background to transparent.

<ImageButton BackgroundColor="Transparent"

Upvotes: 0

mille
mille

Reputation: 101

I'm having the same problem but now it has been solved. If you are using "android:background="@null" it will resulted of no background at all and no button pressed animation will be appeared to the user.

Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.

 "android:background="?android:selectableItemBackground" 

Upvotes: 5

Jeremi
Jeremi

Reputation: 1474

All proposed solutions remove the ripple animation (on tap)

Settings background tint mode to 'add' will remove the background and keep ripple animation:

android:backgroundTintMode="add"

Upvotes: 1

David Aleksanyan
David Aleksanyan

Reputation: 3369

You can keep the square box and just change its color like this:

android:backgroundTint="@color/colorPrimary"

works beautifully

Upvotes: 5

Ratnesh
Ratnesh

Reputation: 65

Use like this

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

android:src="@drawable/icon"

Upvotes: 0

Jaspinder Kaur
Jaspinder Kaur

Reputation: 1149

just useandroid:background="@null" in your xml

Upvotes: 3

user1161960
user1161960

Reputation: 90

please use the Draw 9-patch format for the image b'use it is provided by SDK

1. From a terminal, launch the draw9patch application from your SDK /tools directory.
2. Drag your PNG image into the Draw 9-patch window (or File > Open 9-patch... to locate  the file). Your workspace will now open.

The left pane is your drawing area, in which you can edit the lines for the stretchable patches and content area. The right pane is the preview area, where you can preview your graphic when stretched.

3. Click within the 1-pixel perimeter to draw the lines that define the stretchable patches and (optional) content area. Right-click (or hold Shift and click, on Mac) to erase previously drawn lines.

4. When done, select File > Save 9-patch...

Your image will be saved with the .9.png file name.

Upvotes: 0

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24463

Just use android:background="#0000" (#0000 same with #00000000) or

ImageButton imageButton = new ImageButton(this);
imageButton.setBackgroundDrawable(null);

Upvotes: 47

dong221
dong221

Reputation: 3430

The default background is not transparent.

So, just add the transparent color "#00000000" as you background, then you could solve it.

p.s. #00000000 is the transparent color

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" 
android:background="#00000000"
/>

Upvotes: 30

Sadeshkumar Periyasamy
Sadeshkumar Periyasamy

Reputation: 4908

Yeah. If you drag ImageButton that will usually ask for ImageSource. But the ImageSource(android:src) doesn't remove the gray background. It is an image that should be on top of background:

Instead of that try android:background in layout XML.

Upvotes: 0

ByteMe
ByteMe

Reputation: 1476

if you set the

android:background 

instead of

android:src 

it should overwrite the gray background

Upvotes: 0

Related Questions