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_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/camera"
        android:title="Camera"
        android:icon="@android:drawable/ic_menu_camera"
        app:showAsAction="always" />

</menu>
    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:
res\layout\layout_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@android:style/Widget.ActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">
    
    <Button
        android:id="@+id/button1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@android:drawable/ic_menu_camera" />
</RelativeLayout>
    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():
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item1 = menu.findItem(R.id.camera);

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

        View cameraMenu = menuLayout.findViewById(R.id.button1);
        cameraMenu.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            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() {
            @Override
            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:

Conclusions

    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!

Share


Previous post
« Prev Post
Next post
Next Post »