1/8

Android Assignment 01

Introduction to Android Studio and write first app

Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Android developer website. Please do not hesitate to ask me or the tutor if you have any questions. Under „Tip“ you can find some hints for the task. The „Checklist“ specifies what should happen when your implementation is correct.

2/8

Tasks

  1. Create a new Android Project in Android Studio
  2. Create an AVD
  3. Create an UI with a TextView and a Button
  4. Debug your app
3/8

1. Create a new Android Project in Android Studio

Start Android Studio on MacOS, Linux or Windows and use the wizard to create a new Android project. Follow the wizard instructions and fill in the following information:

center 60%

Hint

Follow the wizard instructions and fill in the following information:

4/8

Create a new Android Project in Android Studio

For your first app choose the „Empty Activity“, as you do not need the features included with the other activity templates.

center 60%

5/8

Project configuration

Fill out the fields as followed

center 60%

Name: MovieTracker

This is also the Application Name appearing in the App list on the device later on.

Package name: de.hdmstuttgart.movietracker

This will be used to generate the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated. Your package name must be unique across all packages installed on the Android system; for this reason, it’s important to use a standard domain-style package for your applications. The example above uses the „de.hdmstuttgart” namespace ― when you develop your own applications, you should use a namespace that’s appropriate to your organization or entity. This is the reason why in the Android Studio Wizard you can only change the company domain and the package name is generated.

Language: Java

Minimum SDK

This value specifies the minimum API Level required by your application. It is a number referring to a version of the Android firmware the user needs to run your application. If you choose the latest version your app will only run on devices with the latest version which usually are not too many. For this and the following assignments please select „API 26: Android 8.0 (Oreo)“.

After clicking on “Finish” your app project will be generated.

6/8

2. Create an AVD

Use the Android Virtual Device (AVD) Manager in Android Studio to create a virtual Device with the latest available Android Version.

Hint

You can find the AVD Manager in the top toolbar in Android Studio.

center 60%

Create new virtual device

You should see a empty screen with a “+ Create Virtual Device…” button. Click this button

center 60%

Choose device template

For our assignments, we will use the Pixel 4 template.

center 60%

Choose Android Version

For our assignments, we will use the Android R / 30 Eventually you have to download Android R and confirm installation before being able to select it. This will take around 1.1 GB of space on your harddrive.

Click the next button center 60%

Confirm Setup

Click the finish button and

center 60%

Run Emulator

After finish, you should see the setup device in your list. Click the start button to start the emulator.

Hint

You can setup multiple devices with different templates run several emulators at the same time. This will take some ressources of your host computer

center 60%

7/8

3. Create an UI with a TextView and a Button

Edit your „activity_main“ layout file and create a layout containing a TextView and a button (similar to the one shown below). You can reuse the “Hello World!”” TextView.

Give your view the following ids

This ids are important for the UI test to run correctly.

TextView: textView
Button: button

center 25%

Add an „OnClickListener“ to the button. When the button is clicked the text in the TextView should change to „Clicked X times“. Where X is the number of times the Button was touched.

center 25%

When click counter reaches 5 clicks, a new activity should open. It shows a TextView with the count of the clicks “clicks: 5” from main activity.

Give the TextView the id: resultTextView

center 25%

Checklist

8/8

4. Run UI Test

In order to ensure you set up all requirements correctly, run this Android Espresso UI test on your project. Therefore check if the correct Gradle dependencies are set in your app modules build.gradle

androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

center 80%

Copy this class to your project folder “app/src/androidTest/de/hdmstuttgart/movietracker” And name the class accordingly “Assignment1Test.java”

package de.hdmstuttgart.movietracker;


import android.widget.Button;
import android.widget.TextView;

import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class Assignment1Test {

	@Rule
	public ActivityScenarioRule<MainActivity> activityScenarioRule
			= new ActivityScenarioRule<>(MainActivity.class);

	@Test
	public void assignment1Test() {
		ViewInteraction appCompatButton = onView(allOf(instanceOf(Button.class), withId(R.id.button)))
				.check(matches(isDisplayed()));

		appCompatButton.perform(click());
		appCompatButton.perform(click());
		appCompatButton.perform(click());
		appCompatButton.perform(click());

		ViewInteraction textview = onView(allOf(instanceOf(TextView.class), withText("Clicked 4 times")))
				.check(matches(isDisplayed()));

		appCompatButton.perform(click());

		ViewInteraction textview2 = onView(allOf(instanceOf(TextView.class), withText("clicks: 5")))
				.check(matches(isDisplayed()));

	}
}

Right click on the class in Android Studio and select “Run Assignment1Test”

center 30%

An test run window at the bottom of Android Studio should appear and show you the test passed.

center 80%