ekatz
ekatz

Reputation: 973

android: imageview inside button

I need to be able to show a button in my layout, but I want the button to have an image (small Icon) and text inside of it.

something like this:
  ___________________
 / ***   SOME TEXT  /
/_**_______________/

where the *s are an image

So what I thought to do is a linear layout with a background and make it clickable - which will probably work, but that means that it won't behave exactly like a button...

Do you have a better idea on how to do that?

Thanks, e.

Upvotes: 7

Views: 9900

Answers (2)

Gera Garza
Gera Garza

Reputation: 161

In your XML, you can use ImageButton (which was added in API level 1, so it should be available)

<ImageButton
   android:id="@+id/imageBtn"
   android:layout_gravity="center"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/btnSrc"
     />

This method doesn't require you to add padding to a side.

Upvotes: 2

Eric Nordvik
Eric Nordvik

Reputation: 14746

In your xml layout for the button, add:

<Button
  ...
  android:drawableLeft="@drawable/myIcon"
  android:drawablePadding="5dp"
  android:paddingLeft="10dp"
  android:text="My text" />

Where myIcon.png is your icon in the drawable folder.

EDIT Added padding. EDIT 2: Added padding to left of the icon.

Upvotes: 18

Related Questions