11

I want to resize my ImageButton using mouse on graphic layout or using code by android:layout_width , android:layout_height. But whenever I do this, the ImageButton isn't resized, in fact and it's not smaller but it is cut at the edges.

thanks in advance

0

8 Answers 8

36

try setting android:background instead of android:src for setting the image on the button. That might help in your case, since it will stretch the image to your button's size. Also, you will have to specify fixed dimensions for the buttons (use dip instead of px). Example:

<ImageButton
     android:background="@drawable/ic_menu_more"
     android:layout_width="50dip"
     android:layout_height="50dip" />
2
  • 3
    for those doing it programatically, use setbackgrounddrawable or setbackground instead of setimageresource
    – Adam Johns
    Jul 9, 2013 at 15:35
  • I am having problem with changing icon color now: android:tint="#D50000" not working while android:backgroundTint="#D50000" isn't available below v21
    – fWd82
    Jul 1, 2021 at 12:21
21

First try to use android:background instead of android:src for setting the image on the button.In most cases,it helps.If not,then see these..

0
5

From steven-byle's solution (https://stackoverflow.com/a/15117536/1741997), he says:

"...Use an android:scaleType="fitCenter" to have Android scale the images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling..."

It works for me

4
    <ImageButton
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@drawable/ic_star_border"
    />
or
       <ImageButton
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="?selectableItemBackgroundBorderless"
            android:scaleType="fitXY"
            android:src="@drawable/ic_delete" />
1

You can use NinePatchDrawable... A resizeable bitmap, with stretchable areas that you define.

http://developer.android.com/reference/android/graphics/drawable/NinePatchDrawable.html

1
  • 1
    I don't understand. I'm trying to use a system image for my button.
    – Roger
    Feb 26, 2011 at 19:02
1

It might be the problem with size of your image..Try to use resized image then use it in your image button.Then you can set the width and height of the button .

0

The way I solved this problem is to create a bitmap from the drawing resource in question then transform it into a scaled bitmap with the desired size. I also set the background to transparent to only show the image.

    int height = 300;
    int width = 500
    Bitmap bmp;
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.myresourcename);
    bmp = Bitmap.createScaledBitmap(bmp, width, height, true);
    ImageButton imageButton = new ImageButton(this.getActivity());
    imageButton.setImageBitmap(bmp);
    imageButton.setBackgroundColor(Color.TRANSPARENT);

Then add it to a layout.

0

Applying cesargastonec's answer will fit the image in your ImageButton without much room to spare. If you are using a png, you can move the image to a bigger background to get an even smaller image.

For example, moving a 48x48 image in a 256x256 background to a 512x512 background will half its size in an ImageButton with android:scaleType="fitCenter".

This approach is best applied to an image you intend to only use in ImageButtons as it may be awkwardly scaled elsewhere and have a deceptively large clickbox.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.