Skip to content Skip to sidebar Skip to footer

Android - 9patch Padding On A Button

I created a button with a 9patch image as a background. In the 9patch image I used the bottom and right lines to specify the content area/padding, to position the text on the butto

Solution 1:

Explicitly setting android:padding="@null" solved my issue.

The issue seemed to be that a Button (or one of the styles in the theme I'm using) is setting the padding, which overrides all other padding in the button - padding in the background drawable, as well as paddingTop, paddingLeft etc.

My button layout is now defined like this:

<Button 
        android:layout_width="450dp"
        android:layout_height="198dp"
        android:text="Some text here"
        android:gravity="center"
        android:padding="@null"
        android:background="@drawable/my_button"/>

My IDE (IntelliJ IDEA 11.1) picked up the @null as an error, even though it compiled. Setting it to -1px also worked.

Solution 2:

If you use a 9-patch image as background, both the padding and the stretched areas are defined in the background.

It's quite graphic if you use the draw9patch tool: the padding is defined (indirectly) with the content area (lines on right and bottom): http://developer.android.com/guide/developing/tools/draw9patch.html

Post a Comment for "Android - 9patch Padding On A Button"