Shivaraj Patil Shivaraj Patil - 21 days ago 5
Android Question

Espresso how to click on a ImageView placed in listview's first item?

I have a custom layout for listview, I want to click on a imageview placed in the list view item, overflow icon of the first item in the list view.

I want to click on last imageview with id "rcOverflow"

My layout,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="10dp"
android:paddingTop="5dp">

<ImageView
android:id="@+id/rcCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentTop="true" />

<TextView
android:id="@+id/rcTime"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="right"
android:maxLines="1"
android:textColor="#ffffff"
android:textSize="13sp" />

<RelativeLayout
android:id="@+id/rsBubble"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_below="@id/recordsMonthHeader"
android:paddingBottom="16dp"
android:paddingTop="16dp">

<ImageView
android:id="@+id/rcImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:visibility="gone"
android:layout_marginLeft="3dp"
android:layout_marginRight="4dp"/>

<TextView
android:id="@+id/rcFilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:textColor="#000000"
android:maxLines="2"
android:ellipsize="end"
android:textSize="15sp" />

<TextView
android:id="@+id/rcDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/recordsFilename"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:textColor="#a09f9f" />

<ImageView
android:id="@+id/records_location_image"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_alignBottom="@id/recordsDuration"
android:layout_marginBottom="4dp" />

<TextView
android:id="@+id/rcLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/recordsDuration"
android:layout_marginBottom="1dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:ellipsize="end"
android:lines="1"
android:textColor="#a09f9f"
android:textSize="12sp"
android:visibility="gone" />

<TextView
android:id="@+id/rcTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/recordsDuration"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:textColor="#a09f9f"
android:textSize="12sp"
android:visibility="gone" />

<ImageView
android:id="@+id/rcOverflow"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_below="@+id/recordsImage"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="true"
android:background="?android:attr/selectableItemBackground"
android:src="@drawable/overflow" />

</RelativeLayout>




what I tried

onData(anything()).inAdapterView(withId(getId("recorderlistpage_listView")))
.perform(click());


or

onData(anything()).atPosition(0).inAdapterView(withId(getId("rcOverflow")))
.perform(click());


both not working

Answer

This worked for me

onData(allOf()).atPosition(0).
onChildView(withId(R.id.rcOverflow)).
perform(click());