user4138608
user4138608

Reputation:

Image inside ImageButton not scaling to fill button

My layout includes this ImageButton as shown:

You can see in this picture

But as you can see, the image is not taking the full size of the ImageButton.

This is the layout file where the ImageButton is defined:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/dashboard_profile"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_marginTop="100dp"
    android:id="@+id/imageButton3"
    android:scaleType="fitXY"/>

As you can see, I already added android:scaleType="fitXY", but it's not working.

I also tried to change the button size in Android Studio's design mode, but it's not letting me change.

How can I solve these problems?

Upvotes: 0

Views: 1151

Answers (3)

amalBit
amalBit

Reputation: 12191

Unlike Imageview, Imagebutton has a padding around the image area. This is intentional. This is because of the default button style. You can do one of the following:

  • Use a borderless button style as your background.
    style="?android:attr/borderlessButtonStyle"

  • Set your background as null.
    android:background="@null"

Upvotes: 3

Rahul Pareta
Rahul Pareta

Reputation: 796

You can use this code

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/dashboard_profile"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_marginTop="100dp"
    android:id="@+id/imageButton3"
    android:scaleType="fitXY"/>

Upvotes: 1

Sergei Bubenshchikov
Sergei Bubenshchikov

Reputation: 5371

Try to use android:src instead of app:srcCompat

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:src="@mipmap/dashboard_profile"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_marginTop="100dp"
    android:id="@+id/imageButton3"
    android:scaleType="fitXY"/>

Upvotes: 0

Related Questions