Code example for PopupWindow

Methods: getBackgroundgetMaxAvailableHeightupdate

0
        @Override 
        public void onGlobalLayout() { 
            if (mPopupWindow == null) return; 
            updatePopupLayoutParams(); 
            // Need to update the position of the popup window 
            mPopupWindow.update(mAnchorView, 
                    mPopupOffsetX, mPopupOffsetY, mPopupWidth, mPopupHeight); 
        } 
    }; 
 
    public void show() { 
        if (mPopupWindow != null) return;
        mAnchorView.getViewTreeObserver()
                .addOnGlobalLayoutListener(mOnGLobalLayoutListener);
        mPopupWindow = createPopupWindow();
        updatePopupLayoutParams(); 
        mPopupWindow.setWidth(mPopupWidth);
        mPopupWindow.setHeight(mPopupHeight);
        mPopupWindow.showAsDropDown(mAnchorView, mPopupOffsetX, mPopupOffsetY);
    } 
 
    private void updatePopupLayoutParams() { 
        ListView content = mContentList;
        PopupWindow popup = mPopupWindow;
 
        Rect p = new Rect();
        popup.getBackground().getPadding(p);
 
        int maxHeight = mPopupWindow.getMaxAvailableHeight(mAnchorView) - p.top - p.bottom;
        mContentList.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));
        mPopupWidth = content.getMeasuredWidth() + p.top + p.bottom;
        mPopupHeight = Math.min(maxHeight, content.getMeasuredHeight() + p.left + p.right);