Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an Android test, how to check that a View is shown?

I'm writing a UI test case (using ActivityUnitTestCase) and would like to check if at a given time a View subclass is visible to the user. I've found the View#isShown() method, which claims to do exactly this - checking the visibility field of this element and all its parents - but somehow it always returns "false" for all the elements. I'll be grateful for some help. If it makes it easier, I can paste some code.

Also, I found ViewAsserts#assertOnScreen(View origin, View view) but it doesn't seem to do the right thing either - always returns true. Am I perhaps calling it wrong: assertOnScreen(viewImTesting.getRootView(), viewImTesting)?

Thanks, Jan

like image 291
Jan Żankowski Avatar asked Sep 21 '25 04:09

Jan Żankowski


1 Answers

I found a sensible workaround: just checking View#getVisibility() against View#VISIBLE, VIEW#INVISIBLE, or VIEW#GONE.

This probably doesn't work when e.g. a parent view is not visible but this one has visibility set to VISIBLE, but for most cases it should suffice.

like image 191
Jan Żankowski Avatar answered Sep 22 '25 17:09

Jan Żankowski