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

Android Assignment 02

Introduction to RecyclerView

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

  • Create a new Android Project in Android Studio and use your "assignment2" git folder as the project root folder.
    • Template: "Empty Activity"
    • Name: "MovieTracker"
    • Package name: "de.hdmstuttgart.movietracker"
    • Save location: Path to your assignment2 folder
    • Language: Java
    • Minimum SDK: API 26
  • Add RecyclerView Gradle dependency to your project implementation "androidx.recyclerview:recyclerview:1.2.1"
  • Add a vertical RecyclerView to your MainActivity and give it the following id: "android:id="@+id/homeRecyclerView""
  • Create item views as described
  • When user clicks on a row, remove the row
  • optional: run espresso test to validate your implementation

2 Item Views

Use this android ids for each row:

  • title: @+id/title
  • year: @+id/year
  • actor: @+id/actor

Example Layout:


3 Row 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 android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.matcher.BoundedMatcher;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.core.internal.deps.guava.base.Preconditions.checkNotNull;
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

public class Assignment2Test {

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

    public void mainActivityTest() {

                .check(matches(atPosition(0, hasDescendant(withText("Dr. No")))));

                .check(matches(atPosition(1, hasDescendant(withText("From Russia with Love")))));

                .check(matches(atPosition(2, hasDescendant(withText("Goldfinger")))));

                .check(matches(atPosition(3, hasDescendant(withText("Thunderball")))));

                .check(matches(atPosition(4, hasDescendant(withText("You Only Live Twice")))));

                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

                .check(matches(atPosition(0, hasDescendant(withText("From Russia with Love")))));

                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

                .check(matches(atPosition(0, hasDescendant(withText("Goldfinger")))));

    public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) {
        return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
            public void describeTo(Description description) {
                description.appendText("has item at position " + position + ": ");

            protected boolean matchesSafely(final RecyclerView view) {
                RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position);
                if (viewHolder == null) {
                    // has no item on such position
                    return false;
                return itemMatcher.matches(viewHolder.itemView);

5 Gradle Dependencies

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation ''
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation "androidx.recyclerview:recyclerview:1.2.1"
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'