Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The Navigation Drawer is a popular UI component in many Android applications. It's a sliding panel that comes from the side of the screen and can contain any kind of widgets, but it��s commonly used for navigation purposes.
Add the Material Design and Navigation Component dependencies in your build.gradle
(if you haven't already):
implementation 'com.google.android.material:material:1.X.X' implementation 'androidx.navigation:navigation-fragment:2.X.X' implementation 'androidx.navigation:navigation-ui:2.X.X'
Create a layout for the main activity, which will contain the DrawerLayout
:
activity_main.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" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.google.android.material.navigation.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </androidx.drawerlayout.widget.DrawerLayout>
You need to define the items that appear in the navigation drawer. Create a new menu XML file inside the res/menu
directory.
activity_main_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="Home" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_gallery" android:title="Gallery" /> <!-- Add more items as needed --> </group> </menu>
In your MainActivity
, you'll handle the interactions:
class MainActivity : AppCompatActivity() { private lateinit var appBarConfiguration: AppBarConfiguration override fun onCreate(savedInstanceState: Bundle?) { 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) appBarConfiguration = AppBarConfiguration(setOf( R.id.nav_home, R.id.nav_gallery), 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() } }
This is a basic implementation. You can customize the navigation drawer further by adding headers, footers, or handling specific item clicks.
Remember to design the navigation drawer responsibly by following Material Design guidelines and ensure a great user experience.
Implementing Navigation Drawer in Android:
<!-- activity_main.xml --> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Main Content --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- Navigation Drawer --> <ListView android:id="@+id/nav_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#fff"/> </androidx.drawerlayout.widget.DrawerLayout>
Navigation Drawer with ActionBar in Android:
// MainActivity.java ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener(toggle); toggle.syncState();
Handling Clicks in Navigation Drawer Android:
// MainActivity.java ListView navDrawer = findViewById(R.id.nav_drawer); navDrawer.setOnItemClickListener((adapterView, view, position, id) -> { // Handle item click based on position // Example: loadFragment(position); });