What I am trying to do is showing a PopupWindow pointing to the overflow icon (the three dots) on the Toolbar. So I need to get a reference to the View object with the id of the icon. But what is the id?
The PopupWindow is used to tell the users that there are new entries added to the overflow menu. And suggest users to check it out.
The overflow menu item doesn't have a resource id. I found the overflow view by traversing the toolbar. The debugger showed an id of
-1
and the Hierarchy Viewer showed no resource-id.Here is how I found the overflow view without a resource id:
Calling
getOverflowMenuButton(activity)
will returnnull
inonCreate
because the overflow menu isn't laid out yet. To get the overflow menu in onCreate I did the following:you want to create custom
DropDown
menu? consider this "native" wayor use
android:showAsAction="never"
in yourmenu.xml
. doc ofshowAsAction
attribute HERE. when one ofMenuItem
s have setnever
value then you will get overflow three-dot icon automatically and theseMenuItem
s will be hidding therealso you may try to use Hierarchy Viewer to investigate this id if really needed
I found a library called TapTarget and a function TapTarget.forToolbarOverflow(). It presents a solution: https://github.com/KeepSafe/TapTargetView/blob/master/taptargetview/src/main/java/com/getkeepsafe/taptargetview/TapTarget.java#L96
The way how it finds the overflow view is not neat but should be stable.
Instead of using expensive and complicated layout traversal to find the overflow menu, I have achieved showing the PopupWindow under the overflow menu by using the Toolbar view as anchor and setting gravity to Gravity.END:
You probably created a menu item xml in your menu ressources to have an overflow button, so you must use the id of the overflow button item that you specified in your menu xml