Search code examples
androidviewreferenceandroid-actionbar

Android get a View reference to action bar item?


I'm using MaterialShowcaseView to show the user a quick tutorial, when they first start the app.

The problem I'm having is that I'm only able to get a view reference to the items in my action bar items when the user selects that item in onOptionsItemSelected.

i.e.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.my_location:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            ShowcaseConfig config = new ShowcaseConfig();
            config.setDelay(500); // half second between each showcase view


            MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(this, SHOWCASE_ID);

            sequence.setConfig(config);

            sequence.addSequenceItem(findViewById(R.id.action_my_location),
                    "This is button one", "GOT IT");
            sequence.start();
            Toast.makeText(MapsActivity.this, "My location action press", Toast.LENGTH_SHORT).show();

            return true;

        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);
    }
}

The above code works.

Is it possible to get a view reference in onCreateOptionsMenu? Everything else I've tried gives me a null object reference for the view.

I've tried this answer too, to no success.

I should mention that for the actionBar I used android's action bar tutorial.

Thanks guys.

EDIT:

This is what I've tried to do now:

@Override
    public void invalidateOptionsMenu() {
        super.invalidateOptionsMenu();

    }
    MenuItem mi;
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        mi = menu.findItem(R.id.action_my_location);
        new MaterialShowcaseView.Builder(this)
                .setTarget(mi.getActionView())
                .setDismissText("GOT IT")
                .setContentText("This is some amazing feature you should know about")
                .setDelay(300) // optional but starting animations immediately in onCreate can make them choppy
                .singleUse("101010110") // provide a unique ID used to ensure it is only shown once
                .show();
        return super.onPrepareOptionsMenu(menu);
}

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.getLocationInWindow(int[])' on a null object reference


Solution

  • You can do menu.findItem(R.id.youritemid) to get the menu item, and you can get reference to menu object in your onCreateOptionsMenu(Menu menu) method then you can initialize a global variable with that menu object to use it anywhere.

    Here is some code:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        _menu = menu;
        return true;
    }
    

    I am initialize my global variable Menu _menu; with menu object and then i can do like this MenuItem mi = _menu.findItem(R.id.itemid); any where i want.

    Edit: Please take care that you are not calling anything on menuitem before the menu gets created, you can schedule a thread to wait for 3 to 5 secs or you can do it some other way, all you should worry about is whether the menu has been initialized or not.