Reputation: 21893
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()) {
...
Upvotes: 7
Views: 24160
Reputation: 1437
You could use PopupWindow instead. There you have many options for positioning:
popupWindow.showAsDropDown(View parent)
Will display below the parent ViewpopupWindow.showAsDropDown(View anchor, int xoff, int yoff)
Will show below the parent View but also applies the offsetpopupWindow.showAsDropDown(View anchor, int xoff, int yoff, int gravity)
like before but also gravitypopupWindow.showAtLocation(View parent, int gravity, int x, int y)
popup at any absolute positionUpvotes: 2
Reputation: 1510
You can change your PopupMenu's position by using the following attributes: gravity, dropDownHorizontalOffset and dropDownVerticalOffset
First set gravity to Gravity.END
popup.setGravity(Gravity.END);
Then change your dropdown-offsets by creating a style
<style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu">
<item name="android:dropDownHorizontalOffset">-4dp</item>
<item name="android:dropDownVerticalOffset">4dp</item>
</style>
If you want to overlap the anchor view use
parent="@style/Widget.AppCompat.PopupMenu.Overflow"
Lastly apply MyPopupMenu to your theme
<item name="popupMenuStyle">@style/MyPopupMenu</item>
Upvotes: 10
Reputation: 1468
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();
}
Upvotes: 16
Reputation: 2560
You can use ListPopupWindow for show List popup menu. There is functions for setting offsets (ListPopupWindow).
Or you can set offsets for your PopupWindow explicitly using this function
Upvotes: 0
Reputation: 75629
You cannot. As docs say:
A PopupMenu displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.
You may want to try to use PopupWindow perhaps, which allows such positioning.
Upvotes: 0