0

I am having a problem trying to set the background of a button to be a bitmap image. I have the following in my layout xml file:

<Button
    android:id="@+id/p1b1"
    android:layout_width="90px"
    android:layout_height="60px"
    android:layout_alignParentLeft="true"
    android:background="@drawable/gridt"


    android:text="" />

This is just the part for the button. I have the gridt.png image in my drawable-hdpi, drawable-ldpi and drawable-mdpi folders.

The button image simply shows a white box on the empty button and the corners are not rounded.

I need to do this on several buttons, but learning how to efficiently do this on one button will be a good starting point. I would like to use a Button and not an ImageButton as I have text o the button as well.

Thanks.

eljainc
  • 117
  • 2
  • 6
  • 12

1 Answers1

4

Use

android:drawableTop="@drawable/gridt"

This will put the text below your drawable on the button

Alternatively you can use

android:drawableBottom="@drawable/gridt"
android:drawableLeft="@drawable/gridt"
android:drawableRight="@drawable/gridt"

depending on where you want the text placed relative to the picture

dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • If I want to stretch the image to the size of the button and also center the text on the button, how would I go about doing that? Also, I have noticed that the image doesn't quite fit the button-my image is the same size in pixels (90x60) as the size of the defined button – eljainc Oct 06 '11 at 21:54
  • Ah I see. Unfortunately I don't think there's a straightforward answer. You can take a look at what someone else did here: http://stackoverflow.com/questions/4817449/how-to-have-image-and-text-center-within-a-button#5524749 It's somewhat "hacky" but it does work the way you describe and I believe it's the only way to do this. – dymmeh Oct 07 '11 at 12:51