Android Tip: Detecting long click at options menu item

    In Android, the options menu is where you should include actions and other options that are relevant to the current activity context, such as "Search," "Compose email," and "Settings". The option menu always located in Action Bar/Toolbar (from API 11):
    By reading my previous post, you've learned about detecting overflow button (3) clicked (opening/closing hidden menu). With other options menu item which always displayed on the Action Bar/Toolbar, handling it's click event is very easy through overriding onOptionItemSelected(MenuItem item) method but detecting it's long click event is not simple, we must set a custom view for this menu item and handle the view long click event.
    Now, with this tip, I will present a solution to solve this problem.

Default long click event of the option menu item

    Suppose we have a simple menu file with only one item like this:
<menu xmlns:android=""
        app:showAsAction="always" />

    With menu item which has icon property, when running, this icon will be displayed instead of item title and if you long click at this item, the item title will be shown by a Toast (like ImageView's contentDescription):

Custom option menu item actionView

    So, if you want to custom the long click event of menu item, the first work is creating a new layout for it:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
        android:background="@android:drawable/ic_menu_camera" />
    And now, you must set this layout as the MenuItem action view. Get the Button by call findViewId() and handle it's long click event (by use setOnLongClickListener() method). These works are perform in onCreateOptionsMenu():
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        MenuItem item1 = menu.findItem(;

        MenuItemCompat.setActionView(item1, R.layout.layout_menu);
        View menuLayout = MenuItemCompat.getActionView(item1);

        View cameraMenu = menuLayout.findViewById(;
        cameraMenu.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View view) {
                Toast.makeText(MainActivity.this, "Options menu item long clicked!", Toast.LENGTH_SHORT).show();
                return false;

        return super.onCreateOptionsMenu(menu);
    And this is our new output when long click at the camera icon in the options menu:
    There is an important note here: with this custom, you can not handle this item "normal click" event by override onOptionsItemSelected(MenuItem item). So, if you want to perform this work, please call setOnClickListener() for the Button inside onCreateOptionsMenu(Menu menu):
cameraMenu.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "option menu item clicked!", Toast.LENGTH_SHORT).show();
    And you'll get this output when click at the camera icon:


    With this small tip, I hope you've learned one more trick with options menu in Android to apply to your own work. For more posts about Android menu topic, please visit this tag link. Finally, as usual, please get my full project by click the button below!


Previous post
« Prev Post
Next post
Next Post »