Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 609
ekatz Avatar asked Mar 14 '11 22:03

ekatz


2 Answers

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.

like image 110
Eric Nordvik Avatar answered Sep 22 '22 00:09

Eric Nordvik


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.

like image 26
Gera Garza Avatar answered Sep 21 '22 00:09

Gera Garza