How can i use espresso to click an item from a list view based on item’ text?


I’m trying to click a list view item using espresso but no success.

I tried everything that’s on stackOverflow

                    check(matches(withText("Log in")))

            onView(allOf(is(instanceOf(MenuListAdapter.class)), hasSibling(withText("Log in")))).perform(click());

            onData(allOf(is(instanceOf(MenuListAdapter.class)), hasEntry(equalTo("STR"), is("Log in"))))

            onData(anything()).inAdapterView(withContentDescription("Log in")).atPosition(0).perform(click());


            onData(hasEntry(equalTo(MenuListAdapter.class),is("Log out")))
            onView(withId("Log in")));

I also tried to create a custom matcher
    public static Matcher<Object> withItemValue(final String value) {
        return new BoundedMatcher<Object, ExtraMenu>(ExtraMenu.class) {
            protected boolean matchesSafely(ExtraMenu item) {
                return item.getText().toUpperCase().equals(String.valueOf(value));


            public void describeTo(Description description) {
                description.appendText("has value " + value);

used as:


Most of the times i get this error. There s a menu with 5 items Caused by: java.lang.RuntimeException: No data found matching: has value Promotions contained values: <[Data: 0 (class: java.lang.Integer) token: 0, Data: 1 (class: java.lang.Integer) token: 1, Data: 2 (class: java.lang.Integer) token: 2, Data: 3 (class: java.lang.Integer) token: 3, Data: 4 (class: java.lang.Integer) token: 4]>


I found a way, i’ll post it in order to help others. I had to change the list view to a recycle view and used this command.

                        hasDescendant(withText("Your string")), click()));

You might want to use Thread.sleep(2000) if you have multiple actions before this line of code. Also you need this dependency in your build.gradle:

androidTestImplementation("") {
    exclude group: '', module: 'appcompat'
    exclude group: '', module: 'support-v4'
    exclude group: '', module: 'support-v7'
    exclude group: '', module: 'design'
    exclude module: 'support-annotations'
    exclude module: 'recyclerview-v7'

Answered By – Panduru Ionut

Leave a Comment