Friday, August 28, 2015

How to set an image to ImageView in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#d8dcda"
    >
    <!--
        android:src
            Sets a drawable as the content of this ImageView.

            May be a reference to another resource, in the form "@[+][package:]type:name"
            or to a theme attribute in the form "?[package:][type:]name".

            May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

            This corresponds to the global attribute resource symbol src.
    -->
    <!-- Set ImageView image by XML-->
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android_image"
        android:padding="3dp"
        />
    <!-- Set ImageView image programmatically-->
    <ImageView
        android:id="@+id/iv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv"
        android:padding="3dp"
        />
    <!-- Set ImageView image as drawable programmatically-->
    <ImageView
        android:id="@+id/iv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv2"
        android:padding="3dp"
        />
    <!-- Set ImageView image as bitmap programmatically-->
    <ImageView
        android:id="@+id/iv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv3"
        android:padding="3dp"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


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
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        ImageView iv = (ImageView) findViewById(R.id.iv);
        ImageView iv2 = (ImageView) findViewById(R.id.iv2);
        ImageView iv3 = (ImageView) findViewById(R.id.iv3);
        ImageView iv4 = (ImageView) findViewById(R.id.iv4);

        /*
            setImageResource (int resId)
                Sets a drawable as the content of this ImageView.

                Parameters
                    resId : the resource identifier of the drawable

                This does Bitmap reading and decoding on the UI thread, which can cause
                a latency hiccup. If that's a concern, consider using
                setImageDrawable(android.graphics.drawable.Drawable) or
                setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.
         */
        // Set an image for second ImageView
        iv2.setImageResource(R.drawable.android_image2);

        // Set image as drawable for third ImageView
        iv3.setImageDrawable(getDrawable(R.drawable.android_image3));

        /*
             Bitmap decodeResource (Resources res, int id)
                Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options) with null Options.

                Parameters
                    res : The resources object containing the image data
                    id : The resource id of the image data

                Returns
                    The decoded bitmap, or null if the image could not be decoded.
         */
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.android_image4);

        // Set image as bitmap for fourth ImageView
        iv4.setImageBitmap(bitmap);
   }
}
More android examples