2020-07-15-Android-Kotlin-NavDrawer

build.gradle (Module: app)

dependencies {
    ...
    implementation 'com.google.android.material:material:1.1.0'
    ...
}

Step 1: Add Layout files.

  • app/res/layout/content_main.xml - which will have contents of main activity

  • app/res/layout/nav_header.xml - which will contain navigation header

Step 2: Update styles

  • Open app/res/styles.xml

  • Add following style within <resources> tag.

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Final styles.xml should look like this.

Step 3: Add a menu for Drawer Menu.

  • Add file app/res/menu/nav_menu.xml

If menu folder doesn't exists, please create it under app/res

Step 4: Remove ActionBar from MainActivity

  • Open app/manifests/AndroidManifest.xml

  • Add theme attribute to MainActivity as illustrated below.

You can run this on Emulator / Device to make sure that ActionBar is now hidden.

Step 5: Add DrawerLayout to MainActivity's layout file.

DrawerLayout will contain just two elements.

  • Main container

  • Drawer Menu

In Drawer Menu we can provide both

  • header Layout

  • Menu to show.

Here is how xml would look like of activity_main.xml

Step 6: Design for DrawerMenu - header

  • I'll add an imageview and two text views for header of drawer menu.

  • Something like as follows.

  • app/res/layout/nav_header.xml

Preview Image

Step 7: Menu for DrawerMenu

  • I've added few dummy menu items. Please modify it as per your need.

  • app/res/menu/nav_menu.xml

Step 8: Finish design for Main Content

  • I've added few dummy elements. Please modify it as per your need.

  • Make sure you add AppBarLayout

  • app/res/layout/content_main.xml

Step 9: MainActivity.kt

Access Drawer Menu & add listners

Last updated

Was this helpful?