Example usage for android.view View measure

List of usage examples for android.view View measure

Introduction

In this page you can find the example usage for android.view View measure.

Prototype

public final void measure(int widthMeasureSpec, int heightMeasureSpec) 

Source Link

Document

This is called to find out how big a view should be.

Usage

From source file:com.ameron32.apps.tapnotes._trial.ui.MultiViewPager.java

protected void onMeasurePage(int widthMeasureSpec, int heightMeasureSpec) {
    // Only measure if a measurement pass was scheduled
    if (!mNeedsMeasurePage) {
        return;/*from w w w .j a va2 s. c  o m*/
    }
    if (mMatchWidthChildResId == 0) {
        mNeedsMeasurePage = false;
    } else if (getChildCount() > 0) {
        View child = getChildAt(0);
        child.measure(widthMeasureSpec, heightMeasureSpec);
        int pageWidth = child.getMeasuredWidth();
        View match = child.findViewById(mMatchWidthChildResId);
        if (match == null) {
            throw new NullPointerException(
                    "MatchWithChildResId did not find that ID in the first fragment of the ViewPager; "
                            + "is that view defined in the child view's layout? Note that MultiViewPager "
                            + "only measures the child for index 0.");
        }
        int childWidth = match.getMeasuredWidth();
        // Check that the measurement was successful
        if (childWidth > 0) {
            mNeedsMeasurePage = false;
            int difference = pageWidth - childWidth;
            setPageMargin(-difference);
            int offscreen = (int) Math.ceil((float) pageWidth / (float) childWidth) + 1;
            setOffscreenPageLimit(offscreen);
            requestLayout();
        }
    }
}

From source file:com.github.omadahealth.circularbarpager.library.viewpager.WrapContentViewPager.java

/**
 * Allows to redraw the view size to wrap the content of the bigger child.
 *
 * @param widthMeasureSpec  with measured
 * @param heightMeasureSpec height measured
 *//* w  w  w .  ja  v a 2 s  .c  o  m*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    try {
        int mode = MeasureSpec.getMode(heightMeasureSpec);

        if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int height = 0;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                if (child != null) {
                    child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    int h = child.getMeasuredHeight();
                    if (h > height) {
                        height = h;
                    }
                }
            }
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    } catch (RuntimeException e) {
        Log.e(TAG, "Exception during WrapContentViewPager onMeasure " + e.toString());
    }
}

From source file:cn.ieclipse.af.view.ah.AutoHeightViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int size = getChildCount();
    if (size > 0 && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
        Point p = mHeights.get(getCurrentItem());
        if (p == null) {
            try {
                int i = getChildIndex();
                View child = getChildAt(i);
                if (child != null) {
                    child.measure(widthMeasureSpec,
                            MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED));
                    int w = child.getMeasuredWidth();
                    int h = child.getMeasuredHeight();
                    mHeights.put(getCurrentItem(), new Point(w, h));
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
                    // widthMeasureSpec = MeasureSpec.makeMeasureSpec(w,
                    // MeasureSpec.EXACTLY);
                }//www  .j  av a2  s  . c om
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(p.y, MeasureSpec.EXACTLY);
            // widthMeasureSpec = MeasureSpec.makeMeasureSpec(p.x,
            // MeasureSpec.EXACTLY);
        }
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

From source file:com.lolay.android.ui.LolayBaseFragmentActivity.java

/**
 * Thanks to http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html
 * WARNING: Only works with Linear Layouts or you'll get a NullPointerException in the measure code
 *//*from   w  ww  .j a  va  2s. c  om*/
public void listViewSetHeightFromChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:com.brq.wallet.activity.main.WrapContentHeightViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // find the first child view
    View view = getChildAt(0);
    if (view != null) {
        // measure the first child view with the specified measure spec
        view.measure(widthMeasureSpec, heightMeasureSpec);
    }//from   w  ww  .j av a 2s .  c om

    setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
}

From source file:com.fastbootmobile.encore.app.ui.WrapContentHeightViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // get the currently visible fragment (after the title strip at index 0)
    View view = getChildAt(getCurrentItem() + 1);
    if (view != null) {
        // measure the first child view with the specified measure spec
        view.measure(widthMeasureSpec, heightMeasureSpec);
    }//from   w ww.  j a  v  a2 s.co m

    // Ensure we keep enough room for the pager strip as well
    final View tabStrip = getChildAt(0);
    int measuredHeight = measureHeight(heightMeasureSpec, view) + tabStrip.getMeasuredHeight();

    setMeasuredDimension(getMeasuredWidth(), measuredHeight);
}

From source file:com.actionbarsherlock.internal.view.menu.ActionMenuView.java

/**
 * Measure a child view to fit within cell-based formatting. The child's width
 * will be measured to a whole multiple of cellSize.
 *
 * <p>Sets the expandable and cellsUsed fields of LayoutParams.
 *
 * @param child Child to measure//from   w ww  .j  ava 2  s. c o  m
 * @param cellSize Size of one cell
 * @param cellsRemaining Number of cells remaining that this view can expand to fill
 * @param parentHeightMeasureSpec MeasureSpec used by the parent view
 * @param parentHeightPadding Padding present in the parent view
 * @return Number of cells this child was measured to occupy
 */
static int measureChildForCells(View child, int cellSize, int cellsRemaining, int parentHeightMeasureSpec,
        int parentHeightPadding) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();

    final int childHeightSize = MeasureSpec.getSize(parentHeightMeasureSpec) - parentHeightPadding;
    final int childHeightMode = MeasureSpec.getMode(parentHeightMeasureSpec);
    final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode);

    int cellsUsed = 0;
    if (cellsRemaining > 0) {
        final int childWidthSpec = MeasureSpec.makeMeasureSpec(cellSize * cellsRemaining, MeasureSpec.AT_MOST);
        child.measure(childWidthSpec, childHeightSpec);

        final int measuredWidth = child.getMeasuredWidth();
        cellsUsed = measuredWidth / cellSize;
        if (measuredWidth % cellSize != 0)
            cellsUsed++;
    }

    final ActionMenuItemView itemView = child instanceof ActionMenuItemView ? (ActionMenuItemView) child : null;
    final boolean expandable = !lp.isOverflowButton && itemView != null && itemView.hasText();
    lp.expandable = expandable;

    lp.cellsUsed = cellsUsed;
    final int targetWidth = cellsUsed * cellSize;
    child.measure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), childHeightSpec);
    return cellsUsed;
}

From source file:com.chatwing.whitelabel.views.HeightWrappingViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;

    if (wrapHeight) {
        /**//from  w w  w. j av  a  2 s.c o m
         * The first super.onMeasure call made the pager take up all the
         * available height. Since we really wanted to wrap it, we need
         * to remeasure it. Luckily, after that call the first child is
         * now available. So, we take the height from it.
         */

        int width = getMeasuredWidth(), height = getMeasuredHeight();

        // Use the previously measured width but simplify the calculations
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);

        /* If the pager actually has any children, take the first child's
         * height and call that our own */
        if (getChildCount() > 0) {
            View firstChild = getChildAt(0);

            /* The child was previously measured with exactly the full height.
             * Allow it to wrap this time around. */
            firstChild.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));

            height = firstChild.getMeasuredHeight();
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

From source file:com.culinars.culinars.WrapContentViewPager.java

private int measureViewHeight(View view) {
    view.measure(getChildMeasureSpec(widthMeasuredSpec, getPaddingLeft() + getPaddingRight(),
            view.getLayoutParams().width), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    return view.getMeasuredHeight();
}

From source file:com.andremion.heroes.ui.util.PagerSharedElementCallback.java

private void forceSharedElementLayout(View view) {
    int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}