How to Create App Drawer in Android TUTORIAL
The Material Design squad at Google defines the functionality of a navigation drawer in Android as follows:
The navigation drawer slides in from the left and contains the navigation destinations for your app.
An example of a pop Android app that implements the navigation drawer menu design is the Inbox app from Google, which uses a navigation drawer to navigate different application sections. You can bank check it yourself past downloading the Inbox app from the Google Play store if you don't already take it on your device. The screenshot beneath shows Inbox with the navigation drawer pulled open.



The user can view the navigation drawer when they swipe a finger from the left edge of the activity. They can besides find it from the dwelling action (the summit level of the app) by tapping the app icon (also known as the Android "hamburger" menu) in the activeness bar.
Note that if you have many unlike destinations (more than six, say) in your app, it's recommended that y'all use a navigation drawer menu design.
In this postal service, you'll learn how to display navigation items inside a navigation drawer in Android. We'll cover how to use Jetpack navigation to perform this task. Equally a bonus, you'll also larn how to use the Android Studio templates feature to bootstrap your project with a navigation drawer chop-chop.
Prerequisites
To be able to follow this Android Studio navigation drawer tutorial, you'll demand:
- Android Studio 3.three or higher
- Kotlin plugin 1.i.51 or higher
- Java viii linguistic communication features
Create an Android Studio Projection
Fire up Android Studio and create a new projection (you lot can proper noun itNavigationDrawer
) with an empty activity calledMainActivity
. Brand sure to as well choose theKotlinlanguage.



Add Project Dependencies
Support for navigation requires some dependencies. Open the appbuild.gradle file and add together the following dependencies.
dependencies { def lifecycle_version = "ii.2.0" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" ... }
Also add the material library to the project.
dependencies { ... implementation "com.google.android.material:material:$version" ... }
Sync the project files for the changes to take outcome.
Create theDrawerLayout
To display the drawer icon on all destinations in our app, we will utilize theDrawerLayout
component. Open upmain_acivity.xml and addDrawerLayout
every bit the root view. The drawer layout will host two child views,NavHostFragment
andNavigationView
.
<?xml version="ane.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="commencement"> <!--TOOLBAR HERE--> <!--NavHostFragment HERE--> <!--NavigationView HERE--> </androidx.drawerlayout.widget.DrawerLayout>
Here we created aDrawerLayout
widget with the iddrawer_layout
. Thetools:openDrawer
property is used to display the navigation drawer toggle when the XML layout is open in Android Studio design view.
The official documentation says the following mostDrawerLayout
:
DrawerLayout
acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.
After adding theDrawerLayout
widget, we included a child layout,app_bar_main.xml which points to the toolbar layout.
<!--main_activity.xml--> <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <!--NavHostFragment HERE--> <!--NavigationView HERE--> </androidx.drawerlayout.widget.DrawerLayout>
Here is myapp_bar_main.xml resource file. This file has aCoordinatorLayout
, anAppBarLayout
, and aToolbar
widget.
<!--tool_bar_layout.xml--> <?xml version="one.0" encoding="utf-eight"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-car" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/Theme.NavigationDrawer.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:groundwork="?attr/colorPrimary" app:popupTheme="@way/Theme.NavigationDrawer.PopupOverlay" /> </com.google.android.material.appbar.AppBarLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
Create a Navigation Graph
Anavigation graph is an XML resource file that contains all of your app's destinations and actions, and these destinations are connected via deportment. Beneath is an example of a navigation graph showing five fragments.
.png)
.png)
.png)
To add together a navigation graph, right-click on the res directory and selectNew > Android Resource File. In the side by side dialog, selectNavigation as theResource Type, and clickOK. A new XML file, nav_graph.xml, will be created in the Navigation binder, equally shown below.



AddNavHostFragment
A navigation host fragment acts as a host for the app's fragments and swaps fragments in and out every bit necessary when the user moves from one destination to the other. These destinations accept to exist defined in the navigation graph.
Add togetherNavHostFragment
to themain_activity.xml file and reference thenavGraph
.
<!--main_activity.xml--> <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="showtime"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/nav_host_fragment" android:proper noun="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/nav_graph" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--NavigationView Hither--> </androidx.drawerlayout.widget.DrawerLayout>
Add Fragments to the Destination Graph
Fragments represent all the destinations of your app. In our instance, we volition add three fragments to the navigation graph. Right-click the navigation folder and open nav_graph.xml. To add a fragment, click onCreate New Destination and fill up out the rest of the details.



Repeat the aforementioned steps and create two additional fragments, the profile fragment and the settings fragment. Your navigation graph should now look similar this.



Add aNavigationView
Component
Finally, let's create aNavigationView
widget. The official documentation says the following aboutNavigationView
:
NavigationView
represents a standard navigation menu for application. The bill of fare contents can be populated by a menu resource file.
Open upmain_activity.xml and add together theNavigationView
.
<!--main_activity.xml--> <?xml version="i.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-car" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="truthful" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/nav_graph" /> </androidx.constraintlayout.widget.ConstraintLayout> <com.google.android.fabric.navigation.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="commencement" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:bill of fare="@menu/drawer_menu" /> </androidx.drawerlayout.widget.DrawerLayout>
In theNavigationView
XML widget, you can encounter that we added anandroid:layout_gravity
attribute with the value start
. This is used to position the drawer—y'all desire the navigation drawer carte du jour design to come out from the left or right (the first or terminate on platform versions that support layout management). In our ain case, the drawer will come out from the left.
We too included anapp:headerLayout
attribute, which points to@layout/nav_header_main
. This will add aView
as a header of the navigation carte.
Here is mynav_header_main.xml layout resources file:
<?xml version="one.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:gravity="bottom" android:orientation="vertical" android:theme="@fashion/ThemeOverlay.AppCompat.Dark"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="@cord/nav_header_title" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> </LinearLayout>
To include the menu items for the navigation drawer, we can use the aspectapp:card
with a value that points to a card resource file.
app:carte du jour="@menu/drawer_menu" />
Here is theres/card/drawer_menu.xml menu resources file:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:showIn="navigation_view"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/home" android:title="@string/menu_home" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/person" android:title="@string/menu_gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/settings" android:title="@string/menu_slideshow" /> </group> </card>
Hither nosotros have divers aMenu
using the<menu>
which serves equally a container for menu items. An <item>
creates aMenuItem
, which represents a single item in a menu. It's also important to note that the ids of the menu items correspond to the ids of the matching fragment.
Note that when showing the navigation listing items from a menu resource, we could utilize aListView
instead. But, past configuring the navigation drawer with a menu resources, we get the material design styling on the navigation drawer for gratuitous! If you lot used aListView
, you'd take to maintain the list and also style it to run across the recommended fabric design specs for the navigation drawer.
Initialization of Components
Next, we are going to initialize instances of all our components. Initialization is going to happen withinonCreate()
inMainActivity.kt.
TheAppBarConfiguration
object is used to manage the behavior of the navigation drawer button.
private lateinit var appBarConfiguration: AppBarConfiguration
First, we use the setSupportActionBar()
method to set the toolbar equally the app bar for the activity.
val toolbar: Toolbar = findViewById(R.id.toolbar) setSupportActionBar(toolbar)
Side by side, we set all fragments as meridian-level destinations, this means that they will remain in the dorsum stack when navigating.
// Passing each menu ID as a set of Ids because each // carte du jour should be considered as peak level destinations. appBarConfiguration = AppBarConfiguration(setOf( R.id.home_menu, R.id.profile_menu, R.id.settings_menu), drawerLayout)
The method setupActionBarWithNavController
automatically updates the championship in the action bar when the destination changes.
setupActionBarWithNavController(navController, appBarConfiguration)
Set up upwards the navigation drawer.
val navView: NavigationView = findViewById(R.id.nav_view) val navController = findNavController(R.id.nav_host_fragment) navView.setupWithNavController(navController)
Lastly, show the up button that appears at the top left of the app bar. This is done by integrating the navigation controller withe app bar using the onSupportNavigateUp
method.
The final code forMainActivity.kt should look like this.
package com.example.navigationdrawer // imports course MainActivity : AppCompatActivity() { private lateinit var appBarConfiguration: AppBarConfiguration override fun onCreate(savedInstanceState: Packet?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar: Toolbar = findViewById(R.id.toolbar) setSupportActionBar(toolbar) /// val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout) val navView: NavigationView = findViewById(R.id.nav_view) val navController = findNavController(R.id.nav_host_fragment) // Passing each card ID as a fix of Ids considering each // menu should be considered equally top level destinations. appBarConfiguration = AppBarConfiguration(setOf( R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) } override fun onSupportNavigateUp(): Boolean { val navController = findNavController(R.id.nav_host_fragment) return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp() } }
Testing the App
At this indicate, we can run the app!



Bonus: Using Android Studio Templates
At present that y'all've learnt about the APIs involved to create a navigation drawer, I'll show yous a shortcut that will make information technology faster next time. You tin can only use a template instead of coding a navigation drawer Activity from scratch.
Android Studio provides code templates that follow the Android design and development best practices. These existing code templates (available in Java and Kotlin) can help you rapidly kicking-beginning your projection. I such template can be used to create a navigation drawer activity.
I'll show yous how to use this handy feature in Android Studio.
For a new project, fire up Android Studio.
Enter the application name and click theNext button.
Yous can leave the defaults every bit they're in theTarget Android Devices dialog. Click theNext button once more.
In theAdd together an Activity to Mobile dialog, curl downwardly and selectNavigation Drawer Action.Click theNext push afterward that.
In the last dialog, y'all can rename the Activity name, layout name, or title if y'all desire. Finally, click the Finish button to take all configurations.
Android Studio has at present helped usa to create a project with a navigation drawer activity. Really absurd! Y'all're strongly advised to explore the code generated.
You can use templates for an already existing Android Studio projection too. Only go toFile > New > Activeness > Navigation Drawer Activeness.
Top Android App Templates With Navigation Drawers From CodeCanyon
The templates that come included with Android Studio are proficient for unproblematic layouts and making basic apps, but if you want to kicking-commencement your app even further, you might consider some of the app templates available from Envato Market.
They're a huge time-saver for experienced developers, helping them to cut through the slog of creating an app from scratch and focus their talents instead on the unique and customised parts of creating a new app.
Here are just a small-scale handful of the thousands of Android app templates available on CodeCanyon. If in that location's i that piques your interest, you tin easily get started past making a buy.
Grocery and Vegetable Commitment Android App with Admin Panel
If you or your customer accept a food commitment business, getting an app up and running is crucial. That's why you lot should consider this multi-store grocery service app template. It includes iii templates with stunning layouts and Android hamburger menus. There's no limit to the categories y'all can add, and you lot can likewise employ SMS and electronic mail social club notifications.
Universal: Total Multi-Purpose Android App
Buying the Universal Android app template is just similar downloading a Swiss Army knife. It can do it all, from WordPress and Facebook to Twitter and SoundCloud. In fact, there is a list of more than than 15 content providers that this template supports. Users can admission important information from the slick side menu pattern and easily make their way around their favorite sites.
MaterialX: Android Material Design UI Components 2.7
MaterialX is a recommended download for any app developer. It includes more than than 315 unique UI components across more than than 31 categories. Create stunning Android side menu designs, buttons, dialog boxes, and more from this single download. If you want a quick way to add some much-needed style to your new project, get this template.
Universal Android WebView App
Do you take content hosted online that yous want to plough into a mobile experience? Then check out the Universal Android WebView App template. It was developed in Android Studio and supports phones and tablets running Android 4.1 and to a higher place. The Android navigation drawer menu design is completely customizable, as are other components. Information technology also supports AdMob for monetization.
Android Wallpapers App
Here'south a cool Android app template that's useful if y'all desire to get your creative designs out into the world. The Android Wallpapers app supports static images, GIFs, and 4K photos. This template likewise includes useful features like:
- pinch to zoom
- push button notifications
- AdMob advert support
- Android Studio code
Conclusion
In this tutorial, yous learned how to create a navigation drawer bill of fare design in Android from scratch, using Jetpack navigation. Nosotros also explored how easy and quick it is to apply Android Studio templates to create a navigation drawer.
I highly recommend checking out the official textile design guidelines for navigation drawers to learn more about how to properly blueprint and use navigation drawers in Android.
To learn more than nigh coding for Android, check out some of our other courses and tutorials here on Envato Tuts+!
This mail has been updated with contributions from Nathan Umoh. Nathan is a staff writer for Envato Tuts+.
DOWNLOAD HERE
How to Create App Drawer in Android TUTORIAL
Posted by: ameliatherboys.blogspot.com
Comments
Post a Comment