2

I use ShowcaseView like this:

ActionItemTarget target = new ActionItemTarget(this, R.id.menu_search);

ShowcaseView sv = new ShowcaseView.Builder(this, true)
                .setTarget(target)
                .setContentText("Press this to search")
                .setStyle(R.style.CustomShowcaseTheme)
                .build();

But when I start app the first, ShowcaseView highlights home button. When I try to start app again, correct ActionBar item is showcased. My app doesn't use compatibility libraries like ActionbarSherlock.

Any idea why this may be happening?

ligi
  • 39,001
  • 44
  • 144
  • 244
  • What do you mean by "start app again". Do you force close it and open it or you reopen it calling onPause + onResume? Also when are you calling this code? Because `R.id.menu_search` may not be available yet at that time – Pedro Oliveira Oct 01 '14 at 15:31
  • @PedroOliveira I was calling it in onCreate(). Obviously that's a wrong place. –  Oct 01 '14 at 15:43
  • The best place to use this code would be on onCreateOptionsMenu and even there `R.id.menu_search` is not pointing to a view because it hasn't been inflated yet so you will have to find a way to do that there without using the id. – Pedro Oliveira Oct 01 '14 at 15:46
  • @PedroOliveira maybe you can post an answer, so that I can accept it. –  Oct 01 '14 at 17:30

2 Answers2

3

As requested:

The problem is that you are trying to retrieve an id for a view that is not in the hierarchy yet. According to the activity view cycle the menu is only created after onCreate. The best solution is to use onCreateOptionsMenu but even here R.id.menu_search will not return a valid id because the menu hasn't been created yet. Since the order of events has changed in API lvl 16 I suggest you hack it a little bit. You can create a handler on your onCreate() and execute a postDelayed runable. The code will be executed after the menu has been designed. On that runable you can retrieve the view for R.id.menu_search and highlight it.

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
0

I will try to present sample code here:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    new ShowcaseView.Builder(this)
            .withMaterialShowcase()
            .setTarget(new ToolbarActionItemTarget(this.toolbar, R.id.menu_search))
            .setContentText("Here's how to highlight items on a toolbar")
            .build()
            .show();

    return true;
}

Note: toolbar declare as member of the class.

Shashidhara
  • 665
  • 6
  • 22