Android Navigation Controller Set Checked Item using Navigation Drawer Layout

Viewed 776

I have created Navigation Drawer Activity using Android Studio project template and added few menu items programatically and set the item selected listener to navigate to destination fragment.

Here what I am trying to do is clicking on each menu item added above should open the same destination fragment say HomeFragment but with different arguments so that I can reuse the layout.

So far it is working great with only one problem that the menu items are not highlighting correctly and Home item is always checked. I think this is because I have added the self link to home fragment. Is there any way to fix this?

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);

    // Adding menu items
    Menu menu = navigationView.getMenu();
    SubMenu labels = menu.addSubMenu("Labels");
    labels.add(R.id.group_labels, 201, 201, "Label 1").setIcon(R.drawable.ic_menu_gallery);
    labels.add(R.id.group_labels, 202, 202, "Label 2").setIcon(R.drawable.ic_menu_gallery);    

    navigationView.invalidate();

    appBarConfig = new AppBarConfiguration.Builder(R.id.home)
            .setDrawerLayout(drawer)
            .build();

    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfig);
    NavigationUI.setupWithNavController(navigationView, navController);

    // Navigation item click listener
    navigationView.setNavigationItemSelectedListener(item -> {
        if (item.getGroupId() == R.id.group_labels) {
            HomeFragmentDirections.ActionHomeSelf action = HomeFragmentDirections.actionHomeSelf();
            action.setLabel(item.getTitle().toString());

            navController.navigate(action);
        }

        navigationView.setCheckedItem(item.getItemId()); // Not working

        NavigationUI.onNavDestinationSelected(item, navController);

        drawer.closeDrawer(GravityCompat.START);

        return true;
    });
}

mobile_navigation.xml

<navigation 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/mobile_navigation"
app:startDestination="@+id/home">

<fragment
    android:id="@+id/home"
    android:label="@string/menu_home"
    android:name=".fragments.HomeFragment"
    tools:layout="@layout/fragment_home">

    <argument
        android:name="label"
        app:argType="string"
        android:defaultValue="default label" />

    <action
        android:id="@+id/action_home_self"
        app:destination="@id/home"
        app:launchSingleTop="false">
    </action>
    <action
        android:id="@+id/action_home_to_blank"
        app:destination="@id/blankFragment" />
</fragment>
<fragment
    android:id="@+id/blankFragment"
    android:name=".BlankFragment"
    android:label="fragment_blank"
    tools:layout="@layout/fragment_blank" />

</navigation>

activity_main_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/home"
        android:icon="@drawable/ic_menu_camera"
        android:orderInCategory="0"
        android:title="@string/menu_home" />

    <item
        android:id="@+id/blankFragment"
        android:icon="@drawable/ic_menu_camera"
        android:orderInCategory="0"
        android:title="@string/hello_blank_fragment" />
</group>

<item
    android:orderInCategory="200"
    android:title="@string/drawer_menu_group_labels">
    <menu>
        <group
            android:id="@+id/group_labels"
            android:checkableBehavior="single" />
    </menu>
</item>

<item
    android:orderInCategory="300"
    android:title="@string/drawer_menu_group_pages">
    <menu>
        <group
            android:id="@+id/group_pages"
            android:checkableBehavior="single" />
    </menu>
</item>
</menu>
0 Answers
Related