Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom buttons with icon and text

Tags:

android

I want to create two new custom buttons to use in my android application.

I want them to be like this:

  • Icon on the left
  • Text on the right for a button
  • Another button with Icon on top and text on bottom

So basically I will have a png image stored in my resources that will be the button's icon. I will have another image as a 9patch stretchable as the button background. I tried something but the result is hideous, so I must be doing something wrong.

This code:

   <Button 
             android:layout_width="100px"
       android:layout_height="100px"
             android:drawableTop="@drawable/imgIcon"
             android:drawablePadding="2px"
             android:text="Text"
             android:background="@drawable/button_background"
             />

Later Edit: If I make like suggested by Macarse bellow:

<Button android:id="@+id/yourid"
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="Your text here" 
       android:drawableTop="@drawable/imgdonetracks">
</Button>

I get like in first image

If I make this change:

<Button android:id="@+id/yourid"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="Your text here" 
        android:drawableTop="@drawable/imgdonetracks"
        android:background="@drawable/button_background">
   </Button>

everything get's wrong

Check image http://img255.imageshack.us/i/android3.png/

like image 281
Alin Avatar asked Sep 07 '10 11:09

Alin


1 Answers

<button android:id="@+id/yourid"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="Your text here" 
        android:drawabletop="@drawable/[image]">
</button>

Here is the documentation: http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableTop

like image 178
thpoul Avatar answered Sep 29 '22 01:09

thpoul