Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom button with drawable background: Padding difference in Android 4 vs. Android 2.3

The following code is for displaying a custom button in Android. Unfortunately, all those padding values at those different places affect the way the button is rendered only for Android 2.3, but on Android 4.X it has no effect, i.e. the padding is always the same, regardless of what you set here. Why?

The layout XML:

<Button
    android:id="@+id/sample_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_caption"
    android:padding="6dp"
    android:layout_marginBottom="6dp"
    android:layout_marginRight="6dp"
    style="@style/CustomButton"
    android:textSize="16sp" />

The style:

<style name="CustomButton">
    <item name="android:layout_gravity">center_horizontal</item>
    <item name="android:padding">6dp</item>
    <item name="android:background">@drawable/custom_button</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:textStyle">bold</item>
</style>

The drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid
                android:color="#60ff0000" />
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="6dp"
                android:top="6dp"
                android:right="6dp"
                android:bottom="6dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid
                android:color="#90ff0000" />
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="6dp"
                android:top="6dp"
                android:right="6dp"
                android:bottom="6dp" />
        </shape>
    </item>
</selector>

When I set all these padding values to a small value such as 2, the button is shown almost without a padding on Android 2.3.3 (probably correct) but with a padding that is still very large on Android 4.X (probably wrong). Can someone see why?

like image 285
caw Avatar asked Jan 24 '13 02:01

caw


1 Answers

Android's Holo theme sets a minWidth and minHeight in the system styles.xml resource for Holo.Widget.Button which specifies rules in addition to those used by the non-holo button, Widget.Button. Even with a small button title, the button will take up 64x48dip in Android 4.2.

<Button
    android:id="@+id/sample_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Z"
    android:padding="6dp"
    android:minHeight="1dp"
    android:minWidth="1dp"
    android:layout_marginBottom="6dp"
    android:layout_marginRight="6dp"
    style="@style/CustomButton"
    android:textSize="16sp" />
like image 98
David Snabel-Caunt Avatar answered Sep 21 '22 20:09

David Snabel-Caunt