Friday, August 28, 2015

How to set an image to ImageView in XML 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="#aac4a4"
    >
    <!--
        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="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android_image"
        android:padding="5dp"
        android:layout_margin="10dp"
        />
</RelativeLayout>
More android examples