Not able to target showcase on actionbar item
I am facing issues that I want to target the showcase on action bar item but don't know how it is done
ShowcaseConfig config = new ShowcaseConfig();
MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(this, UNIQUE_ID);
sequence.setConfig(config);
sequence.addSequenceItem(findViewById(R.id.action_search), "This is menu one", "GOT IT");
sequence.addSequenceItem(findViewById(R.id.action_add), "This is menu two", "GOT IT");
sequence.start();
This worked for me. Add this in onCreateOptionsMenu after inflating the menu and use menu id to find the view.
I use this Kotlin static function to get the view in a toolbar (works for action and menu items)
/**
id is the itemId, as set in menu.add() or XML id tag (R.id.action_add)
*/
fun getToolbarViewContainingMenuItem(toolbar: Toolbar, id: Int): View? {
for (i in 0 until toolbar.childCount) {
if (toolbar.getChildAt(i) is ActionMenuView) {
val menuView = toolbar.getChildAt(i) as ActionMenuView
for(childIndex in 0 until menuView.childCount) {
if(childIndex >= (menuView.childCount - 1)) return menuView.getChildAt(menuView.childCount - 1)
val child = menuView.getChildAt(childIndex) as? ActionMenuItemView ?: continue
if( child.id == id ) return child
}
}
}
return null
}
I had same problem and I was struggling with it for couple of days.Finally I got a clue from another library and just did it here and it was just calling toolbar.inflateMenu(menuid) in onCreate()
just like this:
protected void onCreate(){ ... toolbar = findViewById(R.id.my_toolbar_layout); toolbar.inflateMenu(R.menu.item_list_activity_menu); setSupportActionBar(toolbar); ... }
how to use this functionality in fragment. i'm trying to implement it but not able to complete
For fragment, this worked for me:
(activity as MainActivity).toolbar[1] returns the view for menu (3 dots). Index 0 returns view for Toolbar title.
ShowcaseConfig config = new ShowcaseConfig();
MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(this, UNIQUE_ID);
sequence.setConfig(config);
sequence.addSequenceItem(findViewById(R.id.action_search), "This is menu one", "GOT IT");
sequence.addSequenceItem(findViewById(R.id.action_add), "This is menu two", "GOT IT");
sequence.start();This worked for me. Add this inonCreateOptionsMenuafter inflating the menu and use menu id to find the view.
Doesn't work as findViewById returns null.
For fragment, this worked for me:
(activity as MainActivity).toolbar[1] returns the view for menu (3 dots). Index 0 returns view for Toolbar title.
That's the only working way for fragments, thank you!!!