Federico Ponzi Federico Ponzi - 1 year ago 75
Android Question

Handle a view visibility change without overriding the view

Is there a way to handle a view visibility change (say, from GONE to VISIBLE) without overriding the view?

Something like


Answer Source

You can use a GlobalLayoutListener to determine if there are any changes in the views visibility.

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        int newVis = myView.getVisibility();
        if((int)myView.getTag() != newVis)
            //visibility has changed
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download