Here we will learn navigation drawer in android with examples and how to implement android sliding menu using navigation drawer to show the apps main navigation options from left edge of the screen with examples.
In android, Navigation Drawer is a panel that displays the app main navigation options on the left edge of the screen like sliding menu. The navigation drawer is hidden most of the time, but it is revealed when the user swipes a finger from the left edge of the screen or, while at the top level of the app, the user touches the app icon in the action bar.
The Navigation Drawer slides in from the left and contains the navigation destinations for our app. While creating the android application if we select Navigation Drawer Activity, automatically the navigation drawer menu will be created with all required sliding menu options.
Now we will see how to create navigation drawer sliding menu using Navigation Drawer Activity in android application with example.
Following is the example of creating navigation drawer sliding menu in android application.
Create a new android application using android studio and give names as NavigationDrawerExample like as shown below.
Now we need to select the form factors which we need for our app. In case if you're not sure what you need, just select Phone and Tablet and then click Next like as shown below.
Now select the Navigation Drawer Activity in 'Add an activity to Mobile' dialog and click Next like as shown below.
Customize the activity by entering activity name, layout name and title as prompted. In case if default values are fine, then click Finish like as shown below.
Once project created, automatically all the required details will be added in project to create the navigation drawer sliding menu like as shown below.
If you observe above project structure, android studio automatically created all the required files to create navigation drawer sliding menu.
Now open activity_main.xml file from \res\layout folder path and it will contain the code like as shown below.
<?xml version="1.0" encoding="utf-8"?>
Now open your main activity file MainActivity.java from \java\com.tutlane.audioplayerexample path and write the code like as shown below
If you want to modify the default navigation drawer sliding menu, then you need to customize the code based on your requirements.
When we run above program in android studio we will get the result like as shown below.
If you observe above result, the Navigation Drawer slides in from the left and contains the navigation destinations for our app. We can customize the navigation drawer by changing the default things based on our requirements.
This is how we can implement navigation drawer sliding menu in android applications based on our requirements.