user4514473 user4514473 - 3 months ago 59
Android Question

How to scale drawable inside button so that the button size is not controlled by the drawable?

In the screenshot below I'm using the same icon for both the button and the imageview but what I can't seem to control is the scaling of the icon inside the button, I can make the icon smaller by reducing the resolution and make the button smaller but that makes the icon pixelated. Is there any way to have high resolution icons with buttons and let the button control the overall size?

enter image description here

XML for the button:

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:id="@+id/em_btn"
android:layout_gravity="center_horizontal"
android:backgroundTint="#ffffff"
android:drawableLeft="@drawable/email2"/>

Answer
Drawable emailImg = getResources().getDrawable(R.drawable.email2);
        drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.1),
                (int)(drawable.getIntrinsicHeight()*0.1));
        ScaleDrawable scaledImg = new ScaleDrawable(emailImg, 0, 10, 10);
        Button button = (Button)findViewById(R.id.em_btn);
        button.setCompoundDrawables(scaledImg.getDrawable(), null, null, null);

Try this