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

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.

Stuttgart Media University

1 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

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%

0.1 Hint

Follow the wizard instructions and fill in the following information:

0.2 Create a new Android Project in Android Studio

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

center 60%

0.3 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.4 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 Hint

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

center 60%

0.2 Create new virtual device

You should see a empty screen with a "+ Create Virtual Device..." button. Click this button

center 60%

0.3 Choose device template

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

center 60%

0.4 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.2 GB of space on your harddrive.

Click the next button

center 60%

0.5 Confirm Setup

Click the finish button and

center 60%

0.6 Run Emulator

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

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

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

androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

center 80%

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

package de.hdmstuttgart.movietracker

import android.widget.Button
import android.widget.TextView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.instanceOf
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@LargeTest
@RunWith(AndroidJUnit4::class)
class Assignment1Test {

    @get:Rule
    val activityScenarioRule = ActivityScenarioRule(
        MainActivity::class.java
    )

    @Test
    fun assignment1Test() {
        val appCompatButton = onView(allOf(instanceOf(Button::class.java), withId(R.id.button)))
            .check(matches(isDisplayed()))
        appCompatButton.perform(click())
        appCompatButton.perform(click())
        appCompatButton.perform(click())
        appCompatButton.perform(click())
        val textview = onView(allOf(instanceOf(TextView::class.java), withText("Clicked 4 times")))
            .check(matches(isDisplayed()))
        appCompatButton.perform(click())
        val textview2 = onView(allOf(instanceOf(TextView::class.java), 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%