vkislicins
vkislicins

Reputation: 3391

Android Toolbar menu button click event

I have a simple Toolbar in my fragment. The Toolbar has a menu associated to it (overflow menu under the 3 dots icon) and everything works pretty well - I get my click events when one of the menu items is selected.

However, I need to do something when the initial settings menu is clicked, not when one of the menu items are selected. Any ideas how to do that? I'm not specifying the menu manually - just using the one inbuilt into the Toolbar so I can't just do a find and onClick...

My current toolbar code:

mUIToolbar = (Toolbar) view.findViewById(R.id.toolbar);
ActionBarActivity activity = (ActionBarActivity) getActivity();
activity.setSupportActionBar(mToolbar);
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
activity.supportInvalidateOptionsMenu();

Upvotes: 1

Views: 4626

Answers (1)

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5634

In that case you can write your analytics code inside onPrepareOptionsMenu method. This method is called everytime before displaying menu options. You can override this method in your Activity class;

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    //Analytics code will go here.
    return true;
}

Upvotes: 3

Related Questions