android – Left/right padding for Button.setCompoundDrawablesWithIntrinsicBounds()?-ThrowExceptions

Exception or error:

I’m trying to set a left icon on a button with:

setCompoundDrawablesWithIntrinsicBounds(R.drawable.foo, 0, 0, 0);

but the icon is placed flush up against the left edge of my button, and the text string. Is there a way to specify some left/right padding on the supplied icon so that it isn’t right up against the edges?

Thanks

How to solve:

I believe what you’re looking for is android:drawablePadding

Here’s an example of using drawablePadding along with paddingLeft and paddingRight to position an image in a button

<Button
    android:id="@+id/button"
    android:layout_width="200dp"
    android:layout_height="80dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="2dip"
    android:paddingLeft="30dip"
    android:paddingRight="26dip"
    android:text="Test" />

Image example

###

For anyone looking for the programtic way, here is an example:

menuButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.foo, 0, 0, 0);
menuButton.setCompoundDrawablePadding(10);

Leave a Reply

Your email address will not be published. Required fields are marked *