Federico Ponzi Federico Ponzi - 8 months ago 40
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