0

I want to check if view is visible, so I used isShown() method on view, but it's quite useless because I create my own views and these depends on id, so here's example view:

private static final String BTN_ADD_CURRENCY_ALERT = "action_add_currency_alert";

private View btn_AddAlert() {
    return mainHelper.getView(BTN_ADD_CURRENCY_ALERT);
}

Here is body of getView();

public View getView(String id) {
    return solo.getView(id);
}

Then I want to see if this element is visible (if not then should return false).

I call this like this

if (btn_addAlert().isShown()){...}

And the problem is that when I call btn_addAlert() and view is not visible I get AssertionFailedError and it's not even going to isShown().

I catched exception

private boolean catcher() {
    try {
        btn_addAlert().isShown();
        return true;
    } catch (AssertionFailedError e) {
        return false;
    }
}

but it's wrong way because exceptions are not for this and also I have to wait whole timeout time to make it throw exception. Then it just return false.

So what can I do in this situation?

Thanks is advance.

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
k.szulc
  • 88
  • 1
  • 1
  • 9

2 Answers2

0

You can check it like

 if(v.getVisibility() == View.VISIBLE)
       // do you stuff here
Arslan
  • 249
  • 2
  • 13
0

Let's say you're already using this method:

public View getView(String id) {
    return solo.getView(id);
}

to check if this view is visible you can use:

 switch (getView("R.id.button").getVisibility()) {
        case (View.VISIBLE): {
            break;
        }
        case (View.INVISIBLE): {
            break;
        }

        case (View.GONE): {
            break;
        }
        default:
            break;
    }
}

In your exact case try to use btn_addAlert().getVisibility() instead of getView("R.id.button").getVisibility().

Hope it will help

EDIT: Try to use one of these listeners to check if view's visibility has changed:

  • using ViewTreeObserver().addOnGlobalLayoutListener

       btn_addAlert().setTag(myView.getVisibility());
       btn_addAlert().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int newVis = btn_addAlert().getVisibility();
            if((int)btn_addAlert().getTag() != newVis) {
                btn_addAlert().setTag(myView.getVisibility());
                //visibility has changed
               //here you can use my "switch" statement
            }
        }
     });
    

Check: Handle a view visibility change without overriding the view

  • using OnSystemUiVisibilityChangeListener:

    View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            // Note that system bars will only be "visible" if none of the
            // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                // TODO: The system bars are visible. Make any desired
                // adjustments to your UI, such as showing the action bar or
                // other navigational controls.
            } else {
               // TODO: The system bars are NOT visible. Make any desired
               // adjustments to your UI, such as hiding the action bar or
               // other navigational controls.
            }
        }
    });
    

Check: https://developer.android.com/training/system-ui/visibility.html

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • This is just switch version of answer below. I want to do something until view is visible so use while loop but if view is not visible it won't return false and skip loop. It will throw exception and will mark my test as failed. It crashes when I getView and view is not visible, because it's searching for it, and it's gone so it throws an exception. – k.szulc Sep 09 '16 at 06:37
  • Nope. Guy below is using mistical `v`, I'm using direct `Robotium` code, so that's not equal. Don't use loops where purpose is for listeners – piotrek1543 Sep 09 '16 at 07:52