Measure the height of the view will be when showing. - Android User Interface

Android examples for User Interface:View Size

Description

Measure the height of the view will be when showing.

Demo Code


//package com.java2s;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;

public class Main {
    /**//w w  w  . j a  v a2 s . c  o m
     * Measure the height of the view will be when showing.
     * 
     * @param view
     *            View to measure.
     */
    public static void measureView(View view) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp == null) {
            lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        int childMeasureWidth = ViewGroup.getChildMeasureSpec(0, 0,
                lp.width);
        int childMeasureHeight;
        if (lp.height > 0) {
            childMeasureHeight = MeasureSpec.makeMeasureSpec(lp.height,
                    MeasureSpec.EXACTLY);
        } else {
            // Measure specification mode: The parent has not imposed any
            // constraint on the child. It can be whatever size it wants.
            childMeasureHeight = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
        }
        view.measure(childMeasureWidth, childMeasureHeight);
    }
}

Related Tutorials