28

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?

Romain Guidoux
  • 2,943
  • 4
  • 28
  • 48
Kyros
  • 459
  • 2
  • 5
  • 11

5 Answers5

45

You can get the view using the menu item id, by getting it in onOptionsItemSelected ..

findViewById(R.id.menu_item);
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • 2
    No this doesn't work I tried that first of all and it just returns a null pointer. But then I wouldn't expect it anyway as the item on the action bar is a MenuItem which doesn't seem to be a View. So I am trying to get at the View inside that MenuItem – Kyros Nov 12 '12 at 13:39
  • depends where you are calling it, you should call it inside the activity to get the view then pass it to your popup window..it's working for me – Nermeen Nov 12 '12 at 13:41
  • I tried to find the view in OnCreate, where are you calling it? – Kyros Nov 12 '12 at 13:43
  • in my case onOptionsItemSelected – Nermeen Nov 12 '12 at 13:44
  • 4
    This works great if you find the view in onOptionsItemSelected as you suggested, thanks! – Kyros Nov 12 '12 at 13:55
  • 2
    findViewById doesn't have to be run on onOptionsItemSelected in order to get the view of the action item. i've added a new answer for this. – android developer Aug 25 '13 at 09:25
  • Can somebody please describe me how can solve same problem. i can't get any Idea how you all done it. please help. – Chirag Nagpal Jan 09 '14 at 12:11
  • @Nermeen Hi , any ideas how to findViewById on a CAB action ? I have a custom CAB in my app and I want to show ListPopupWindow with the anchor view as one of the actions on the cab, I just can't seem to get the view using findViewById when the action mode starts or onPrepareActionMode it is always null. – EviatarS Nov 13 '14 at 10:47
  • This should work as long as the item's view has been initialized. See `ActionMenuItemView#initialize`. If you depend on this early in the activity life, you may need to put a breakpoint there to see when it is called. (in v21+. Not sure where the equivalent functionality was for < 21... – androidguy Aug 13 '17 at 04:41
39

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:

public boolean onCreateOptionsMenu(final Menu menu) {
  getSupportMenuInflater().inflate(R.menu.main, menu);
  new Handler().post(new Runnable() {
    @Override
    public void run() {
      final View menuItemView = findViewById(R.id.menu_action_item);
      ...

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 .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
2

Every where call:

activity.findViewById(R.id.menu_item);

If your code inside fragment don't use:

getView().findViewById(R.id.menu_item); 

It will return null.

Mahmoud
  • 2,683
  • 1
  • 30
  • 32
1

I meet this issue as well, findViewById should be worked by my side, here is the sample code:

private void setDrawable(final Menu menu) {
    if (menu == null || menu.size() == 0) return;

    Utils.postToUiHandler(new Runnable() {
        @Override
        public void run() {
            int size = menu.size();
            for (int i = 0; i < size; i++) {
                MenuItem menuItem = menu.getItem(i);
                if (menuItem != null) {
                    View view = activity.findViewById(menuItem.getItemId());
                    if (view != null) {
                        Drawable drawable = ThemeDrawables.get(ThemeWorker.Segment.DEFAULT).actionbarButton();
                        if (drawable != null && view.getBackground() != drawable) {
                            int sdk = android.os.Build.VERSION.SDK_INT;
                            if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                                view.setBackgroundDrawable(drawable);
                            } else {
                                view.setBackground(drawable);
                            }
                        }

                    }
                }
            }
        }
    });

}

Please note, I make findViewById in the UI thread as menuitem' view is not inflated so the should be running in the UI thread

1

Just call findViewById(your menu id) but remember not to call on OnCreate(...) because the menus are not inflated at that point but call it in onOptionsItemSelected

Amanth Rai
  • 191
  • 2
  • 12