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.
How It can be done? Thanks.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With