Add the Navigation Drawer

Kotlin and Android Development featuring Jetpack — by Michael Fazio (72 / 125)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 9 Navigate via Navigation Drawer | TOC | Add Fragments (Standing s and Single Team) 👉

We likely won’t need any additional dependencies here for the nav drawer since we added the androidx.navigation library last chapter and apps now by default include the Material Design Components library. If either are missing, though, add them now. We can then jump over to activity_main.xml to add in the nav drawer.

Update activity_main.xml

From a UI perspective, we’re adding in the nav drawer to our existing MainActivity layout, and we’re additionally going to be changing the layout a decent amount. The parent view of the screen is now going to be a <DrawerLayout> with a <Toolbar> and a <FragmentContainerView> inside:

​ <androidx.drawerlayout.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:context=​".MainActivity"​>

​ ​<!-- Contents are in here. -->​

​ </androidx.drawerlayout.widget.DrawerLayout>

The <Toolbar> and <FragmentContainerView> are both wrapped in a <LinearLayout> and are together taking up the entire…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.