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.bytecode.project12.ImageViewPager.java

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

    View view = getChildAt(0);
    if (view != null) {
        view.measure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), view.getMeasuredHeight());
    }// ww  w  .  j av  a 2s . c o  m
}

From source file:com.github.OrangeGangsters.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
 *//*from w  ww  .ja  va  2  s  . c om*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    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);
            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);
}

From source file:com.charon.materialsample.view.ScrollingTabs.java

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);
    }/*  www .  j ava 2s .  com*/

    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);
}

From source file:com.kiwiteam.nomiddleman.TourPageActivity.java

/**
 * Makes listview items fill the list view on page to allow all list items to appear on page
 * @param listView// ww  w  .java2 s .c  o  m
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }

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

From source file:com.jungle.widgets.view.VertWrapContentViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int position = getCurrentItem();

    int childHeight = 0;
    for (int i = 0; i < getChildCount(); ++i) {
        View childView = getChildAt(i);
        childView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int height = childView.getMeasuredHeight();

        Object tag = childView.getTag(TAG_POSITION);
        if (tag instanceof Integer) {
            int tagPos = (int) tag;
            if (position == tagPos) {
                childHeight = height;/*from   w w  w  . j a  v  a2s .  c om*/
                break;
            }
        }

        if (height > childHeight) {
            childHeight = height;
        }
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

From source file:com.facebook.getrecommendations.AppCardPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if (h > height) {
            height = h;// w  ww  .j a  v  a  2 s. co  m
        }
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

From source file:app.wenya.sketchbookpro.ui.view.looppager.WidgetViewPager.java

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

    int height = 0;
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if (h > height)
            height = h;//from  w w  w. j  ava2 s . co  m
    }

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

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

From source file:com.focosee.qingshow.widget.MVerticalViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mSinglePageHeight = this.getMeasuredHeight();
    final LayoutParams lp = (LayoutParams) mRootContainer.getLayoutParams();
    int rootWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, lp.width);
    int rootHeightMeasureSpec = MeasureSpec.makeMeasureSpec(mSinglePageHeight * mViews.length,
            MeasureSpec.EXACTLY);/*from  w  ww.ja v  a  2  s. c  o m*/

    mRootContainer.measure(rootWidthMeasureSpec, rootHeightMeasureSpec);
    int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(mSinglePageHeight, MeasureSpec.EXACTLY);
    int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(mRootContainer.getMeasuredWidth(),
            MeasureSpec.EXACTLY);
    for (View view : mViews) {
        view.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
}

From source file:com.asc_ii.bangnote.bigbang.BigBangActionBar.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int childCount = getChildCount();
    int measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        child.measure(measureSpec, measureSpec);
    }//from  ww w.  j  a  v a2  s  . c  o m

    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    setMeasuredDimension(width, height + mContentPadding + mSearch.getMeasuredHeight());
}

From source file:com.lgallardo.qbittorrentclient.TorrentDetailsFragment.java

/**
 * *//from   ww w .  j ava2 s  . c o m
 * Method for Setting the Height of the ListView dynamically. Hack to fix
 * the issue of not showing all the items of the ListView when placed inside
 * a ScrollView
 * **
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

        long numOfLines = 1;
        view = listAdapter.getView(i, view, listView);

        if (i == 0) {
            view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

        TextView file = (TextView) view.findViewById(R.id.file);
        TextView percentage = (TextView) view.findViewById(R.id.percentage);
        ProgressBar progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);

        if (view.getMeasuredWidth() > desiredWidth) {

            double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
            double desiredWidthLong = Double.valueOf(desiredWidth);

            numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;

            totalHeight += (file.getMeasuredHeight() * numOfLines) + percentage.getMeasuredHeight()
                    + progressBar1.getMeasuredHeight();

        } else {
            totalHeight += view.getMeasuredHeight();
        }

    }

    LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
    listView.requestLayout();

}