Example usage for android.support.v4.widget PopupWindowCompat showAsDropDown

List of usage examples for android.support.v4.widget PopupWindowCompat showAsDropDown

Introduction

In this page you can find the example usage for android.support.v4.widget PopupWindowCompat showAsDropDown.

Prototype

public static void showAsDropDown(PopupWindow popupWindow, View view, int i, int i2, int i3) 

Source Link

Usage

From source file:ac.robinson.bettertogether.QRPopupWindow.java

void showPopUp() {
    // could use mPopupWindow.getMaxAvailableHeight() if only there was a width version!
    DisplayMetrics displayMetrics = new DisplayMetrics();
    mWindowManager.getDefaultDisplay().getMetrics(displayMetrics);
    int popupSize = (int) Math
            .round((displayMetrics.widthPixels > displayMetrics.heightPixels ? displayMetrics.heightPixels
                    : displayMetrics.widthPixels) * 0.9);

    // normally: WindowManager.LayoutParams.WRAP_CONTENT);
    mPopupWindow.setWidth(popupSize);// w  ww .  ja v  a2s .c o m
    mPopupWindow.setHeight(popupSize);
    mPopupWindow.setTouchable(true);
    mPopupWindow.setFocusable(true);
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

    int elevation = mAnchorView.getContext().getResources().getDimensionPixelSize(R.dimen.default_elevation);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mPopupWindow.setElevation(elevation);
    }

    // display as popup overlaying anchor view - as 4dp elevation is same as horizontal offset, use that precalculated value
    PopupWindowCompat.setOverlapAnchor(mPopupWindow, true);
    PopupWindowCompat.showAsDropDown(mPopupWindow, mAnchorView, -elevation, 0, Gravity.TOP | Gravity.END);
}

From source file:com.android.talkbacktests.testsession.PopupViewTest.java

/**
 * Shows a PopupWindow at the anchor view with given window height and list size.
 *//*from  w  w  w .  jav  a 2s. co m*/
private void showPopupWindow(View button, int height, int listSize, boolean useDefaultInflater) {
    final Context context = button.getContext();
    final ListView listView = new ListView(context);
    final BaseAdapter adapter;
    if (useDefaultInflater) {
        adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, android.R.id.text1,
                createSampleArray(listSize));
    } else {
        adapter = new MyAdapter(button.getContext(), createSampleArray(listSize));
    }
    listView.setAdapter(adapter);

    listView.setVerticalScrollBarEnabled(true);
    listView.setBackgroundColor(0xFFFFFF);
    final PopupWindow window = new PopupWindow(listView, ViewGroup.LayoutParams.WRAP_CONTENT, height, true);
    window.setBackgroundDrawable(context.getResources().getDrawable(android.R.drawable.editbox_background));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if (window.isShowing()) {
                window.dismiss();
            }
        }
    });
    PopupWindowCompat.showAsDropDown(window, button, 0, 0, Gravity.NO_GRAVITY);
}

From source file:org.kaaproject.kaa.demo.iotworld.smarthome.widget.SmartHomeToolbar.java

public void setCustomToolbarContentEnabled(boolean enabled) {
    mCustomContentVisible = enabled;//from w ww.  j a v  a2  s .c om
    if (mCustomContent != null && mCustomContentVisible) {
        if (mCustomContentViewWidth < mCustomContentMinWidth) {
            if (mToolbarCustomContentView.getChildCount() > 0) {
                mToolbarCustomContentView.removeAllViews();
            }
            if (mPopupContainer.getChildCount() == 0) {
                mPopupContainer.addView(mCustomContent, mCustomContentPopupViewLayoutParams);
                mPopupContainer.setCardBackgroundColor(mBackgroundColor);
            }
            int popupWidth = (int) (getWidth() * 0.7);
            if (mPopup == null) {
                mPopup = new PopupWindow(mPopupContainer, popupWidth, mCustomContentHeight, true);
                mPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                mPopup.setOnDismissListener(new OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        mCustomContentVisible = false;
                    }
                });
            } else {
                mPopup.setWidth(popupWidth);
            }
            PopupWindowCompat.showAsDropDown(mPopup, this, getWidth() - popupWidth, 0,
                    Gravity.TOP | Gravity.LEFT);
        } else {
            if (mPopupContainer.getChildCount() > 0) {
                mPopupContainer.removeAllViews();
            }
            if (mToolbarCustomContentView.getChildCount() == 0) {
                mToolbarCustomContentView.addView(mCustomContent, mCustomContentViewLayoutParams);
            }
        }
    }
    if (!mCustomContentVisible && mPopup != null && mPopup.isShowing()) {
        mPopup.dismiss();
    }
    if (mCustomContent != null) {
        mCustomContent.setVisibility(mCustomContentVisible ? View.VISIBLE : View.GONE);
    }
}

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

/**
 * Show the popup list. If the list is already showing, this method
 * will recalculate the popup's size and position.
 *///from ww  w  . ja  v a2s. co m
public void show() {
    int height = buildDropDown();

    int widthSpec = 0;
    int heightSpec = 0;

    boolean noInputMethod = isInputMethodNotNeeded();

    if (mPopup.isShowing()) {
        if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) {
            // The call to PopupWindow's update method below can accept -1 for any
            // value you do not want to update.
            widthSpec = -1;
        } else if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
            widthSpec = getAnchorView().getWidth();
        } else {
            widthSpec = mDropDownWidth;
        }

        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            // The call to PopupWindow's update method below can accept -1 for any
            // value you do not want to update.
            heightSpec = noInputMethod ? height : ViewGroup.LayoutParams.MATCH_PARENT;
            if (noInputMethod) {
                mPopup.setWindowLayoutMode(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT
                        ? ViewGroup.LayoutParams.MATCH_PARENT
                        : 0, 0);
            } else {
                mPopup.setWindowLayoutMode(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT
                        ? ViewGroup.LayoutParams.MATCH_PARENT
                        : 0, ViewGroup.LayoutParams.MATCH_PARENT);
            }
        } else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
            heightSpec = height;
        } else {
            heightSpec = mDropDownHeight;
        }

        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);

        mPopup.update(getAnchorView(), mDropDownHorizontalOffset, mDropDownVerticalOffset, widthSpec,
                heightSpec);
    } else {
        if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) {
            widthSpec = ViewGroup.LayoutParams.MATCH_PARENT;
        } else {
            if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
                mPopup.setWidth(getAnchorView().getWidth());
            } else {
                mPopup.setWidth(mDropDownWidth);
            }
        }

        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            heightSpec = ViewGroup.LayoutParams.MATCH_PARENT;
        } else {
            if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
                mPopup.setHeight(height);
            } else {
                mPopup.setHeight(mDropDownHeight);
            }
        }

        mPopup.setWindowLayoutMode(widthSpec, heightSpec);
        setPopupClipToScreenEnabled(true);

        // use outside touchable to dismiss drop down when touching outside of it, so
        // only set this if the dropdown is not always visible
        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);
        mPopup.setTouchInterceptor(mTouchInterceptor);
        PopupWindowCompat.showAsDropDown(mPopup, getAnchorView(), mDropDownHorizontalOffset,
                mDropDownVerticalOffset, mDropDownGravity);
        mDropDownList.setSelection(ListView.INVALID_POSITION);

        if (!mModal || mDropDownList.isInTouchMode()) {
            clearListSelection();
        }
        if (!mModal) {
            mHandler.post(mHideSelector);
        }

        // show item animation
        if (mItemAnimationId != 0)
            mPopup.getContentView().getViewTreeObserver()
                    .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

                        @Override
                        public boolean onPreDraw() {
                            mPopup.getContentView().getViewTreeObserver().removeOnPreDrawListener(this);
                            for (int i = 0, count = mDropDownList.getChildCount(); i < count; i++) {
                                View v = mDropDownList.getChildAt(i);

                                Animation anim = AnimationUtils.loadAnimation(mContext, mItemAnimationId);
                                anim.setStartOffset(mItemAnimationOffset * i);
                                v.startAnimation(anim);
                            }
                            return false;
                        }

                    });
    }
}

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

/**
 * Show the popup list. If the list is already showing, this method
 * will recalculate the popup's size and position.
 *///from ww  w  .  ja v a2s .c  o m
public void show() {
    int height = buildDropDown();

    int widthSpec = 0;
    int heightSpec = 0;

    boolean noInputMethod = isInputMethodNotNeeded();

    if (mPopup.isShowing()) {
        if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) {
            // The call to PopupWindow's update method below can accept -1 for any
            // value you do not want to update.
            widthSpec = -1;
        } else if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
            widthSpec = getAnchorView().getWidth();
        } else {
            widthSpec = mDropDownWidth;
        }

        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            // The call to PopupWindow's update method below can accept -1 for any
            // value you do not want to update.
            heightSpec = noInputMethod ? height : ViewGroup.LayoutParams.MATCH_PARENT;
            if (noInputMethod) {
                mPopup.setWindowLayoutMode(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT
                        ? ViewGroup.LayoutParams.MATCH_PARENT
                        : 0, 0);
            } else {
                mPopup.setWindowLayoutMode(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT
                        ? ViewGroup.LayoutParams.MATCH_PARENT
                        : 0, ViewGroup.LayoutParams.MATCH_PARENT);
            }
        } else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
            heightSpec = height;
        } else {
            heightSpec = mDropDownHeight;
        }

        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);

        mPopup.update(getAnchorView(), mDropDownHorizontalOffset, mDropDownVerticalOffset, widthSpec,
                heightSpec);
    } else {
        if (mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT) {
            widthSpec = ViewGroup.LayoutParams.MATCH_PARENT;
        } else {
            if (mDropDownWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
                mPopup.setWidth(getAnchorView().getWidth());
            } else {
                mPopup.setWidth(mDropDownWidth);
            }
        }

        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            heightSpec = ViewGroup.LayoutParams.MATCH_PARENT;
        } else {
            if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
                mPopup.setHeight(height);
            } else {
                mPopup.setHeight(mDropDownHeight);
            }
        }

        mPopup.setWindowLayoutMode(widthSpec, heightSpec);
        setPopupClipToScreenEnabled(true);

        // use outside touchable to dismiss drop down when touching outside of it, so
        // only set this if the dropdown is not always visible
        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);
        mPopup.setTouchInterceptor(mTouchInterceptor);
        PopupWindowCompat.showAsDropDown(mPopup, getAnchorView(), mDropDownHorizontalOffset,
                mDropDownVerticalOffset, mDropDownGravity);
        mDropDownList.setSelection(ListView.INVALID_POSITION);

        if (!mModal || mDropDownList.isInTouchMode()) {
            clearListSelection();
        }
        if (!mModal) {
            mHandler.post(mHideSelector);
        }
    }
}

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

/**
 * Show the popup list. If the list is already showing, this method
 * will recalculate the popup's size and position.
 *///from ww w.  ja  v  a 2 s  .  c  o m
public void show() {
    final int height = buildDropDown();
    final int widthSpec = getListWidthSpec();

    boolean noInputMethod = isInputMethodNotNeeded();
    PopupWindowCompat.setWindowLayoutType(mPopup, mDropDownWindowLayoutType);

    final int marginsLeft = mMargins.left;
    final int marginsTop = mMargins.top;
    final int marginsBottom = mMargins.bottom;
    final int marginsRight = mMargins.right;

    getBackgroundPadding(mTempRect);
    final int backgroundLeft = mTempRect.left;
    final int backgroundTop = mTempRect.top;
    final int backgroundBottom = mTempRect.bottom;
    final int backgroundRight = mTempRect.right;

    int verticalOffset = mDropDownVerticalOffset;
    int horizontalOffset = mDropDownHorizontalOffset;

    final int anchorWidth = mDropDownAnchorView.getWidth();
    final int anchorHeight = mDropDownAnchorView.getHeight();

    getLocationInWindow(mDropDownAnchorView, mTempLocation);
    final int anchorLeft = mTempLocation[0];
    final int anchorRight = anchorLeft + anchorWidth;
    final int anchorTop = mTempLocation[1];
    final int anchorBottom = anchorTop + anchorHeight;

    final boolean rightAligned = GravityCompat.getAbsoluteGravity(
            getDropDownGravity() & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK,
            mLayoutDirection) == Gravity.RIGHT;
    if (rightAligned) {
        horizontalOffset += anchorWidth - widthSpec - (marginsRight - backgroundRight);
    } else {
        horizontalOffset += (marginsLeft - backgroundLeft);
    }

    final int bottomDecorations = getWindowFrame(mDropDownAnchorView, noInputMethod, mTempRect);
    final int windowLeft = mTempRect.left;
    final int windowRight = mTempRect.right;
    final int windowTop = mTempRect.top;
    final int windowBottom = mTempRect.bottom;

    final int windowWidth = windowRight - windowLeft;
    final int windowHeight = windowBottom - windowTop;

    getBoundsInWindow(mTempRect);
    final int boundsTop = mTempRect.top;
    final int boundsRight = mTempRect.right;
    final int boundsLeft = mTempRect.left;
    final int boundsBottom = mTempRect.bottom;

    final int screenRight = windowRight - (marginsRight - backgroundRight) - boundsRight;
    final int screenLeft = windowLeft + (marginsLeft - backgroundLeft) + boundsLeft;
    final int screenWidth = screenRight - screenLeft;

    if (!rightAligned && windowWidth < anchorLeft + horizontalOffset + widthSpec) {
        // When right aligned due to insufficient space ignore negative horizontal offset.
        horizontalOffset = mDropDownHorizontalOffset < 0 ? 0 : mDropDownHorizontalOffset;
        horizontalOffset -= widthSpec - (windowWidth - anchorLeft);
        horizontalOffset -= marginsRight - backgroundRight;
    } else if (rightAligned && 0 > anchorLeft + horizontalOffset) {
        // When left aligned due to insufficient space ignore positive horizontal offset.
        horizontalOffset = mDropDownHorizontalOffset > 0 ? 0 : mDropDownHorizontalOffset;
        horizontalOffset -= anchorLeft;
        horizontalOffset += marginsLeft - backgroundLeft;
    }

    // Width spec should always be resolved to concrete value. widthSpec > 0;
    if (windowWidth < widthSpec + horizontalOffset + anchorLeft) {
        int diff = Math.abs(windowWidth - (widthSpec + horizontalOffset + anchorLeft));
        horizontalOffset -= diff;
    } else if (0 > anchorLeft + horizontalOffset) {
        int diff = Math.abs(horizontalOffset + anchorLeft);
        horizontalOffset += diff;
    }

    int maxHeight = getMaxAvailableHeight(mDropDownAnchorView, noInputMethod) + backgroundTop
            + backgroundBottom;
    int availableHeight = maxHeight;
    //        availableHeight -= Math.max(0, marginsTop - backgroundTop);
    //        availableHeight -= Math.max(0, marginsBottom - backgroundBottom);
    availableHeight -= marginsTop - backgroundTop;
    availableHeight -= marginsBottom - backgroundBottom;

    int limitHeight = Math.min(windowHeight, availableHeight);

    final int heightSpec;
    if (mPopup.isShowing()) {
        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            // The call to PopupWindow's update method below can accept -1 for any
            // value you do not want to update.
            //                heightSpec = noInputMethod ? height : ViewGroup.LayoutParams.MATCH_PARENT;
            //                if (noInputMethod) {
            //                    mPopup.setWidth(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ?
            //                        ViewGroup.LayoutParams.MATCH_PARENT : 0);
            //                    mPopup.setHeight(0);
            //                } else {
            //                    mPopup.setWidth(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ?
            //                        ViewGroup.LayoutParams.MATCH_PARENT : 0);
            //                    mPopup.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            //                }
            heightSpec = limitHeight;
        } else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
            heightSpec = Math.min(height, limitHeight);
        } else {
            heightSpec = Math.min(mDropDownHeight, limitHeight);
        }
    } else {
        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            //                heightSpec = ViewGroup.LayoutParams.MATCH_PARENT;
            heightSpec = limitHeight;
        } else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
            heightSpec = Math.min(height, limitHeight);
        } else {
            heightSpec = Math.min(mDropDownHeight, limitHeight);
        }
    }

    final int screenBottom = windowBottom - (marginsBottom - backgroundBottom) - boundsBottom;
    final int screenTop = windowTop + (marginsTop - backgroundTop) + boundsTop;

    {
        // Position within bounds.

        final int popupTop = anchorBottom + verticalOffset;
        final int popupBottom = popupTop + heightSpec;
        final int popupHeight = popupBottom - popupTop;

        if (popupBottom > screenBottom) {
            verticalOffset -= (popupBottom - screenBottom);
        } else if (popupTop < screenTop) {
            verticalOffset += (screenTop - popupTop);
        }
    }

    {
        // Account for background padding.

        final int popupTop = anchorBottom + verticalOffset;
        final int popupBottom = popupTop + heightSpec;
        final int popupHeight = popupBottom - popupTop;

        if (windowBottom < popupBottom) {
            int diff = Math.abs(windowBottom - popupBottom);
            verticalOffset -= diff;
        } else if (windowTop > popupTop) {
            int diff = Math.abs(windowTop - popupTop);
            verticalOffset += diff;
        }
    }

    //        verticalOffset -= bottomDecorations;
    //        verticalOffset += Util.dpToPxOffset(mContext, 8);

    if (mPopup.isShowing()) {
        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);

        mPopup.update(getAnchorView(), horizontalOffset, verticalOffset, (widthSpec < 0) ? -1 : widthSpec,
                (heightSpec < 0) ? -1 : heightSpec);
    } else {

        mPopup.setWidth(widthSpec);
        mPopup.setHeight(heightSpec);
        setPopupClipToScreenEnabled(true);

        // use outside touchable to dismiss drop down when touching outside of it, so
        // only set this if the dropdown is not always visible
        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);
        mPopup.setTouchInterceptor(mTouchInterceptor);
        if (sSetEpicenterBoundsMethod != null) {
            try {
                sSetEpicenterBoundsMethod.invoke(mPopup, mEpicenterBounds);
            } catch (Exception e) {
                Log.e(TAG, "Could not invoke setEpicenterBounds on PopupWindow", e);
            }
        }
        // We handle gravity manually. Just as everything else.
        PopupWindowCompat.showAsDropDown(mPopup, getAnchorView(), horizontalOffset, verticalOffset,
                Gravity.NO_GRAVITY);
        mDropDownList.setSelection(ListView.INVALID_POSITION);

        if (DEBUG)
            Log.e(TAG, "isAboveAnchor=" + mPopup.isAboveAnchor());

        if (!mModal || mDropDownList.isInTouchMode()) {
            clearListSelection();
        }
        if (!mModal) {
            mHandler.post(mHideSelector);
        }
    }
}