activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
tools:context=".MainActivity"
android:background="#F8F8FF">
<!-- ImageButton with default size, no scaling -->
<ImageButton
android:id="@+id/ibDefault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:src="@drawable/share_solid"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvDefault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No scaling"
app:layout_constraintStart_toStartOf="@+id/ibDefault"
app:layout_constraintTop_toBottomOf="@id/ibDefault" />
<!-- ImageButton scale type fit center by XML -->
<ImageButton
android:id="@+id/ibFitCenter"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:scaleType="fitCenter"
android:src="@drawable/share_solid"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDefault" />
<TextView
android:id="@+id/tvFitCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale Type : fitCenter"
app:layout_constraintStart_toStartOf="@+id/ibFitCenter"
app:layout_constraintTop_toBottomOf="@id/ibFitCenter" />
<!-- ImageButton scale type fit x y by XML -->
<ImageButton
android:id="@+id/ibFitXY"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:scaleType="fitXY"
android:src="@drawable/share_solid"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvFitCenter" />
<TextView
android:id="@+id/tvFitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale Type : fitXY"
app:layout_constraintStart_toStartOf="@+id/ibFitXY"
app:layout_constraintTop_toBottomOf="@id/ibFitXY" />
<!-- ImageButton scaling programmatically in java code -->
<ImageButton
android:id="@+id/ibCenterCrop"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:src="@drawable/share_solid"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvFitXY" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale Type programmatically\nCENTER_CROP"
app:layout_constraintStart_toStartOf="@+id/ibCenterCrop"
app:layout_constraintTop_toBottomOf="@id/ibCenterCrop" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.cfsuman.androidtutorials;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the widgets reference from XML layout
ImageButton ibCenterCrop = findViewById(R.id.ibCenterCrop);
// Set the ImageButton image scale type for fourth ImageButton
ibCenterCrop.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}

- How to add a border to an ImageButton in Android
- How to resize/scale an ImageButton in Android
- How to add padding to an ImageButton in Android
- How to set an ImageButton background transparent in Android
- How to add listener for a ToggleButton in Android
- How to change ToggleButton on/off text in Android
- Android Switch Button Example
- How to use TextView to display GridView item text in Android
- How to remove underline from EditText in Android
- How to set EditText border color programmatically in Android
- How to create a transparent EditText in Android
- How to add placeholder to an EditText in Android
- How to change EditText hint color in Android
- How to clear EditText in Android
- How to change EditText cursor color in Android