meaholik
meaholik

Reputation: 440

Button's padding is not working without background

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!

Upvotes: 10

Views: 9380

Answers (3)

Nick
Nick

Reputation: 456

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

Upvotes: 28

A.R.
A.R.

Reputation: 2641

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.

Upvotes: 2

Abhishek
Abhishek

Reputation: 1345

Add this

android:drawablePadding="20dp"

Upvotes: 0

Related Questions