Friday, August 28, 2015

How to set an image to ImageView in XML in Android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:padding="24dp"
    tools:context=".MainActivity"
    android:background="#F0F8FF">

    <!--
        ImageView
            Displays an arbitrary image, such as an icon.
            The ImageView class can load images from various sources.
    -->

    <!--
        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 in XML layout -->
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/flower6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>