Tom Tom -4 years ago 73
Android Question

Get MenuItem's View reference for TapTargetView

I am trying to use TapTargetView for the menu items, but I can't get the view of it.

My code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);

new TapTargetSequence(this)
.targets(
TapTarget.forView(menu.findItem(R.id.add).getActionView(), "Gonna"))

.listener(new TapTargetSequence.Listener() {
// This listener will tell us when interesting(tm) events happen in regards
// to the sequence
@Override
public void onSequenceFinish() {
// Yay
}

@Override
public void onSequenceStep(TapTarget lastTarget, boolean targetClicked) {

}


@Override
public void onSequenceCanceled(TapTarget lastTarget) {
// Boo
}
});


return true;
}


Error:


java.lang.IllegalArgumentException: Given null view to target


How can I resolve this issue?
I've tried adding
android:actionViewClass
to xml file, but no luck.

Answer Source

Use TapTarget.forToolbarMenuItem insted of TapTarget.forView

Change the code like this...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);

new TapTargetSequence(this)
        .targets(
                TapTarget.forToolbarMenuItem(toolbar,R.id.add, "Gonna"))

        .listener(new TapTargetSequence.Listener() {
            // This listener will tell us when interesting(tm) events happen in regards
            // to the sequence
            @Override
            public void onSequenceFinish() {
                // Yay
            }

            @Override
            public void onSequenceStep(TapTarget lastTarget, boolean targetClicked) {

            }


            @Override
            public void onSequenceCanceled(TapTarget lastTarget) {
                // Boo
            }
        });


return true;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download