Does Android get a View link to an action bar item?

I use MaterialShowcaseView to show the user a quick tutorial the first time the application starts.

The problem I am facing is that I can get a link to the links to the elements in the elements of the action bar when the user selects this element 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 action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);
    }
}

The above code works.

Can I get a view link in onCreateOptionsMenu? Everything I tried gives me a reference to a null object for presentation.

I already tried this answer without succeeding.

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

Thanks guys.

EDIT:

Here is what I 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);
}

:

java.lang.NullPointerException: 'void android.view.View.getLocationInWindow(int []) '

+4
2

menu.findItem(R.id.youritemid), , onCreateOptionsMenu(Menu menu), , .

:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generate d method stub
    getMenuInflater().inflate(R.menu.main, menu);
    _menu = menu;
    return true;
}

Menu _menu; , MenuItem mi = _menu.findItem(R.id.itemid); .

Edit: , , menuitem , , , 3 5 , -, , , - , .

+4

:

public class ToolbarActionItemTarget implements Target {

private final Toolbar toolbar;
private final int menuItemId;

public ToolbarActionItemTarget(Toolbar toolbar, @IdRes int itemId) {
    this.toolbar = toolbar;
    this.menuItemId = itemId;
}

@Override
public Point getPoint() {
    return new ViewTarget(toolbar.findViewById(menuItemId)).getPoint();
}

:

showcaseView = new ShowcaseView.Builder(getActivity())
            .setTarget(Target.NONE)
            .setTarget(new ToolbarActionItemTarget(toolbar, R.id.action_view))

: https://github.com/amlcurran/ShowcaseView/releases/tag/5.3.0

`

0

All Articles