5COSC005W - Tutorial 3 Exercises

As part of this tutorial for this week, you should complete ALL the tasks described in the following links and specifications: (make sure that you ask questions to your tutor for anything that you do not understand or if you are stuck at any point):

  1. Displaying Drawables
    As you have seen previously when we would like to display drawable resources like images we could use the @drawable/resource in XML. Run the code with the following layout and activity by creating a new Android project and replace its layout and activity code with the one given below. You should download the following image (or use your own favourite image) rename it to brittany_02625.jpg and place it in the drawable directory of your project.

Brittany_02625.jpg

<?xml version="1.0" encoding="utf-8"?>
<!-- This is the layout of the Activity -->
<LinearLayout 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="uk.ac.westminster.drawableexample.MainActivity"
    android:orientation="vertical">

    <ImageView
	android:id="@+id/im_view"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:src="@drawable/brittany_02625"/>
</LinearLayout>
package uk.ac.westminster.drawableexample;

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 v1 = findViewById(R.id.im_view);
    }
}

Similarly with other resources, drawable resources can be used to update widgets dynamically during run-time (Java code) depending on user choices.

Delete the android:src="@drawable/brittany_02625" line from your XML layout file and modify the Activity's Java code so that the onCreate() method looks like the following:

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

    ImageView v1 = findViewById(R.id.im_view);
    v1.setImageResource(R.drawable.brittany_02625);
}

The resource id can also be retrieved programmatically, if you know the name of the actual resource, e.g. the image filename and use it without its extension. Modify the onCreate() method to look as the following:

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

    ImageView v1 = findViewById(R.id.im_view);

    String resource = "brittany_02625";
    int resource_id = getResources().getIdentifier(resource, "drawable", "uk.ac.westminster.drawableexample");
    v1.setImageResource(resource_id);
}

The getIdentifier() method accepts 3 arguments: the first is the resource name, the second the type of the resource (it is a drawable) and the third is the package name.

Challenge: Modify the program above so as to add a button which every time it is clicked alternates and displays one of 2 different images (e.g. as a second image you could use Welsh_springer_spaniel_08203.jpg or one of your favourite images.)

  1. Activities and intents
  2. Activity lifecycle and state

The material for tasks 2 and 3 was created by Google, used under a Creative Commons Attribution 4.0 International license.