5

What I need to do is have the background image just fill up the natural size of the button, as when no background is specified and the default grey background image is shown. But instead, my button scales up to the size of the background image rather than the text.

There was a previous question, but nobody had a solution.

The button:

enter image description here

<Button
    android:id="@+id/morebtn"
    style="@style/CustomButton"
    android:text="More" />

I have this custom button style (greybutton is a 9-patch):

<resources>
    <style name="CustomButton" parent="@android:style/TextAppearance">
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:layout_marginLeft">10dip</item>
        <item name="android:layout_marginRight">10dip</item>
        <item name="android:layout_marginBottom">10dip</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_centerInParent">true</item>
        <item name="android:background">@drawable/greybutton</item>
        <item name="android:textSize">20sp</item>
    </style>
</resources>

Right now it fills up the screen widthwise, despite being told to wrap_content. When I remove android:background from the Button style, the button shrinks down to wrap the text as expected.

Does anyone see why my background image isn't behaving like the default Button background?

Community
  • 1
  • 1
Turnsole
  • 3,422
  • 5
  • 30
  • 52

4 Answers4

5

Why don't you use ImageButton instead:

<ImageButton android:src="@drawable/greybutton"
            android:scaleType="fitCenter" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

then you can adjust the scaling with the scaleType attribute. The only downside is you can't set any text.

Drejc
  • 14,196
  • 16
  • 71
  • 106
2

Damnit. I had an hdpi drawable that hadn't been converted to 9patch, and for some reason that was getting applied instead of the one in the mdpi drawables folder even on my mdpi screen. The background scaling actually does work as expected after all. Sorry!

(So if you're browsing this question with a similar problem, check your drawables folder to make sure you're drawing what you think you are. Also: may wish to be sure the content boundaries in your nine-patch are of the appropriate size. Note that they aren't the same as the stretchable boundaries.)

Svante
  • 50,694
  • 11
  • 78
  • 122
Turnsole
  • 3,422
  • 5
  • 30
  • 52
  • By this I mean that: properly set up, a nine-patch background image will scale to fit the content of a Button. – Turnsole Jul 28 '11 at 17:37
1

Add these two items to your CustomButton style

<item name="android:minWidth">0dp</item> 
<item name="android:minHeight">0dp</item>
MCH
  • 239
  • 2
  • 3
1

You could try hard coding the height and width of the buttons using dip. That will be an issue if you need the Text on the buttons to change but it should work to restrain the button from expanding to the size of the image. Of course a large image will make the button larger.. the width / height is wrap_content, and the image is clearly part of the content... try hardcoding width / height parameters.

Adam Storm
  • 724
  • 1
  • 6
  • 13
  • The image isn't wider than the text, and certainly not large enough to span the screen by itself. It's obviously being scaled up, but I don't understand why it's being stretched across the screen instead of just far enough for the text of the Button. – Turnsole Jul 27 '11 at 21:15
  • try replacing layout_centerinparent with android:scaleType="fitCenter" – Adam Storm Jul 27 '11 at 21:30