Monday, August 24, 2015

How to use ImageButton different image ScaleType in Android

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);
        }
}