I have a PopupWindow
that I want to anchor to an action bar menu item but can't seem anyway to get at the View
of the item that I want to click on.
This is required for the PopupWindow
showAsDropDown()
method.
Does anyone know how to achieve this?
Just call
findViewById
(your menu id) but remember not to call onOnCreate(...)
because the menus are not inflated at that point but call it inonOptionsItemSelected
You can get the view using the menu item id, by getting it in
onOptionsItemSelected
..Every where call:
If your code inside fragment don't use:
It will return null.
findViewById doesn't have to be run on onOptionsItemSelected in order to get the view of the action item.
however, do note that sometimes action items get to be inside the overflow menu so you might get a null instead.
so, how can you do it?
here's a sample code:
this was tested when using actionBarSherlock library, on android 4.1.2 and android 2.3.5 .
another alternative is to use a more extensive way , used on the showcaseView library, here .
I meet this issue as well, findViewById should be worked by my side, here is the sample code:
Please note, I make findViewById in the UI thread as menuitem' view is not inflated so the should be running in the UI thread