I have a popup menu in a listview adapter for each item. The menu pops up on the left edge of the screen, how can I change it to be on the right
private void showPopupMenu(View v, final App app) {
PopupMenu popupMenu = new PopupMenu(context, v);
popupMenu.getMenuInflater().inflate(R.menu.quick_action_menu,
popupMenu.getMenu());
popupMenu
.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
...
Android Popup Menu: Android Popup Menu displays a list of items in a vertical list which presents to the view that invoked the menu and useful to provide an overflow of actions that related to specific content.
position property Null safetyoffset is used to change the position of the popup menu relative to the position set by this parameter. When not set, the position defaults to PopupMenuPosition. over which makes the popup menu appear directly over the button that was used to create it.
To add programmatically: PopupMenu menu = new PopupMenu(this, view); menu. getMenu().
OnClickListener() { @Override public void onClick(View view) { PopupMenu popupMenu = new PopupMenu(Sample1. this, view); popupMenu. setOnMenuItemClickListener(Sample1. this); popupMenu.
Better late than once =) This is my decision, allows you to set PopupMenu to the specified coordinates. The code is not very good but it works.
public void show(Activity activity, float x, float y)
{
final ViewGroup root = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);
final View view = new View(context);
view.setLayoutParams(new ViewGroup.LayoutParams(1, 1));
view.setBackgroundColor(Color.TRANSPARENT);
root.addView(view);
view.setX(x);
view.setY(y);
PopupMenu popupMenu = new PopupMenu(context, view, Gravity.CENTER);
popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener()
{
@Override
public void onDismiss(PopupMenu menu)
{
root.removeView(view);
}
});
popupMenu.show();
}
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