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

Android Assignment 03

Introduction to Navigation

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.

1 Tasks

  • Reuse your assignment2 code and copy it to assignment3 folder.
  • Goal is to use NavHostController to navigate between different @Composable.
  • In the home screen the movie list should be empty on app start.
  • Add Button to MainActivity above the list with the testTag "openSearchButton"
  • When the button is pressed, open new screen showing the search
  • Add the following elements to the search screen
    • TextField with the testTag "textField"
    • Button with the testTag "searchButton"
    • LazyColum containing the movie items
  • Reuse the model from Assignment2
    • When movie title is entered in TextField and button "searchButton" is clicked, show search results in the LazyColum. Use "contains" to look for movie title. Example: Search string is "uss", show only "From Russia with Love"
  • When list item in search is clicked, save the movie item, close the search screen and show all saved movies in the home screen. First clicked results are on top of list.
  • In the home screen, when the movie item is clicked, remove the movie item from saved ones

2 Views

2.1 Home Screen

On app start:

With single saved movie:

With multiple saved movies:

2.2 Search Screen

On activity start:

With search result:

3 Movie Data

Use this data to fill out your rows:

title year actor
Dr. No 1962 Sean Connery
From Russia with Love 1963 Sean Connery
Goldfinger 1964 Sean Connery
Thunderball 1965 Sean Connery
You Only Live Twice 1967 Sean Connery

4 Espresso Test

package de.hdmstuttgart.movietracker

import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import org.junit.Rule
import org.junit.Test

class Assignment3Test {

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

  @Test
  fun myTest() {
    composeTestRule.onNodeWithTag("openSearchButton").performClick()
    composeTestRule.onNodeWithTag("textField").performTextInput("Gold")
    composeTestRule.onNodeWithTag("searchButton").performClick()
    composeTestRule.onAllNodesWithTag("movieItem")[0].assertTextContains("Goldfinger")
    composeTestRule.onAllNodesWithTag("movieItem")[0].performClick()
    composeTestRule.onAllNodesWithTag("movieItem")[0].assertTextContains("Goldfinger")
    composeTestRule.onNodeWithTag("openSearchButton").performClick()
    composeTestRule.onNodeWithTag("textField").performTextInput("From Russia with Love")
    composeTestRule.onNodeWithTag("searchButton").performClick()
    composeTestRule.onAllNodesWithTag("movieItem")[0].performClick()
    composeTestRule.onAllNodesWithTag("movieItem")[1].assertTextContains("From Russia with Love")
  }
}

5 Gradle Dependencies

Module build.gradle.kts:

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.navigation.compose)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}