Friday August 21st, 2020

Kotlin Navigation Drawer

By Ebubekir Sezer

Helloo, when we look at the popular apps we can see that there is a menu coming from side of screen. I will try to do these menu in a Kotlin project. This menu named as Navigation Drawer in Android World. I open an empty kotlin project in Android  Studio.

In this project, we will use some libraries that we need to use for the data binding and navigation, So lets implement them. In the Build.Gradle, i enabled the databinding and then added to the Navigation library.

I will use fragments instead of the using activities. So i added the HomeFragment to my project and i will not make any design for this fragment because i just focus the navigation drawer. After the adding the fragment, I added the navigation to the my res folder and added the HomeFragment for the navigation.

After the adding the navigation, we need to make arrangements in activity_main file. For the making databinding, i wrapped the all the tag with layout tag. For the having the menu, i added the DrawerLayout tag and give it an id. I added a LinearLayout and inside of it i added the fragment. In the fragment, I added the android:name and give it to the navGraph and i also make the defaultNavHost true.

We did almost everything for the layout files. In the end of the Linear Layout, I added the Navigation View and inside of the navigation view i added the menu which i created in the menu in the res folder. In the menu i just added the two item.

After the making necessary things in the layout files, we need to arrange the MainActivity. In the MainActivity, i created the binding variable and setup the navigation drawer. I created an navController which is came from the fragment that we created and arrange the necessary setup options.

As you can see, we created a menu. We can customize this menu by adding some images or changing the designs. I will add a header, for that i created an layout and then add this layout to the NavigationView headerLayout property.

We can more customize the menu, if we want. You can reach the project by clicking here. If you have any question or comments, you can reach me via e-mail or comments.

References: https://developer.android.com/guide/navigation/navigation-ui