Tobias Baumeister
Tobias Baumeister

Reputation: 2147

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?

Upvotes: 7

Views: 15920

Answers (5)

Null Pointer Exception
Null Pointer Exception

Reputation: 1583

  <Button
            android:id="@+id/otherapps"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/btn_background"
            android:text="@string/other_apps"
            android:layout_weight="1"
            android:fontFamily="cursive"
            android:layout_marginBottom="10dp"
            android:drawableLeft="@drawable/ic_more"
            android:paddingLeft="8dp" //for drawable padding to the left
            android:textColor="@android:color/holo_red_light" />

Upvotes: 0

Bhaskar Kumar Singh
Bhaskar Kumar Singh

Reputation: 419

Use this Code:

Drawable image = (Drawable)getResources().getDrawable(R.drawable.onstar_mute_unmute_button_selector);

ImageButton button=(ImageButton)findViewById(R.id.imageButton);

button.setPadding(0,10,5,0);

button.setGravity(Gravity.RIGHT);

button.setBackground(image);

Upvotes: 1

Ratata Tata
Ratata Tata

Reputation: 2879

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.

Upvotes: 2

Ankit Arjaria
Ankit Arjaria

Reputation: 111

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" />

Upvotes: 5

Steveo
Steveo

Reputation: 2268

Use this API call:

public void setCompoundDrawablePadding (int pad)

Upvotes: 1

Related Questions