Android navigation Drawer Tutorial and Example in Kotlin

Android navigation Drawer: is a UI panel that, it shows main navigation within an app like Gmail app has a setting, inbox, chat etc option in navigation drawerIt’s hidden by default, the user has to slide in from the side or form the top level of the app, the user touches the drawer icon in the app bar to open android navigation.

Android navigation Drawer Tutorial and Example in Kotlin

In this tutorial, you will learn the following:

  1. What is Android navigation Drawer
  2. An Example of navigation drawer in kotlin with coding

How to add Navigation Drawer in Android. Let’s starts.

Add Dependencies

Add the following dependencies to your apps module’s build.gradle file. It’s updating time to time, always add the latest one.

Adding Toolbar to your layout

You Have to remove ActionBar Theme and Add Toolbar

Then open your “values/style.xml” file and set the app theme to one without the action bar, such as Theme.AppCompat.Light.NoActionBar:

Here we are using theme:@style/ThemeOverlay.AppCompat.Dark.ActionBar, On the light app theme. You can use any other ThemeOverlay.

Like Light Toolbar on Dark app Theme @style/ThemeOverlay.AppCompat.ActionBar

Set the toolbar as the action bar

Add this in Activity class.

Add the nav drawer button

A setting button on App bar(ToolBar).

Let’s Build Android navigation Drawer example in kotlin:

We will implement Navigation Drawer and basic functionally in this example. In the next Tutorial, we will learn the use of activity and fragments.

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Add Dependencies in the apps module’s build.gradle file
Step 3. Create “drawer_view.xml” menu

if you don’t have any menu resource file then create first “Android Resource directory” then create resource file drawer_view.xml”

Then add the following code,

Note: we are using String directly, for good practice always add in a value/strings.xml resource file. And we are using a common launcher image in every item for simple example, you can use as per your requirements.

Material icon download link:

Step 4. Add following code in “activity_main.xml” layout file
Step 5. Open your “values/style.xml” and set the app theme

such as Theme.AppCompat.Light.NoActionBar:

Step 6. Open the “MainActivity.kt” and add following code
Step 7. Now Run the application, in an emulator or On your android device

Output screenshot Android Navigation Drawer :

Android navigation Drawer Tutorial and Example in Kotlin output

Download source code Android Navigation Drawer in kotlin

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)



Coding in Kotlin

3 thoughts on “Android navigation Drawer Tutorial and Example in Kotlin

    1. navigationView.getMenu().getItem(0).setChecked(true);

      You have to store the value and pass in the item.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.