Open menu with table of contents Android Assignment 01
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Android Development

Android Assignment 01

Introduction to Android Studio and write first app

Stuttgart Media University

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.

1 Tasks

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

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%

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

center 60%

0.1 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.

Build configuration language: Kotlin DSL [Recommended]

0.2 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.

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.

0.1 Create new virtual device

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

You should see a empty screen with a "Add a new device..." button. Click this button

center 60%

0.2 Choose device template

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

center 60%

0.3 Choose Android Version

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

Click the next button

center 60%

0.4 Confirm Setup

Click the finish button and

center 60%

0.5 Run Emulator

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

0.6 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%

3. Create an UI with a Text and a Button

Edit your MainActivity class and add a new @Composable containing a Column which has a Text and a Button. Please mind to add Modifier.testTag("button") to the Button. This is needed for the UI test later.

The UI should then look like in the following screen capture:

center 25%

Modify the onClick action of the Button. When the button is clicked the text in the Text 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 Text with the count of the clicks "Clicks: 5" from main activity.

center 25%

0.1 Checklist

  • When the button is touched, the text changes correspondingly.
  • When user clicked 5 times on button, open a new activity and show the value of the counting variable.

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

Module build.gradle.kts:

androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(libs.androidx.junit)

center 80%

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

0.1 Hint

Check out the official Android UI testing cheat sheet

package de.hdmstuttgart.movietracker

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import org.junit.Rule
import org.junit.Test

class Assignment1Test {

    @get:Rule
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    @Test
    fun myTest() {
        composeTestRule.onNodeWithTag("button").assertIsDisplayed()
        composeTestRule.onNodeWithTag("button").performClick()
        composeTestRule.onNodeWithTag("button").performClick()
        composeTestRule.onNodeWithTag("button").performClick()
        composeTestRule.onNodeWithTag("button").performClick()
        composeTestRule.onNodeWithText("Clicked 4 times").assertIsDisplayed()
        composeTestRule.onNodeWithTag("button").performClick()
        composeTestRule.onNodeWithText("Clicks: 5").assertIsDisplayed()
    }
}

Click on the green double arrow next to class Assignment1Test in Android Studio and select "Run Assignment1Test"

center 30%

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

center 80%