Android button with the ability to draw - 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 registration on the left between the valid and the border of the button. Unfortunately, they are not.
Any help?

+4
source share
5 answers

You must add indents that you can extract by changing the content area of ​​9 slices or changing the selection:

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

If you use a selector, you need to add a fill rule to each of the available states.

+2
source

This will create a text + image button (Image on the left and 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" /> 
+1
source

Use this API call:

 public void setCompoundDrawablePadding (int pad) 
0
source

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); 
0
source
  <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" /> 
0
source

Source: https://habr.com/ru/post/1483645/


All Articles