Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button scales to background image instead of content

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?

like image 668
Turnsole Avatar asked Jul 27 '11 20:07

Turnsole


4 Answers

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.

like image 112
Drejc Avatar answered Nov 17 '22 03:11

Drejc


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.)

like image 28
Turnsole Avatar answered Nov 17 '22 03:11

Turnsole


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.

like image 1
Adam Storm Avatar answered Nov 17 '22 05:11

Adam Storm


Add these two items to your CustomButton style

<item name="android:minWidth">0dp</item> 
<item name="android:minHeight">0dp</item>
like image 1
MCH Avatar answered Nov 17 '22 04:11

MCH