Try to get the view layout info before onResume(). - Android User Interface

Android examples for User Interface:Layout

Description

Try to get the view layout info before onResume().

Demo Code


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class Main{
    /**/*from www  . j  a  v a2 s .c o m*/
     * Try to get the view layout info before onResume().
     * 
     * @param view the view you want to get its layout info before onResume(), ViewGroup is also OK.
     * @param listener the callback when you can get the view layout info.
     */
    public static void getViewLayoutInfoBeforeOnResume(final View view,
            final LayoutUtils.OnViewGlobalLayoutListener listener) {
        // The ViewTreeObserver observer is not guaranteed to remain valid for the lifetime of this View.
        // So we have to check isAlive().
        if (view.getViewTreeObserver().isAlive()) {
            view.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                        // We use the new method when supported
                        @SuppressWarnings("deprecation")
                        // We check which build version we are using.
                        @SuppressLint("NewApi")
                        @Override
                        public void onGlobalLayout() {
                            // remove listener
                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                view.getViewTreeObserver()
                                        .removeGlobalOnLayoutListener(this);
                            } else {
                                view.getViewTreeObserver()
                                        .removeOnGlobalLayoutListener(this);
                            }
                            if (listener != null) {
                                listener.onViewGlobalLayout(view);
                            }
                        }
                    });
        } else {
            System.err.println("== The ViewTreeObserver is NOT alive. ==");
        }
    }
}

Related Tutorials