do View On Global Layout - Android User Interface

Android examples for User Interface:Layout

Description

do View On Global Layout

Demo Code


//package com.java2s;

import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;

public class Main {
    public static void doOnGlobalLayout(final View view,
            final Runnable runnable) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override/*from  w w  w .  j  a  v a 2  s. c  o m*/
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            view.getViewTreeObserver()
                                    .removeOnGlobalLayoutListener(this);
                        } else {
                            view.getViewTreeObserver()
                                    .removeGlobalOnLayoutListener(this);
                        }
                        runnable.run();
                    }
                });
    }
}

Related Tutorials