Android: popup menu position
This is a simple and stupid question, but I am developing an application in which I show a popup menu with a click of a button. The code looks like this:
PopupMenu popup = new PopupMenu(mContext, view);
popup.getMenu().add("A");
popup.getMenu().add("B");
popup.getMenu().add("c");
popup.show();
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// My stuff
}
}
Problem: On some devices, the popup appears at the bottom of the button, but on some devices, the popup overlaps the button or partially overlaps.
How to set the popup so it doesn't overlap the button and the button is fully visible.
+3
source to share
2 answers
You can use a showAtLocation()
class method PopupWindow
to find PopupWindow
at a specific position on the screen, for example, for example:
PopupWindow popup = new PopupWindow(mContext, view);
popup.getMenu().add("A");
popup.getMenu().add("B");
popup.getMenu().add("c");
//takes the parent view, gravity and location offsets as paremetrs
popup.showAtLocation(view, Gravity.CENTER, 0, coordinate);
You can find other answers here: How to show PopupWindow in a special place?
-1
source to share