Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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);
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 questionAsk a Question
731 491 924 answers to any question