scrollTo doesn’t work when it is behind a view with alpha

Issue

I can’t make scrollTo to work when the EditText is behind a LinearLayout with alpha background.

I wanted to avoid uiautomator cause I think it doesn’t work sometimes

<RelativeLayout ...>
<ScrollView>
 <LinearLayout>
   <EditText />
   <EditText />
   <EditText />
   <EditText />
   ....
   <EditText android:id"@+id/name" />
 </LinearLayout>
</ScrollView>

<LinearLayout
            android:id"@+id/button_group"
            android:layout_width"match_parent"
            android:layout_height"wrap_content"
            android:padding"16dp"
            android:layout_alignParentBottom"true"
            android:background"@color/white80">

</RelativeLayout>


onView(withId(R.id.name)).perform(scrollTo(), typeText(name), closeSoftKeyboard());

It says that it can’t find the id name. It works fine if the screen is big but if it’s small and the edittext name is behind the button_group which has an alpha backgroound, it always fail.

Solution

Maybe you could alter the button_group visibility with a custom ViewAction before performing the scroll.

Firstly, you need the custom ViewAction that performs the change in the visibility:

private static ViewAction setViewVisibitity(final boolean value) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return null;
        }

        @Override
        public String getDescription() {
            return "Show / Hide View";
        }

        @Override
        public void perform(UiController uiController, View view) {
            view.setVisibility(value ? View.VISIBLE : View.GONE);
        }
    };
}

And then:

onView(withId(R.id.button_group)).perform(setViewVisibitity(false));

onView(withId(R.id.name)).perform(scrollTo(), typeText(name), closeSoftKeyboard());

And later on you can restore the button_group visibility whenever you want:

onView(withId(R.id.button_group)).perform(setViewVisibitity(true));

Give it a try. As the button_group will have its visibility set to GONE it should not interfere with the scroll.

Answered By – jeprubio

Leave a Comment