Android-Navigation Drawer
The navigation drawer is a UI panel that displays the primary navigation menu for your app. When the user swipes their finger from the screen’s left edge or presses the drawer icon in the app bar, the drawer opens.
1. Create a menu folder under res folder to implement menu as following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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:ignore="HardcodedText"> <item android:id="@+id/nav_account" android:title="My Account" /> <item android:id="@+id/nav_settings" android:title="Settings" /> <item android:id="@+id/nav_logout" android:title="Logout" /> </menu> |
2. The following code in the activity_main.xml to set up the basic things required for the Navigation Drawer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:id="@+id/my_drawer_layout" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:ignore="HardcodedText" tools:context=".NavigationDrawer"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="128dp" android:gravity="center" android:text="Navigation Drawer" android:textSize="18sp" /> </LinearLayout> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/navigation_menu" /> |
3. The following code in the MainActivity.java file to show the menu icon on the action bar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.example.layouts; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.drawerlayout.widget.DrawerLayout; import android.os.Bundle; import android.view.MenuItem; public class NavigationDrawer extends AppCompatActivity { public DrawerLayout drawerLayout; public ActionBarDrawerToggle actionBarDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation_drawer); drawerLayout = findViewById(R.id.my_drawer_layout); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close); // pass the Open and Close toggle for the drawer layout listener // to toggle the button drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } |