How change position of popup menu on android overflow button?

user3266062 picture user3266062 · May 7, 2014 · Viewed 37.3k times · Source

I just like to implement somethings same as popup menu in the Gmail app, anchored to the overflow button at the top-right. for that I used the same code as google tutorial for android Android popup menu, but for me show pop menu on top of edge of actionbar not under that. If you notice on pop menu of gmail overflow you saw that popmenu take place at edge of actionbar.

This is the xml that I used for popup menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item1"
        android:title="lablab"/>
    <item
        android:id="@+id/item2"
        android:title="lablab"/>

</menu>

and at the follow is in my activity:

public void showFontSetting(View view) {
    PopupMenu popup = new PopupMenu(this, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
        // TODO Auto-generated method stub

            switch (item.getItemId()) {
                case R.id.item1:
                    Toast.makeText(Index.this,
                        "You Clicked : " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item2:
                    break;
            }
            return true;
        }
    });
}

Answer

user1185087 picture user1185087 · Apr 17, 2015

To overlap only, use this approach:

PopupMenu popupMenu = new PopupMenu(getContext(), this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);

To get a PopupMenu with a bright background and a detailed control over the offsets use this approach:

styles.xml

<style name="PopupMenuOverlapAnchor" parent="@style/Theme.AppCompat.Light">
   <item name="android:overlapAnchor">true</item>
   <item name="android:dropDownVerticalOffset">0dp</item>
   <item name="android:dropDownHorizontalOffset">0dp</item>
</style>

Code:

ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenuOverlapAnchor);
PopupMenu popupMenu = new PopupMenu(contextThemeWrapper, this);

Does not work anymore: Here a simple way to adjust the position of a PopupMenu. It positions the menu over its anchor view (overflowButton) like the menu in the action bar:

    PopupMenu popupMenu = new PopupMenu(context, overflowMenuButton);
    popupMenu.inflate(R.menu.my_menu);

    // Fix vertical offset
    overflowMenuButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            popupMenu.show();
            if (popupMenu.getDragToOpenListener() instanceof ListPopupWindow.ForwardingListener)
            {
                ListPopupWindow.ForwardingListener listener = (ListPopupWindow.ForwardingListener) popupMenu.getDragToOpenListener();
                listener.getPopup().setVerticalOffset(-overflowMenuButton.getHeight());
                listener.getPopup().show();
            }
        }
    });