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:android.support.v7.widget.GridLayout.java

private void measureChildWithMargins2(View child, int parentWidthSpec, int parentHeightSpec, int childWidth,
        int childHeight) {
    int childWidthSpec = getChildMeasureSpec(parentWidthSpec, getTotalMargin(child, true), childWidth);
    int childHeightSpec = getChildMeasureSpec(parentHeightSpec, getTotalMargin(child, false), childHeight);
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.chenglong.muscle.ui.LazyViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, or internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view. We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
    // Children are just made to fill our space.
    int childWidthSize = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    /*/*from   w  ww .j  ava2  s .c o m*/
    * Make sure all children have been properly measured. Decor views
    * first. Right now we cheat and make this less complicated by assuming
    * decor views won't intersect. We will pin to edges based on gravity.
    */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                Log.d(TAG, "gravity: " + lp.gravity + " hgrav: " + hgrav + " vgrav: " + vgrav);
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;
                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(childWidthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, heightMode);
                child.measure(widthSpec, heightSpec);
                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }
    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);
    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;
    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
            }
        }
    }
}

From source file:android.support.design.widget.RaeTabLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // If we have a MeasureSpec which allows us to decide our height, try and use the default
    // height/*  w w w  . jav a  2 s . c o m*/
    final int idealHeight = dpToPx(getDefaultHeight()) + getPaddingTop() + getPaddingBottom();
    switch (MeasureSpec.getMode(heightMeasureSpec)) {
    case MeasureSpec.AT_MOST:
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                Math.min(idealHeight, MeasureSpec.getSize(heightMeasureSpec)), MeasureSpec.EXACTLY);
        break;
    case MeasureSpec.UNSPECIFIED:
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(idealHeight, MeasureSpec.EXACTLY);
        break;
    }

    final int specWidth = MeasureSpec.getSize(widthMeasureSpec);
    if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED) {
        // If we don't have an unspecified width spec, use the given size to calculate
        // the max tab width
        mTabMaxWidth = mRequestedTabMaxWidth > 0 ? mRequestedTabMaxWidth
                : specWidth - dpToPx(TAB_MIN_WIDTH_MARGIN);
    }

    // Now super measure itself using the (possibly) modified height spec
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (getChildCount() == 1) {
        // If we're in fixed mode then we need to make the tab strip is the same width as us
        // so we don't scroll
        final View child = getChildAt(0);
        boolean remeasure = false;

        switch (mMode) {
        case MODE_SCROLLABLE:
            // We only need to resize the child if it's smaller than us. This is similar
            // to fillViewport
            remeasure = child.getMeasuredWidth() < getMeasuredWidth();
            break;
        case MODE_FIXED:
            // Resize the child so that it doesn't scroll
            remeasure = child.getMeasuredWidth() != getMeasuredWidth();
            break;
        }

        if (remeasure) {
            // Re-measure the child with a widthSpec set to be exactly our measure width
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                    getPaddingTop() + getPaddingBottom(), child.getLayoutParams().height);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
}

From source file:cn.emagsoftware.ui.BugFixedSlidingPaneLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Consume all of the space if specified
            // or pick a magic number from thin air otherwise.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (widthMode == MeasureSpec.AT_MOST) {
                widthMode = MeasureSpec.EXACTLY;
            } else if (widthMode == MeasureSpec.UNSPECIFIED) {
                widthMode = MeasureSpec.EXACTLY;
                widthSize = 300;// w  w w .ja  v a 2 s . c  o m
            }
        } else {
            throw new IllegalStateException("Width must have an exact value or MATCH_PARENT");
        }
    } else if (heightMode == MeasureSpec.UNSPECIFIED) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Pick a magic number from thin air instead.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (heightMode == MeasureSpec.UNSPECIFIED) {
                heightMode = MeasureSpec.AT_MOST;
                heightSize = 300;
            }
        } else {
            throw new IllegalStateException("Height must not be UNSPECIFIED");
        }
    }

    int layoutHeight = 0;
    int maxLayoutHeight = -1;
    switch (heightMode) {
    case MeasureSpec.EXACTLY:
        layoutHeight = maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom();
        break;
    case MeasureSpec.AT_MOST:
        maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom();
        break;
    }

    float weightSum = 0;
    boolean canSlide = false;
    int widthRemaining = widthSize - getPaddingLeft() - getPaddingRight();
    final int childCount = getChildCount();

    if (childCount > 2) {
        Log.e(TAG, "onMeasure: More than two child views are not supported.");
    }

    // We'll find the current one below.
    mSlideableView = null;

    // First pass. Measure based on child LayoutParams width/height.
    // Weight will incur a second pass.
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        if (child.getVisibility() == GONE) {
            lp.dimWhenOffset = false;
            continue;
        }

        if (lp.weight > 0) {
            weightSum += lp.weight;

            // If we have no width, weight is the only contributor to the final size.
            // Measure this view on the weight pass only.
            if (lp.width == 0)
                continue;
        }

        int childWidthSpec;
        final int horizontalMargin = lp.leftMargin + lp.rightMargin;
        if (lp.width == LayoutParams.WRAP_CONTENT) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, MeasureSpec.AT_MOST);
        } else if (lp.width == LayoutParams.FILL_PARENT) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, MeasureSpec.EXACTLY);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
        }

        int childHeightSpec;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST);
        } else if (lp.height == LayoutParams.FILL_PARENT) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }

        child.measure(childWidthSpec, childHeightSpec);
        final int childWidth = child.getMeasuredWidth();
        final int childHeight = child.getMeasuredHeight();

        if (heightMode == MeasureSpec.AT_MOST && childHeight > layoutHeight) {
            layoutHeight = Math.min(childHeight, maxLayoutHeight);
        }

        widthRemaining -= childWidth;
        canSlide |= lp.slideable = widthRemaining < 0;
        if (lp.slideable) {
            mSlideableView = child;
        }
    }

    // Resolve weight and make sure non-sliding panels are smaller than the full screen.
    if (canSlide || weightSum > 0) {
        final int fixedPanelWidthLimit = widthSize - mOverhangSize;

        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);

            if (child.getVisibility() == GONE) {
                continue;
            }

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (child.getVisibility() == GONE) {
                continue;
            }

            final boolean skippedFirstPass = lp.width == 0 && lp.weight > 0;
            final int measuredWidth = skippedFirstPass ? 0 : child.getMeasuredWidth();
            if (canSlide && child != mSlideableView) {
                if (lp.width < 0 && (measuredWidth > fixedPanelWidthLimit || lp.weight > 0)) {
                    // Fixed panels in a sliding configuration should
                    // be clamped to the fixed panel limit.
                    final int childHeightSpec;
                    if (skippedFirstPass) {
                        // Do initial height measurement if we skipped measuring this view
                        // the first time around.
                        if (lp.height == LayoutParams.WRAP_CONTENT) {
                            childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST);
                        } else if (lp.height == LayoutParams.FILL_PARENT) {
                            childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY);
                        } else {
                            childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
                        }
                    } else {
                        childHeightSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(),
                                MeasureSpec.EXACTLY);
                    }
                    final int childWidthSpec = MeasureSpec.makeMeasureSpec(fixedPanelWidthLimit,
                            MeasureSpec.EXACTLY);
                    child.measure(childWidthSpec, childHeightSpec);
                }
            } else if (lp.weight > 0) {
                int childHeightSpec;
                if (lp.width == 0) {
                    // This was skipped the first time; figure out a real height spec.
                    if (lp.height == LayoutParams.WRAP_CONTENT) {
                        childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST);
                    } else if (lp.height == LayoutParams.FILL_PARENT) {
                        childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY);
                    } else {
                        childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
                    }
                } else {
                    childHeightSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(),
                            MeasureSpec.EXACTLY);
                }

                if (canSlide) {
                    // Consume available space
                    final int horizontalMargin = lp.leftMargin + lp.rightMargin;
                    final int newWidth = widthSize - horizontalMargin;
                    final int childWidthSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
                    if (measuredWidth != newWidth) {
                        child.measure(childWidthSpec, childHeightSpec);
                    }
                } else {
                    // Distribute the extra width proportionally similar to LinearLayout
                    final int widthToDistribute = Math.max(0, widthRemaining);
                    final int addedWidth = (int) (lp.weight * widthToDistribute / weightSum);
                    final int childWidthSpec = MeasureSpec.makeMeasureSpec(measuredWidth + addedWidth,
                            MeasureSpec.EXACTLY);
                    child.measure(childWidthSpec, childHeightSpec);
                }
            }
        }
    }

    setMeasuredDimension(widthSize, layoutHeight);
    mCanSlide = canSlide;
    if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && !canSlide) {
        // Cancel scrolling in progress, it's no longer relevant.
        mDragHelper.abort();
    }
}

From source file:android.support.v7.widget.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *///from ww  w.  j  av a  2s. c o m
private int buildDropDown() {
    ViewGroup dropDownView;
    int otherHeights = 0;

    if (mDropDownList == null) {
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // measure the hint's height to find how much more vertical space
            // we need to add to the drop down's height
            int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
            int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);
    } else {
        dropDownView = (ViewGroup) mPopup.getContentView();
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    // Max height available on the screen for a popup.
    boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = mPopup.getMaxAvailableHeight(getAnchorView(),
            mDropDownVerticalOffset /*, ignoreBottomDecorations*/);

    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:com.android.leanlauncher.CellLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    LauncherAppState app = LauncherAppState.getInstance();
    DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();

    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
    int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
    if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
        int cw = grid.calculateCellWidth(childWidthSize, mCountX);
        int ch = grid.calculateCellHeight(childHeightSize, mCountY);
        if (cw != mCellWidth || ch != mCellHeight) {
            mCellWidth = cw;/*from ww  w  .  j  a  va2s . c o m*/
            mCellHeight = ch;
            mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap, mCountX,
                    mCountY);
        }
    }

    int newWidth = childWidthSize;
    int newHeight = childHeightSize;
    if (mFixedWidth > 0 && mFixedHeight > 0) {
        newWidth = mFixedWidth;
        newHeight = mFixedHeight;
    } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
        throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
    }

    int numWidthGaps = mCountX - 1;
    int numHeightGaps = mCountY - 1;

    if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
        int hFreeSpace = childWidthSize - (mCountX * mCellWidth);
        int vFreeSpace = childHeightSize - (mCountY * mCellHeight);
        mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
        mHeightGap = Math.min(mMaxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
        mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap, mCountX,
                mCountY);
    } else {
        mWidthGap = mOriginalWidthGap;
        mHeightGap = mOriginalHeightGap;
    }
    int count = getChildCount();
    int maxWidth = 0;
    int maxHeight = 0;
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
        int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
        child.measure(childWidthMeasureSpec, childheightMeasureSpec);
        maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
        maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
    }
    if (mFixedWidth > 0 && mFixedHeight > 0) {
        setMeasuredDimension(maxWidth, maxHeight);
    } else {
        setMeasuredDimension(widthSize, heightSize);
    }
}

From source file:com.cliff.material.widget.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *//* w  ww  .jav  a2  s  .c  o  m*/
private int buildDropDown() {
    int otherHeights = 0;

    if (mDropDownList == null) {
        ViewGroup dropDownView;
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // measure the hint's height to find how much more vertical space
            // we need to add to the drop down's height
            int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
            int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);

    } else {
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    // Max height available on the screen for a popup.
    boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = mPopup.getMaxAvailableHeight(getAnchorView(),
            mDropDownVerticalOffset /*, ignoreBottomDecorations*/);

    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:co.codecrunch.musicplayerlite.slidinguppanelhelper.SlidingUpPanelLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException("Width must have an exact value or MATCH_PARENT");
    } else if (heightMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException("Height must have an exact value or MATCH_PARENT");
    }/*  w  w  w.  j  a v a 2s . co  m*/

    final int childCount = getChildCount();

    if (childCount != 2) {
        throw new IllegalStateException("Sliding up panel layout must have exactly 2 children!");
    }

    mMainView = getChildAt(0);
    mSlideableView = getChildAt(1);
    if (mDragView == null) {
        setDragView(mSlideableView);
    }

    // If the sliding panel is not visible, then put the whole view in the
    // hidden state
    if (mSlideableView.getVisibility() != VISIBLE) {
        mSlideState = PanelState.HIDDEN;
    }

    int layoutHeight = heightSize - getPaddingTop() - getPaddingBottom();
    int layoutWidth = widthSize - getPaddingLeft() - getPaddingRight();

    // First pass. Measure based on child LayoutParams width/height.
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        // We always measure the sliding panel in order to know it's height
        // (needed for show panel)
        if (child.getVisibility() == GONE && i == 0) {
            continue;
        }

        int height = layoutHeight;
        int width = layoutWidth;
        if (child == mMainView) {
            if (!mOverlayContent && mSlideState != PanelState.HIDDEN) {
                height -= mPanelHeight;
            }

            width -= lp.leftMargin + lp.rightMargin;
        } else if (child == mSlideableView) {
            // The slideable view should be aware of its top margin.
            // See
            // https://github.com/umano/AndroidSlidingUpPanel/issues/412.
            height -= lp.topMargin;
        }

        int childWidthSpec;
        if (lp.width == LayoutParams.WRAP_CONTENT) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
        } else if (lp.width == LayoutParams.MATCH_PARENT) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
        }

        int childHeightSpec;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
        } else if (lp.height == LayoutParams.MATCH_PARENT) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }

        child.measure(childWidthSpec, childHeightSpec);

        if (child == mSlideableView) {
            mSlideRange = mSlideableView.getMeasuredHeight() - mPanelHeight;
        }
    }

    setMeasuredDimension(widthSize, heightSize);
}

From source file:chickennugget.spaceengineersdata.material.widgets.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *//*  w ww.  j  a va2s.  c o m*/
private int buildDropDown() {
    int otherHeights = 0;

    if (mDropDownList == null) {
        ViewGroup dropDownView;
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // measure the hint's height to find how much more vertical space
            // we need to add to the drop down's height
            int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
            int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);

    } else {
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    int systemBarsReservedSpace = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        //  getMaxAvailableHeight() on Lollipop seems to ignore the system bars.
        systemBarsReservedSpace = Math.max(getSystemBarHeight("status_bar_height"),
                getSystemBarHeight("navigation_bar_height"));
    }

    // Max height available on the screen for a popup.
    boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = mPopup.getMaxAvailableHeight(getAnchorView(),
            mDropDownVerticalOffset /*, ignoreBottomDecorations*/) - systemBarsReservedSpace;

    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:com.hippo.widget.BothScrollView.java

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

    // TODO HorizontalFillViewport VerticalFillViewport
    if (!mFillViewport) {
        return;//  ww w  . j a  v  a  2 s.  c  om
    }

    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    if (widthMode == MeasureSpec.UNSPECIFIED) {
        return;
    }

    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    if (heightMode == MeasureSpec.UNSPECIFIED) {
        return;
    }

    if (getChildCount() > 0) {
        int childWidthMeasureSpec;
        int childHeightMeasureSpec;
        final View child = getChildAt(0);
        final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();

        int width = getMeasuredWidth();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        if (child.getMeasuredWidth() < width) {
            childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width - paddingLeft - paddingRight,
                    MeasureSpec.EXACTLY);
        } else {
            childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, lp.width);
        }

        int height = getMeasuredHeight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        if (child.getMeasuredHeight() < height) {
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height - paddingTop - paddingBottom,
                    MeasureSpec.EXACTLY);
        } else {
            childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom,
                    lp.height);
        }

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
}