10

Today I encountered a quit weird layout issue and I have not found out any helpful answer from Google.

On my layout, I have a button with text on the left and an icon on the right. I want the text to be 20dp left margin to the border of the button then I set paddingLeft to the button but it's not working. By chance, I set background color for the button and the padding works like charm. Anyone can help me explain this thing.

The layout is as below

<Buttonandroid:layout_width="fill_parent"
        android:drawableRight="@drawable/right_arrow"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_height="72dp"
        android:text="Button"
        android:id="@+id/btn"
        android:gravity="center_vertical"
        android:fontFamily="roboto regular"
        android:textColor="#ffffff00"
        style="@android:style/Widget.DeviceDefault.Button.Borderless" />

Thank you all!

meaholik
  • 440
  • 5
  • 19
  • do you want to separate the button and the drawable ? – Abhishek Apr 17 '15 at 05:22
  • I updated the question. I want the icon to be 20dp right margin and the text to be 20dp left margin to the border of my button – meaholik Apr 17 '15 at 05:24
  • 1
    What I really need in this question is an explanation why the padding left could not work without setting background color for the button... – meaholik May 13 '15 at 17:54

3 Answers3

28

Setting minWidth and minHeight seem to do the trick of getting the padding and margins to work properly with or without a background.

<Button 
    android:minHeight="0dp"
    android:minWidth="0dp" ...

As far as why the background does anything to how the padding works... I think it has to do with this bit of code in View.java

protected int getSuggestedMinimumWidth() {
    return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}

protected int getSuggestedMinimumHeight() {
    return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
}

https://stackoverflow.com/a/20323723/4401507

476rick
  • 2,764
  • 4
  • 29
  • 49
Nick
  • 446
  • 4
  • 7
2

You can try this

Instead of using Button you can use Textview, it works same,as what you want

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/right_arrow"
        android:gravity="center_vertical"
        android:text="Button"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:textColor="#ffffff00" />

Hope this works for you.

A.R.
  • 2,631
  • 1
  • 19
  • 22
  • 1
    "android:padding" worked with a Button, but not specific padding ( such as "android:paddingLeft" ). Changing my Button to a TextView solved my problem. Thanks. – advice Mar 10 '16 at 12:14
  • Thanks Advice-Dog. Your "android:padding" comment resolved it for me too, but why? – Slartibartfast Jun 07 '18 at 23:23
0

Add this

android:drawablePadding="20dp"
Abhishek
  • 1,337
  • 10
  • 29