add View On Global Layout Listener - Android User Interface

Android examples for User Interface:Layout

Description

add View On Global Layout Listener

Demo Code


//package com.java2s;

import android.os.Build;

import android.view.View;
import android.view.ViewTreeObserver;

public class Main {
    public static void addOnGlobalLayoutListener(final View view,
            final Runnable runnable) {
        ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override// www  .  j ava 2s . c  o m
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    view.getViewTreeObserver()
                            .removeGlobalOnLayoutListener(this);
                } else {
                    view.getViewTreeObserver()
                            .removeOnGlobalLayoutListener(this);
                }
                runnable.run();
            }
        });
    }
}

Related Tutorials