In last week post, I mentioned that I worked with navigation drawer and in this week post I will talk about navigation drawer and we will work out a simple example on how to implement a navigation drawer in your app.
According to Android Developers website “The navigation drawer is a UI panel that shows your app’s main navigation menu. It is hidden when not in use, but appears when the user swipes a finger from the left edge of the screen or, when at the top level of the app, the user touches the drawer icon in the app bar. ”
So what’s that means?
You most likely came across an app that uses navigation drawer before, such as Mishwar, Telegram, Gmail and lots of other apps, you just swipe from the left edge of your screen and your app navigation drawer will appear just like opening a drawer hence comes the name. When the drawer opens you will find a navigation menu items that allow you to navigate to different destinations in your app by just click on one of items. When you reach your destination you can choose to close the navigation drawer as we will see in our example. You can also add a button in the action bar at the top of the app and use it to open the navigation drawer.
As Linus Torvalds once said “Talk is cheap. Show me the code”. So I will jump right in the code.
We will build a simple app with only one activity containing a navigation drawer with a couple of menu items. The activity has a text and whenever we click on one of the navigation drawer menu items the text will change accordingly.
I will use Android Studio 3.0.1 in this example. The min SDK was 15 and targeted SDK was 27.
As usual open a new project and make sure you check include kotlin support option and choose empty activity.
At the time of writing this post I used the following dependencies:
Now you should sync your project.
Now you need to add the navigation drawer to the app layout, so first you need to change the root view to be DrawerLayout and inside the DrawerLayout you can add a layout for the main content of the activity UI, which mean what appears in the screen when the navigation drawer is hidden, and also you need to add another view for the navigation drawer contents, as shown below:
From the layout above in the NavigationView we set the layout gravity to start to support right to left (RTL) language.
Now its time to specify the menu items for our navigation drawer. To do that you need to specify a menu resource in the layout using app:menu as shown below:
Now we need to create the menu resource that contains the menu items itself. In res/menu file we create the file drawer_view.xml as shown below:
In res/values/strings.xml we add the following:
In MainActivity we will set a listener for the item click so when you click on an item (checked by item id) a certain action happens, in our case, it will display the title of the menu item in the text view in the screen and after that, the navigation drawer is closed. All that shown in the code below:
Just like that navigation drawer is set and ready and the output will look something like the following:
You will find this week example on Github.
I hope you benefit from this week post, and there are more about navigation drawer and I encourage you to check the Android Developers website and Material Design website for more details about navigation drawer.
If you have any questions, thoughts or feedback please lets us know at the comments. But for now, that’s it for this week.
Till next week.