Beginning Android Studio 6. ImageView

Introduction

Hi there, welcome to the sixth tutorial in the Beginning Android studio series. In the last tutorial you learned about RadioGroups and RadioButtons. In this tutorial, I will show you how to add images to your android app in two different ways, directly with XML and programmatically with Java. If you prefer videos, here you go (final product 3:10):

 

The XML way

This app is going to have two images, one declared in XML and the other one in Java. To make it possible to show images, the app obviously need to know where to find those images. Drop your images in the app/res/drawable folder (see 1:10 in the video). I'm wrapping the two ImageViews inside a RelativeLayout, and feeding the RelativeLayout with android:layout_centerInParent="true" to make it look nicer. Something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/hallucindicon"/>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_below="@id/imageView1"/>

    </RelativeLayout>
</RelativeLayout>

Pay attention to the line android:src="@drawable/hallucindicon" in the first ImageView. This is the line that's connecting your image to the ImageView. Just make sure you change hallucindicon to the name of your own image. The second ImageView does not contain this line since we're going to use Java for that one.

 

The Java way

Open up your MainActivity.java. It's very easy to show images with java, all you have to do is reference your ImageView and call the setImageResource function, which takes the location for image as a parameter:

        ImageView imageView2 = findViewById(R.id.imageView2);
        imageView2.setImageResource(R.drawable.your_image);

 

Simple, right? This is my full MainActivity.java:

package com.hallucind.imageview;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

        ImageView imageView2 = findViewById(R.id.imageView2);
        imageView2.setImageResource(R.drawable.hallucindicon);
    }
}

 

Enjoyed this article? Give the teacher an apple.

cookie

0

Author

authors profile photo

Articles with similar tags

Comments