简体   繁体   中英

Handle clicks on menu item in toolbar in android

How to handle clicks from a toolbar with Navigation Component in android? I have a MaterialToolbar in an activity with a menu in xml, the menu shows up and accepts clicks but no action is executed. This is how I initialize the navigation component:

binding = DataBindingUtil.setContentView(this, R.layout.activity_home);
navController = Navigation.findNavController(this, R.id.fragment_container);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
    .setDrawerLayout(binding.activityHome)
    .build();
NavigationUI.setupWithNavController(binding.navigationView, navController);
NavigationUI.setupWithNavController(binding.toolBar, navController, appBarConfiguration);

This is my Toolbar menu , it has only one item that will open a DialogFragment :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/dialog_account"
        android:icon="@drawable/icon_account"
        android:title="@string/dialog_account"
        app:showAsAction="always" />
</menu>

Then this is the dialog in the navigation_graph

   <dialog
        android:id="@+id/dialog_account"
        android:name="my.package.name.dialogs.DialogAccount"
        android:label="@string/dialog_account"
        tools:layout="@layout/dialog_account" />

This is the activity layout, nothing fancy there:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data></data>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/activity_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="end">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimaryDark"
                app:layout_constraintBottom_toTopOf="@+id/fragment_container"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <com.google.android.material.appbar.MaterialToolbar
                    android:id="@+id/tool_bar"
                    style="@style/toolbar_style"
                    app:menu="@menu/toolbar" />
            </com.google.android.material.appbar.AppBarLayout>

            <fragment
                android:id="@+id/fragment_container"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="@dimen/null_dimen"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/app_bar"
                app:navGraph="@navigation/nav_graph" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigation_view"
            style="@style/navigation_view" />
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

The Activity layout has a NavigationView and a MaterialToolbar , the navigation from the NavigationView works fine, but I can't find a way to open my dialog from the Toolbar even though it seems like is taking clicks but doing nothing. I have tried with onCreateOptionsMenu but it does not get triggered, guess the Navigation Component is working but my dialog is not opening on touching of the menu.

What am I missing here, any hand?

To handle on click on Toolbar simply use "setOnMenuItemClickListener" by providing id of the toolbar.

  mToolbar= findViewById(R.id.main_tollbar);
    mToolbar.inflateMenu(R.menu.game_menu);
    mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()){
                case R.id.search_menu:
                    Toast.makeText(MainActivity.this,"Search Successful",Toast.LENGTH_LONG).show();
                    break;
                case R.id.menus_logout:
                    Toast.makeText(MainActivity.this,"Logout Successful",Toast.LENGTH_LONG).show();
                    break;
                case R.id.acc_settings:
                    Toast.makeText(MainActivity.this,"Setting Successful",Toast.LENGTH_LONG).show();
                    break;
                case R.id.about_menu:
                    Toast.makeText(MainActivity.this,"About Successful",Toast.LENGTH_LONG).show();
                    break;
                case R.id.feedback_menu:
                    Toast.makeText(MainActivity.this,"Feedback Successful",Toast.LENGTH_LONG).show();
                    break;
            }
            return true;
        }
    });

If you are using the Toolbar (without using setSupportActionBar(toolbar); ) you have to use:

NavigationUI.setupWithNavController(toolbar,navController,mAppBarConfiguration);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
      @Override
      public boolean onMenuItemClick(MenuItem item) {
           NavController navController = Navigation.findNavController(context, R.id.nav_host_fragment);
           return NavigationUI.onNavDestinationSelected(item, navController);
       }
});

In your case it is enough since the id in the menu matches with the id in the navigation graph. In general you can also use:

public boolean onMenuItemClick(MenuItem item) {
      Navigation.findNavController(view).navigate(R.id.action...);
      //....
}

Final note: the method onCreateOptionsMenu is only triggered if your are using an ActionBar .

For Navigation component you can use inflator

val view-inflater.inflate(R.Layout.fragment_main, container, false)

view.textview1.setonClickListener{


Navigation.findNavController(view).navigate(R.id.settings)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM