-2

I have a fragment. In it I have an overflow menu (three dots on toolbar). I need to highlight them via Material Showcase.

The trouble I face is that I can not obtain these "three dots" id. How to get or to set id of these three-dots-menu?

Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32
Pavel Sarpov
  • 73
  • 2
  • 13
  • The three dots is called the overflow icon. See [this question](http://stackoverflow.com/questions/9733312/changing-overflow-icon-in-the-action-bar), and [this one](http://stackoverflow.com/questions/29038861/how-to-change-toolbar-navigation-and-overflow-menu-icons-appcompat-v7) for how to change the icon, which is what I understood you want to do. – Nicolas Apr 17 '17 at 23:49
  • @NicolasMaltais It was not about an icon. It was about overlow id. I need it id to findViewById with it. – Pavel Sarpov Apr 18 '17 at 00:21
  • Post your menu xml and fragment code – Ferdous Ahamed Apr 18 '17 at 07:15

2 Answers2

0

Just looked up the source code and this button doesn't have an id, it's added by code as needed. If you just want to highlight it, I strongly suggest you to only change the icon, or its color, with a style or theme.

If you still want to get that view then you might try my (untested) code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  // That's your toolbar here
try {
    // Toolbar has a private field mMenuView. Get it.
    Field menuViewField = Toolbar.class.getDeclaredField("mMenuView");
    menuViewField.setAccessible(true);
    ActionMenuView menuView = (ActionMenuView) menuViewField.get(toolbar);

    // ActionMenuView has a private field mPresenter. Get it.
    Field menuPresenterField = ActionMenuView.class.getDeclaredField("mPresenter");
    menuPresenterField.setAccessible(true);
    Object menuPresenter = menuPresenterField.get(menuView);

    // ActionMenuPresenter has a private field mOverflowButton. Go get your button.
    Class<?> menuPresenterClass = Class.forName("android.widget.ActionMenuPresenter");
    Field overflowBtnField = menuPresenterClass.getDeclaredField("mOverflowButton");
    overflowBtnField.setAccessible(true);
    ImageButton overflowBtn = (ImageButton) menuPresenterField.get(menuPresenter);

} catch (Exception e) {
    Log.d(TAG, "Couldn't find overflow button", e);
}

Will probably fail but try it anyway. Only works with a Toolbar, not an ActionBar. If it works you probably shouldn't use it, because if the field name changes in new version or is different in other APIs, it won't work. Also if you try getting an overflow button that doesn't exist it will fail. (still worth upvote for the effort :)

Good luck.

Nicolas
  • 6,611
  • 3
  • 29
  • 73
0

you should be able to keep a reference to the overflow view from

private View view;

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem actionViewItem = menu.findItem(R.id.your_menu_item);
    view = MenuItemCompat.getActionView(actionViewItem);
    return super.onPrepareOptionsMenu(menu);
}

then target that view with showcase

new MaterialShowcaseView.Builder(this)
                    .setTarget(view)
                    .show();
Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32