13

I would like to remove the padding around text in Button view. The first screenshot is the result I would achieve, and the second one is the state of the art.

enter image description here

enter image description here

Of course, I have defined a custom drawable to get the button appearance. But even if I set the padding attribute to 0dp the result does not change.

Any suggestion, please?

EDIT Here is the xml code of the button

<Button
        android:id="@+id/btnCancel"
        style="@style/dark_header_button"
        android:layout_width="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_height="wrap_content"
        android:includeFontPadding="false"
        android:padding="0dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="@android:string/cancel" />

Here is the style xml file:

<style name="dark_header_button">
    <item name="android:background">@drawable/bkg_dark_header_button</item>
    <item name="android:shadowDy">-1</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowRadius">1</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">#ffffff</item>
</style>

and here is the drawable xml file:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">
    <shape>

        <corners android:radius="10dp" />

        <gradient 
            android:angle="90" 
            android:endColor="#060606"
            android:startColor="#707070"
            android:type="linear" />

    </shape>
</item>
<item>
    <shape>

        <corners android:radius="10dp" />

        <gradient 
            android:angle="90" 
            android:endColor="#707070" 
            android:startColor="#060606" 
            android:type="linear" />

        <stroke 
            android:width="0.5dp" 
            android:color="#2b2b2b" />

    </shape>
</item>


</selector>
4
  • mind to share your xml too....?
    – waqaslam
    Oct 7, 2013 at 14:07
  • "Of course, I have defined a custom drawable to get the button appearance" Please show us this definition, if you define your own there should be no padding
    – A.S.
    Oct 7, 2013 at 14:08
  • Do you have your layout_height attribute set as wrap_content? And make sure you 9 patched images.
    – yugidroid
    Oct 7, 2013 at 14:14
  • Sorry guys I forgot the code. @yugidroid it is and xml drawable so no 9-patch. And yes, layout_height is set correctly. Oct 7, 2013 at 14:23

3 Answers 3

57

Its not actually padding but the theme is setting a minHeight and minWidth for button widgets. You can completely remove this effect without changing your theme by setting the following on your button in the xml:

android:minHeight="0dp"
android:minWidth="0dp"

It thus also implies that you can play around with different settings for different effects, 0dp is merely an example to completely remove this effect.

19

you can try using android:includeFontPadding. set it to false. also set the padding to 0.

1

Button text doesn't need padding, it is by default in center of the button. I think you are using the android:theme="@android:style/Theme.Black.NoTitleBar" in the manifest file, please remove it and use native app Theme and try it.

2
  • Thank you!! It was set Theme.Holo.Light.NoActionBar by default. Oct 7, 2013 at 14:26
  • 2
    And what if you don't want a title/action bar?
    – Andrew
    Sep 4, 2014 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.