Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Button with Drawable - Padding

I want to add a Drawable Image to my Button on the left and tried the following:

button = new Button(this);
button.setWidth(screen_dimens(0.25, "w")); //25% of screen width
button.setPadding(5, 5, 0, 5);
Drawable img = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);  

But I want to have a padding on the left between the drawable and the border of the button. Unfortunately, there is none.
Any help?

like image 806
Tobias Baumeister Avatar asked May 30 '13 18:05

Tobias Baumeister


3 Answers

This will generate text + image button ( Image on Left & Text After That )

   <Button
   android:gravity="center_vertical"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"       
   android:id="@+id/button2"
   android:paddingLeft="20dp"
   style="@style/whiteText"
   android:onClick="openWhyAshoka"
   android:drawableLeft="@drawable/arrow"
   android:drawablePadding="50dp"
   android:text="Ankit" />
like image 69
Ankit Arjaria Avatar answered Sep 20 '22 22:09

Ankit Arjaria


You have to add the padding to you drawable, by changing the 9 slices content area or by changing your drawable padding:

<padding
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp" />

If you are using selector you have to add the padding rule to each one of the drawable states.

like image 24
Ratata Tata Avatar answered Sep 20 '22 22:09

Ratata Tata


Use this API call:

public void setCompoundDrawablePadding (int pad)
like image 27
Steveo Avatar answered Sep 21 '22 22:09

Steveo