Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the position of where my Popup menu pops up

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()) {
                    ...
like image 425
code511788465541441 Avatar asked Nov 18 '12 21:11

code511788465541441


People also ask

What display a pop up menu?

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.

How do I set the popup menu position in flutter?

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.

How do I show the pop up menu on Android?

To add programmatically: PopupMenu menu = new PopupMenu(this, view); menu. getMenu().

How do I pass custom layout to PopupMenu?

OnClickListener() { @Override public void onClick(View view) { PopupMenu popupMenu = new PopupMenu(Sample1. this, view); popupMenu. setOnMenuItemClickListener(Sample1. this); popupMenu.


1 Answers

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();
}
like image 66
maXp Avatar answered Oct 11 '22 16:10

maXp