0% found this document useful (0 votes)
186 views

Navigation Drawer Menu

This document provides step-by-step instructions for creating a navigation drawer menu in Android. It describes adding dependencies, setting up layout files with a DrawerLayout and NavigationView, creating menu and header XML files, and writing Java code to handle navigation item clicks and toggle the drawer open and closed. Key steps include adding design support libraries, defining themes without action bars, setting up the drawer layout and toolbar, creating menu and header resources, and programming the navigation item selection logic.

Uploaded by

Rana Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

Navigation Drawer Menu

This document provides step-by-step instructions for creating a navigation drawer menu in Android. It describes adding dependencies, setting up layout files with a DrawerLayout and NavigationView, creating menu and header XML files, and writing Java code to handle navigation item clicks and toggle the drawer open and closed. Key steps include adding design support libraries, defining themes without action bars, setting up the drawer layout and toolbar, creating menu and header resources, and programming the navigation item selection logic.

Uploaded by

Rana Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

How To Create Navigation Drawer Menu

No.1 First add


//noinspection GradleCompatible
implementation 'com.android.support:design:28.0.0'
No.2 No Action Bar Set so we open values directory to set two themes file like below

No.3 Change the layout is androidx.drawerlayout.widget.DrawerLayout


And also add id, and openDrawer
android:id="@+id/dreawrnemu"
tools:openDrawer="start"

No.4 Create a Toolbar in Relative Layout and set the following parameters
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Toolbar"
app:title="Navigation Demo"
app:titleTextColor="@color/white"
android:background="@color/teal_700"/>

</RelativeLayout>

No.5 Create menu director. Right Click res and click in new, select Android Resource Directory
to add menu directory.
No.6 Click here menu Directory and create iconmenu file
View Code of iconmue File

No.7 Right Click drawable to create new in Vector Asset

No.8 Add a Home icon in drawable directory


Add three more Icon here which want to use in Menu

No.9 Create Menu Item and three command Title, Id, icon and also three Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Home"
android:id="@+id/menu_home"
android:icon="@drawable/ic_baseline_home_24"
/>

<item android:title="Call"
android:id="@+id/menu_call"
android:icon="@drawable/ic_baseline_call_24"
/>

<item android:title="Search"
android:id="@+id/menu_setting"
android:icon="@drawable/ic_baseline_person_search_24"
/>

</menu>

No.10 Create a Navigation Header File


No.11 Type File name and press ok button

No.12 In this file we are first three setting here……..


1- Change Layout is linear
2- Width wrap content
3- Orientation is vertical
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

</androidx.appcompat.widget.LinearLayoutCompat>

No.13 In liner layout we insert two things. So first add company logo in u project.
1- Image view of u company (download any)
2- Company name (Text view)
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/companylogo"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Company Name"
android:textSize="30dp"
android:textColor="@color/purple_200"/>

In navhearder file change background color is


No.14 Finally we create Navigation view in name file in coding section. Open main
file and type here..
Four setting here…
(1) id ,
(2) add menu file
(3) headerlayout
(4) is layout_gravity
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/navmenu"
app:menu="@menu/iconmenu"
app:headerLayout="@layout/navheader"
android:layout_gravity="start"/>
No.15 Write code in Java file (MyDrawerMenu)

package com.example.mysplashscreen;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;

public class MyDrawerMenu extends AppCompatActivity {


NavigationView nav;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_drawer_menu);
// alreday remove toolbar so we add custom toolbar
Toolbar toolbar=findViewById(R.id.Toolbar);
setSupportActionBar(toolbar);

nav=(NavigationView)findViewById(R.id.navmenu);
drawerLayout=(DrawerLayout)findViewById(R.id.dreawrnemu);

toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,
R.string.app_name,R.string.app_name);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();

nav.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.menu_home :
Toast.makeText(getApplicationContext(),"Home Panel is
Open",Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
break;

case R.id.menu_call :
Toast.makeText(getApplicationContext(),"Call Panel is
Open",Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
break;

case R.id.menu_setting :
Toast.makeText(getApplicationContext(),"Setting Panel is
Open",Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
break;
}
return false;
}
});

}
}

You might also like