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

Tomik
  • 23,857
  • 8
  • 121
  • 100
eng.ahmed
  • 905
  • 4
  • 16
  • 38

8 Answers8

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" />
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
zrgiu
  • 6,200
  • 1
  • 33
  • 35
  • 3
    for those doing it programatically, use setbackgrounddrawable or setbackground instead of setimageresource – Adam Johns Jul 09 '13 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 01 '21 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..

Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60
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

cesargastonec
  • 430
  • 7
  • 15
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" />
live-love
  • 48,840
  • 22
  • 240
  • 204
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 .

Payal
  • 903
  • 1
  • 9
  • 28
1

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

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

Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
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.

musterjunk
  • 341
  • 3
  • 15
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.

Peter
  • 11
  • 2