1

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();
vkislicins
  • 3,331
  • 3
  • 32
  • 62
  • what do you mean by this "I need an additional event to be triggered on the initial menu button click " – Tanim reja May 06 '15 at 15:12
  • Sorry if I was unclear - when the user hits the 3 dots AKA settings menu on the toolbar, I need to catch that event and do some stuff. At the moment I can only catch an event of the user selecting one of the settings items – vkislicins May 06 '15 at 15:17
  • what you want to do by catching that event? do you want to change menu options conditionally ? – Rajen Raiyarela May 06 '15 at 15:24
  • nope, I have a requirement to send a Google Analytics event, so I just need a hook into the event.. – vkislicins May 06 '15 at 15:26
  • I suggest you read this post, i think you need a menu option listener for handle that [http://stackoverflow.com/questions/23806383/actionbars-overflow-menu-open-close-listener][1] [1]: http://stackoverflow.com/questions/23806383/actionbars-overflow-menu-open-close-listener – encastellano May 06 '15 at 15:46

1 Answers1

3

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;
}
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • If you want apply changes dinamically in your menu items, you should overrride this function. – encastellano May 06 '15 at 15:35
  • Yes but questioner actually wants to write analytics code for when user click on overflow icon, so this method will always be called for amending the menu options, but as did not want to do this so can simply return true from here but before returning can write there code for analytics. – Rajen Raiyarela May 06 '15 at 15:39
  • Yep, i write another answer. Thanks for the aclaration – encastellano May 06 '15 at 15:47
  • @vkislicins I think you should use a menu listener for handle when menu is open or closed . Read this post http://stackoverflow.com/questions/23806383/actionbars-overflow-menu-open-close-listener – encastellano May 06 '15 at 15:57
  • @encastellano agreed. I tried it as it seemed like a cleaner approach, but it seems to be firing multiple times? close is firing about 5-6 times and open is firing at least twice (maybe once for each of my overflow items?). Not sure what's happening there but I've already spent too much time on this today :) – vkislicins May 06 '15 at 16:05
  • Sorry i cannot test now, i read about this, if i find some i tell you. – encastellano May 06 '15 at 16:07
  • onPrepareOptionsMenu only fired if I click hard menu button, if I click ... in toolbar, it does not. – Nam Vu Mar 19 '16 at 10:09
  • For question it was appropriate solution. If you want to get onPrepareOptionsMenu called you can call invalidateOptionsMenu() inside your code, when you want the menus to be reset. – Rajen Raiyarela Mar 19 '16 at 12:26