How can I scale the background of a button?

14,993

Solution 1

You could use an ImageButton and try android:scaleType

ImageView.ScaleType

Solution 2

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));

Solution 3

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

Solution 4

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.

Share:
14,993
Bobs
Author by

Bobs

You can contact me using this: breceivemail yahoo dot com می توانید با من تماس بگیرید: breceivemail yahoo dot com #SOreadytohelp

Updated on June 09, 2022

Comments

  • Bobs
    Bobs almost 2 years

    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?

  • Bobs
    Bobs over 12 years
    I need the "OK" text so I can not use ImageButton.
  • zienkikk
    zienkikk over 12 years
    How about using a ScaleDrawable?