7

I'm trying to create a custom popup menu that I would like to position on the overflow menu button of the actionbar (am using a Toolbar with setSupportActionBar() for this if it matters). I found out that this can be done with the setAnchorView() (from reading Custom Menu item in Overflow menu).

However I cant seem to figure out how to retrieve the overflow menu as a view (that I could use to set the anchor with).

Also I did try anchoring this to the parent layout of the activity itself but it showed up on the left top, and the height of the menu was equal to the height of the actionbar (which is not very useful).

Does anyone know how this can be achieved?

source.rar
  • 8,002
  • 10
  • 50
  • 82

3 Answers3

6

Instead of having an actual overflow menu you could "cheat" a little bit. Have an icon in your actionbar that looks like the overflow icon. You should set showAsAction to always on this MenuItem. OnClick of the overflow icon, you show a ListPopupWindow that's anchored to the MenuItem view. If the ListPopupWindow doesn't show up where you want it to, you can call ListPopupWindow.setHorizontalOffset() and ListPopupWindow.setVerticalOffset()

Andrew Orobator
  • 7,978
  • 3
  • 36
  • 36
  • Thanks. Will give this a shot and post back soon. :) – source.rar Mar 10 '15 at 11:24
  • Very good idea. But, when confronted with this problem, I ended up using a PopupMenu, which is much simpler to use than a ListPopupWindow, which on top of its complexity presents the drawback of having its dimensions specified in physical pixels. – Papa Smurf May 25 '19 at 19:04
5

I agree with Andrew Orobator in that it is most likely going to need to be "faked" but I would recommend adding an actual view to your Toolbar, instead of trying to deal with Menu items:

<?xml version="1.0" encoding="utf-8"?>
   <android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/toolbar"
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:minHeight="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:elevation="@dimen/elevation_med">

<ImageView
    android:id="@+id/overflow"
    android:layout_width="2dp"
    android:layout_height="2dp"
    android:background="@drawable/overflow_icon"
    android:layout_gravity="right"/>

</android.support.v7.widget.Toolbar>

In this way, you can directly get the view from the toolbar via toolbar.findViewById(R.id.overflow); or something similar, then set the view as your anchor. It means you would also have to set your own onClick listeners to it as it would no longer get onOptionsMenu() callbacks, but I think this would be the best way to handle your scenario.

Jawnnypoo
  • 993
  • 12
  • 18
1

I learnt how to get a view from a menuitem here: Android : Get view Reference to a Menu Item

  1. Call View v = findViewById(R.id.overflow) within your onOptionsItemSelected method.

  2. Use that view as your anchor in your popupmenu: PopupMenu popupMenu = new PopupMenu(this, v);

Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217