Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align popup to the left of the view

I need to display a popup window whenever user presses the imageView(the present image). This popup window must be aligned to the left of the imageView as shown in the attached image. enter image description here

How It can be done? Thanks.

like image 736
eyal Avatar asked Nov 13 '22 00:11

eyal


1 Answers

To align the PopupWindow to the left of the trigger view, you must know the width of the PopupWinow before it is drawn.

// Before popup , need to get PopupWindow size.
// because contentView is not drawn in this time, width/height is 0.
// so, need to by measure to get contentView's size.
private static int makeDropDownMeasureSpec(int measureSpec) {
        int mode;
        if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) {
            mode = View.MeasureSpec.UNSPECIFIED;
        } else {
            mode = View.MeasureSpec.EXACTLY;
        }
        return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode);
}

then

// measure contentView size
View contentView = popupWindow.getContentView();
// need to measure first, because in this time PopupWindow is nit pop, width is 0.
contentView.measure(makeDropDownMeasureSpec(popupWindow.getWidth()),
                makeDropDownMeasureSpec(popupWindow.getHeight()));

int offsetX = -popupWindow.getContentView().getMeasuredWidth();
int offsetY = -mTriggerView.getHeight();
// show at the left edge of the trigger view
popupWindow.showAsDropDown(mTriggerView, offsetX, offsetY);
like image 88
haiwuxing Avatar answered Nov 15 '22 13:11

haiwuxing