0

Currently I have three options in my bottom navigation and a navigation graph for them.

part of my mainActivity.xml file look like this:

<fragment
        android:id = "@+id/nav_host_fragment"
        android:layout_width = "match_parent"
        android:layout_height = "0dp"
        android:layout_weight = "1"
        android:name = "androidx.navigation.fragment.NavHostFragment"
        app:navGraph = "@navigation/nav_graph"
        app:defaultNavHost = "true" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id = "@+id/bottom_nav"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        app:menu = "@menu/bottom_nav" />

and In my mainActivity I have written this code,

navController = Navigation.findNavController(this, R.id.nav_host_fragment)
bottom_nav.setupWithNavController(navController)
NavigationUI.setupActionBarWithNavController(this, navController)

everything related to navigation is handled by jetpack navigation library. Now I want to add a Navigation drawer also and in drawer I want to add different menu items(not only three that are in bottom navigation), so I will add new menu resource file for navigation drawer, now how should I use navigation library for both bottom nav and nav drawer? I don't want to manually do Fragment transactions and work with fragment manager.

One approach that I can think of is adding all of the fragments in a single nav graph(which is currently used for bottom nav) and then use the same navController for nav drawer also but I am looking for a better approach.

Nitin Verma
  • 485
  • 4
  • 19
  • You can hook both a `BottomNavigationView` and `NavigationView` to the same `NavController`, there's nothing special you need to do. – ianhanniballake May 02 '19 at 17:37
  • @ianhanniballake I know I can hook both of them with nav controller, but what I want to ask is that how will I handle clicks on item of nav drawer? how will they replace fragment in nav_host_fragment? – Nitin Verma May 02 '19 at 17:43
  • That's what calling `navigation_view.setupWithNavController(navController)` does for you. Just the same as your bottom nav, as long as the ids of the menu items match ids of destinations in your graph, it'll handle the replacements for you. – ianhanniballake May 02 '19 at 18:03
  • I thought about doing it but it will result in too many fragments in my nav graph. can you think of a way by which I can use a different nav graph for nav drawer so that everything is clear and maintainable. – Nitin Verma May 03 '19 at 03:25
  • you should use one navigation graph. Use your navigation graph as a litmus test for your users: if you think you have too many screens, then users most certainly will as well. Note that sub screens of a particular feature can be part of nested graphs - those destinations don't need to directly be on your top level graph – ianhanniballake May 03 '19 at 03:50
  • nested graphs might solve my problem. I'll look into it, thanks ! – Nitin Verma May 03 '19 at 06:29

1 Answers1

0

The structure of drawer is you have to include activity INSIDE drawer layout. So in your case the implementation of the bottom navigation is you include bottom_navigation INSIDE main_activity ---> main_activity INSIDE drawer_layout

Code sample

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
        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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start">

    <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

SRC https://code.tutsplus.com/tutorials/how-to-code-a-navigation-drawer-in-an-android-app--cms-30263

Bo Z
  • 2,359
  • 1
  • 13
  • 31
  • how will I handle clicks on item of nav drawer? how will they replace fragment in nav_host_fragment? – Nitin Verma May 02 '19 at 17:44
  • @NitinVerma drawer has its own menu. Implementation of the listener is in MainActivity so there you are changing fragments / sending intents and what ever you want to do. – Bo Z May 02 '19 at 17:46
  • so I'll have to handle the transaction myself by using fragment manager? I was looking for a way to automate this task like in bottom nav but at the same time not put everything in the same nav graph. Is there a way to make a different nav graph for nav drawer and use it for navigation ? – Nitin Verma May 03 '19 at 03:23
  • @NitinVerma it is suppose to be separate. The holder for fragments and for drawer is activity. All implementation is there. Drawer has its own menu listener. So everything is separate implementing in activity – Bo Z May 03 '19 at 13:35