deano2390 / MaterialShowcaseView

A Material Design themed ShowcaseView for Android
Apache License 2.0
2.72k stars 526 forks source link

Not able to target showcase on actionbar item #142

Open goswamiji0 opened 6 years ago

goswamiji0 commented 6 years ago

I am facing issues that I want to target the showcase on action bar item but don't know how it is done

vishnusp commented 6 years ago

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.

btraas commented 6 years ago

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
    }
ali73 commented 6 years ago

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); ... }

kumar5572 commented 5 years ago

how to use this functionality in fragment. i'm trying to implement it but not able to complete

murdak commented 2 years ago

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.

murdak commented 2 years ago

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.

Doesn't work as findViewById returns null.

DonMarv00 commented 1 year ago

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!!!