Friday, August 28, 2015

How to set an image to ImageView in Android

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8dcda"
    android:padding="24dp"
    tools:context=".MainActivity">

    <!-- Set ImageView image by XML-->
    <ImageView
        android:id="@+id/ivXML"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:src="@drawable/flower"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Set ImageView image programmatically-->
    <ImageView
        android:id="@+id/ivResource"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivXML" />

    <!-- Set ImageView image as drawable programmatically-->
    <ImageView
        android:id="@+id/ivDrawable"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivResource" />

    <!-- Set ImageView image as bitmap programmatically-->
    <ImageView
        android:id="@+id/ivBitmap"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivDrawable" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java

package com.cfsuman.androidtutorials;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.core.content.ContextCompat;


public class MainActivity extends Activity {

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

                // Get the context
                MainActivity context = this;

                // Get the widgets reference from XML layout
                ImageView ivResource = findViewById(R.id.ivResource);
                ImageView ivDrawable = findViewById(R.id.ivDrawable);
                ImageView ivBitmap = findViewById(R.id.ivBitmap);

                // Set image from resource
                ivResource.setImageResource(R.drawable.flower2);

                // Set image as drawable for third ImageView
                ivDrawable.setImageDrawable(
                        ContextCompat.getDrawable(
                                context,
                                R.drawable.flower3
                        )
                );

                Bitmap bitmap = BitmapFactory.decodeResource(
                        getResources(),R.drawable.flower4);

                // Set image as bitmap for fourth ImageView
                ivBitmap.setImageBitmap(bitmap);
        }
}