Set/Change width and height of ImageButton image in android using XML

How to resize/scale image button image with layout_width,layout_height with  android:scaleType=”fitCenter” .

In this tutorial we are creating an image button using activity_main.xml layout file now after changing the height and width of image button it will make the crop the image button and image button looks like cut from all over the corners. But with the use of android:scaleType=”fitCenter” attribute it will set the image scale into center and image dose not automatically crops from corners. So here is the complete step by step tutorial for Set/Change width and height of ImageButton image in android using XML.

android-project-download-code-button

How to Set/Change width and height of ImageButton image in android using XML.

Code for MainActivity.java file.

 package com.setwidthheightimagebuttonimage_android_examples.com;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.setwidthheightimagebuttonimage_android_examples.com.MainActivity" >

 <ImageButton
 android:id="@+id/imageButton1"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:scaleType="fitCenter"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:src="@drawable/demo_img" />

</RelativeLayout>

Screenshot:

Set/Change width and height of ImageButton image in android using XML

Click here to download Set/Change width and height of ImageButton image in android using XML project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *