Android Kotlin – Navigation and navigation drawer

First, you need to add the material support library dependency at the Build.gradle(App)

implementation "com.google.android.material:material:$supportlibVersion"

, <span>Android Kotlin – Navigation and navigation drawer</span>, KandZ

Because we use Navigation, you need to add new destinations to the Navigation layout and specify an ID for every destination.

Then you have to create a menu resource file. Right-click res click New and then Android resource file. Specify a file name and select Menu as the resource type. Click OK.

, <span>Android Kotlin – Navigation and navigation drawer</span>, KandZ

Now edit the navigation_drawer_menu. Add the items you want the menu to have. Check the example below. Just remember the id of the item should be the same id you used when you added new destinations at the Navigation layout.

, <span>Android Kotlin – Navigation and navigation drawer</span>, KandZ

Now open the activity layout and edit it. You have to have the DrawerLayout as the outer counter of the main layout.

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
.
.
.

</androidx.drawerlayout.widget.DrawerLayout>

, <span>Android Kotlin – Navigation and navigation drawer</span>, KandZ

Before the end of the DrawerLayout you must add the NavigationView. The app:menu is the place where you gonna specify the navigation_drawer_menu you created before.

 <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_drawer_menu"
        />

</androidx.drawerlayout.widget.DrawerLayout>

If you want to add a header to the NavigationView then you have to create a header layout and add:

app:headerLayout="@layout/header"

Now you need to edit the activity that has the NavigationDrawer. You need to find the Drawer Layout

private lateinit var drawerLayout: DrawerLayout

#and in onCreate initiate it
drawerLayout = findViewById(R.id.drawerLayout)

Find the navController and setup the drawer layout at onCreate

val navController = Navigation.findNavController(this,R.id.myNavHostFragment)

NavigationUI.setupActionBarWithNavController(this, navController,drawerLayout)

You need to inform the Navigation UI about the navigation view

NavigationUI.setupWithNavController(findViewById<NavigationView>(R.id.navigationView), navController)

Now you have to override onSupportNavigateUp so you navigateUp with the drawerLayout

override fun onSupportNavigateUp(): Boolean
{
    val navController = Navigation.findNavController(this,R.id.myNavHostFragment)

    return NavigationUI.navigateUp(navController, drawerLayout)
}

, <span>Android Kotlin – Navigation and navigation drawer</span>, KandZ

 

 

 

37020cookie-checkAndroid Kotlin – Navigation and navigation drawerAndroid Kotlin – Navigation and navigation drawer

Related posts