1

I have look for some solution how to change a drawableLeft icon size on a button?, but it's not work for me .

My button layout:

<RelativeLayout
        android:id="@+id/twoButtonLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eceef5"
        android:visibility="visible">

        <Button
            android:id="@+id/goToPersonalPage"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_marginLeft="31dp"
            android:drawableLeft="@drawable/drawable_left_green"
            android:paddingLeft="18dp"
            android:drawablePadding="-10dp"
            android:background="@drawable/button_oval"
            android:text="ICon"
            android:textColor="#29395e"
            android:textSize="18dp" />

        <Button
            android:id="@+id/logOutPersonal"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="30dp"
            android:background="@drawable/button_oval"
            android:text="@string/logOut"
            android:textColor="#29395e"
            android:textSize="18dp" />

    </RelativeLayout>

I adjust the size of button by drawable_left_green.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    // I think it should be more smaller if i set 15sp.
    <item
        android:drawable="@mipmap/ic_launcher"
        android:width="15sp"
        android:height="15sp"
        />

</layer-list >

When i compile the project it (photo2)doesn't look like the preview layout(photo1): enter image description here enter image description here

I can't figure out why it's happen like this , some one can teach me how to fix it ? Any help would be appreciated , thanks in advance.

Morton
  • 5,380
  • 18
  • 63
  • 118

1 Answers1

4

You get the answer from my link but i thought to give you in answer so it will help others.

Setting scaled drawable in code

 Drawable drawable = ContextCompat.getDrawable(YourActivity.this,R.drawable.akshay);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5), 
                     (int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null);
Shashank Verma
  • 284
  • 4
  • 19