Z
Z
ZAndrew2015-09-09 20:43:39
Android
ZAndrew, 2015-09-09 20:43:39

How to set where PopupMenu appears on the screen?

You need to set the coordinates of the pop-up menu, because by itself, it appears somewhere in the back of the interface, namely in the upper left corner. In the vastness of the world, I found an example of such a solution:

import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.ListPopupWindow.ForwardingListener;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;

........

final Button menuButton = (Button) vView.findViewById(R.id.OptionsButton);
    final PopupMenu popupMenu = new PopupMenu(mainAct, vView, Gravity.CENTER);
    popupMenu.inflate(R.menu.list_manager_context_menu);
    popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener(){
                menuButton.setOnClickListener( new OnClickListener() {
      @Override
      public void onClick(View view) {
          
                 ListPopupWindow.ForwardingListener  listener =(ForwardingListener)               
                 popupMenu.getDragToOpenListener();
    		         listener.getPopup().setVerticalOffset(-menuButton.getHeight());

             popupMenu.show();
      }
    });
    return vView;
  }

But my listener.getPopup() returns null.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
ZAndrew, 2015-09-10
@ZAndrew

It turned out that everything is quite simple - just pass the button to which I want to bind the menu to the PopupMenu constructor:

final Button menuButton = (Button) vView.findViewById(R.id.OptionsButton);
final PopupMenu popupMenu = new PopupMenu(mainAct, menuButton , Gravity.CENTER);

V
Vasilij Aleksashin, 2015-09-10
@TerraMorf

The alignment is set in the PopupMenu constructor. You need to look at the markup, or rather the binding of elements in XML

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question