View wait For Measure - Android User Interface

Android examples for User Interface:View

Description

View wait For Measure

Demo Code


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

public class Main{
    public static void waitForMeasure(final View view,
            final OnMeasuredCallback callback) {
        int width = view.getWidth();
        int height = view.getHeight();

        if (width > 0 && height > 0) {
            callback.onMeasured(view, width, height);
            return;
        }/*from   w ww.  j  a v a  2  s. c  o m*/

        view.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        final ViewTreeObserver observer = view
                                .getViewTreeObserver();
                        if (observer.isAlive()) {
                            observer.removeOnPreDrawListener(this);
                        }

                        callback.onMeasured(view, view.getWidth(),
                                view.getHeight());

                        return true;
                    }
                });
    }
}

Related Tutorials