3

I am using the PopupMenu ,i have to fix the position of the pop up menu below the buttton which i click,but it show on the above the my button below is the code which i do.

private final static int ONE = 1;
private final static int TWO = 2;
private final static int THREE = 3;

PopupMenu popupMenu = new PopupMenu(context, convertView.findViewById(R.id.txtOverflowIconList_item_Egov));

popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "Item 1");
popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, "Item 2");
// popupMenu.getMenu().add(Menu.NONE, THREE, Menu.NONE, "Item 3");

popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {

        switch (item.getItemId()) {
            case ONE:
                Toast.makeText(context, "first ", 100).show();
                break;
            case TWO:
                Toast.makeText(context, "Two ", 100).show();
                break;
        }
        return false;
    }
});


holder.txtOverflowIcon.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        popupMenu.show();
    }
});

Below is th output what i get:

this is output what i get.

please help me.

gotwo
  • 663
  • 8
  • 16
Suraj
  • 550
  • 1
  • 5
  • 17

3 Answers3

1

I could fix that problem. The PopupMenu will reveal like the one in the ActionBar: https://stackoverflow.com/a/29702608/1185087

Community
  • 1
  • 1
user1185087
  • 4,468
  • 1
  • 30
  • 38
1

Instead of popupMenu.show(); You need to do like this

        popupMenu.show();
        if (popupMenu.getDragToOpenListener() instanceof ListPopupWindow.ForwardingListener) {
            ListPopupWindow.ForwardingListener listener = (ListPopupWindow.ForwardingListener) popupMenu
                    .getDragToOpenListener();
            listener.getPopup().setHorizontalOffset(x);
            listener.getPopup().setVerticalOffset(y);

            listener.getPopup().show();
        }

Calculate the position of anchor view or Button you want to display the popup to set the HorizontalOffset(x) and VerticalOffset(y).

karthiKeyan B
  • 91
  • 1
  • 7
0

pop up menu is always anchored to a view (button) and will always appear above or below the view it is attached to, depending upon the space available.

if you want to position the menu as your needs better use dialogs for this purpose or pop up window.

Junaid
  • 4,822
  • 2
  • 21
  • 27