javaandroidandroid-toolbarandroid-optionsmenu

Overflow menu not working on toolbar in activity


I have setup a toolbar in MainActivity. I'm able to create the options menu. But, when I click on the item nothing happens. I want to open a fragment on the click of the menu item. I'm using app navigation patterns.

I tried setting up toolbar as supportActionToolbar but it doesn't work. I tried to inflate menu on the toolbar and then use toolbars's setOnMenuItemClick listener but that also doesn't seem to work.

Here's the code:

MainActivity.java

Toolbar toolbar = binding.toolbar;
toolbar.setTitle("Howdy John Doe !!");
toolbar.setSubtitle("Mumbai");
toolbar.setSubtitleTextColor(getResources().getColor(R.color.white, getTheme()));
toolbar.setTitleTextColor(getResources().getColor(R.color.white, getTheme()));
setSupportActionBar(toolbar);


@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==R.id.navigation_refine){
            Toast.makeText(this, "Hey there", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onOptionsItemSelected: Its working!");
        }
        return super.onOptionsItemSelected(item);
    }



activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/blue_200"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:navGraph="@navigation/mobile_navigation" />



</androidx.constraintlayout.widget.ConstraintLayout>




menu_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/navigation_refine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:showAsAction="always"
        app:actionLayout="@layout/custom_menu_layout"
        android:icon="@drawable/mdi_format_list_checks"
        android:title="Refine" />
</menu>

Solution

  • Add a return true statement to all your conditionals in onOptionsItemSelected(). It indicates that you have successfully handled a menu item.

    @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            if(item.getItemId()==R.id.navigation_refine){
                Toast.makeText(this, "Hey there", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onOptionsItemSelected: Its working!");
                return true;    // add this to your code
            }
            return super.onOptionsItemSelected(item);
        }
    
    

    If you are using a custom layout for your menu, it doesn't call onOptionsItemSelected() by default. So you need to implement a click listener. You can follow this tutorial for more details.

        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            final MenuItem menuItem = menu.findItem(R.id.navigation_refine);
            View rootView =  menuItem.getActionView();
    
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onOptionsItemSelected(menuItem);
                }
            });
    
            return super.onPrepareOptionsMenu(menu);
        }