title: Espresso: How do I scroll to an item in a HorizontalScrollView by index?
body: <p>This is what my <code>HorizontalScrollView</code> looks like:</p>
  
  <pre><code>&lt;HorizontalScrollView
      android:layout_below="@id/saved_circuits_title"
      android:id="@+id/saved_circuits_scrollview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      &gt;
      &lt;LinearLayout
          android:id="@+id/saved_circuits_scroll"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:orientation="horizontal"&gt;
      &lt;/LinearLayout&gt;
  &lt;/HorizontalScrollView&gt;
  </code></pre>
  
  <p>In my <code>HomeActivity</code> I have the following relevant code that populates the <code>HorizontalScrollView</code> with bitmaps</p>
  
  <pre><code>onCreate{
  ...
  this.savedCircuitsScroll = (LinearLayout) findViewById(R.id.saved_circuits_scroll);
  ...
  }
  
  updateSavedCircuits(){
  ...// code to make an ImageView from a retrieved bitmap
  newImageImage.setOnClickListener(this.thumbnailListener);
  this.savedCircuitsScroll.addView(newImageImage);
  ...
  }
  </code></pre>
  
  <p>How do I use Espresso to scroll to an ImageView at a specified index in my HorizontalScrollView and click it?</p>
  
  <hr>
  
  <h2>What I've tried</h2>
  
  <p>I do not have ID's in my layout xml so an approach such as <a href="https://stackoverflow.com/questions/25410613/android-espresso-scrolling-to-a-non-list-view-item">this</a> does not work:</p>
  
  <pre><code>onView( withId( R.id.button)).perform( scrollTo(), click());
  </code></pre>
  
  <p>I know you can <a href="https://stackoverflow.com/questions/27396583/how-to-click-on-an-item-inside-a-recyclerview-in-espresso">click on an item by index in a RecyclerView</a> and tried finding an analogous approach for <code>HorizontalScrollViews</code>:</p>
  
  <pre><code>    onView(withId(R.id.saved_circuits_scroll))
              .perform(HorizontalScrollViewActions.actionOnItemAtPosition(0, click()));
  </code></pre>
  
  <p>Except <code>HorizontalScrollViewActions</code> does not exist. </p>
  
  <p>or following <a href="https://spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews/" rel="nofollow noreferrer">this blog</a> I tried the following to at least click an item in the <code>HorizontalScrollView</code> at a specified index:</p>
  
  <pre><code>// Click item at position 3
  onView(withHorizontalScrollView(R.id.scroll_view).atPosition(3)).perform(click());
  // Convenience helper
  public static HorizontalScrollViewMatcher withHorizontalScrollView(final int horizontalScrollViewId) {
      return new HorizontalScrollViewMatcher(horizontalScrollId);
  }
  </code></pre>
  
  <p>except <code>HorizontalScrollViewMatcher</code> does not exist.</p>
  
  <p>What do we do for <code>HorizontalScrollView</code>? It isn't a descendant of <code>ScrollView</code> so the answer <a href="https://stackoverflow.com/a/25417769/2734863">here</a> suggests I need to implement my own custom <code>ViewAction</code>. All I want to do is scroll to an item in a <code>HorizontalScrollView</code> by index and click it. Is this really needed? If this is what I need to do, how do I go about implementing this custom <code>ViewAction</code>?</p>
  
  <hr>