Skip to content

Instantly share code, notes, and snippets.

@matthewmorrone
Created September 29, 2020 18:52
Show Gist options
  • Select an option

  • Save matthewmorrone/aa2373846f5bd4a4993994ebf938d167 to your computer and use it in GitHub Desktop.

Select an option

Save matthewmorrone/aa2373846f5bd4a4993994ebf938d167 to your computer and use it in GitHub Desktop.
private void layoutAndShowPopupWindow(Key key, int xPosition) {
popupWindow = new PopupWindow(popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setClippingEnabled(false);// <-- let popup display above keyboard
int location[] = new int[2];
key.getLocationInWindow(location);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
popupView.measure(measureSpec, measureSpec);
int popupWidth = popupView.getMeasuredWidth();
int spaceAboveKey = key.getHeight() / 4;
int x = xPosition - popupWidth / popupView.getChildCount() / 2;
int y = location[1] - popupView.getMeasuredHeight() - spaceAboveKey;
popupWindow.showAtLocation(key, Gravity.NO_GRAVITY, x, y);
// using popupWindow.showAsDropDown(key, 0, yOffset) might be
// easier than popupWindow.showAtLocation()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment