Home » Android » android – How can I scale the background of a button?

android – How can I scale the background of a button?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have the following button:

<Button
android:id="@+id/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/back_no_save"
android:text="OK" />

Its appearence is:

enter image description here

How can I scale down my drawable and show a small arrow?

How to&Answers:

You could use an ImageButton and try android:scaleType

ImageView.ScaleType

Answer:

I don’t hink you can from xml. There’s no xml attribute that can do scaling for buttons. You can do it programatically though, in the following way :

Bitmap originalBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap scaledBitmap=Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, true);
txt.setBackgroundDrawable(new BitmapDrawable(bit));

Answer:

You could scale down the actual graphic that the drawable points to.

Answer:

If you don’t want to deal with Bitmap.createScaledBitmap then it seems to me that the best way to do it is to create your own component.
You can extend RelativeLayout and put ImageView into it, for example.

Answer:

I know this is an old post, but I did something like this:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:padding="16dp"
    android:id="@+id/XXXXXXXXXXXX"
    android:orientation="horizontal">
    <ImageView
        android:src="@drawable/XXXXXXXXXXXX"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="centerInside"
        android:id="@+id/XXXXXXXXXXXXXXXX"/>
    <TextView
        android:id="@+id/XXXXXXXXXXXXXX"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="XXXXXXX"
        android:layout_gravity="center"/>
</LinearLayout>