
Hiii everyone is this article I am sharing How to use multiple navigation drawer in android activities. In android, multiple navigation drawer is the user when I click on the main navigation drawer. Click on an item on the main drawer then open sub navigation drawer in with its item.
In these android solutions android, multiple navigations drawers add in an activity. I create multiple navigation drawer layouts. and manganese its click on item according to open sub-navigation.
So let’s Start on this Android development multiple navigation drawers in android activities.in this example I am using androidX with all the latest library in android. How to use multiple navigation drawer in android activities.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.android.support:design:29.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.drawerlayout:drawerlayout:1.0.0"
implementation 'com.google.android.material:material:1.1.0'
implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
Step 1: Create an android project with empty activity in android studio.
So tart your android studio and making a new project with an empty activity like blank screen and follow these steps.
Step 2: Open your XML file And add these codes.
<?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_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@color/White"
app:itemTextColor="@color/Black"
app:itemIconTint="@color/colorPrimaryDark"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
Step 3 Make a new layout file nav_header_home.xml.
<?xml version="1.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="100dp"
android:background="@color/White"
android:orientation="vertical"
android:padding="15dp"
android:gravity="bottom"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/action_icons" />
</LinearLayout>
Step 4: Generate a new Menu XML file activity_home_drawer for the main Drawer.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_Profile"
android:icon="@drawable/baseline_announcement_24"
android:title="Profile" />
<item
android:id="@+id/nav_Product"
android:icon="@drawable/baseline_cloud_24"
android:title="Product Services " />
<item
android:id="@+id/nav_Resources"
android:icon="@drawable/baseline_open_in_new_24"
android:title="Resources" />
<item
android:id="@+id/nav_Events"
android:icon="@drawable/ic_calander_24dp"
android:title="Events" />
<item
android:id="@+id/nav_Blogs"
android:icon="@drawable/baseline_style_24"
android:title="Blogs" />
<item
android:id="@+id/nav_Case_Studies"
android:icon="@drawable/baseline_school_24"
android:title="Case Studies" />
<item
android:id="@+id/nav_Privacy"
android:icon="@drawable/baseline_verified_user_24"
android:title="Privacy Policy" />
<item
android:id="@+id/nav_QRCode"
android:icon="@drawable/icons_qr_code"
android:title="QR Code" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_ContactUs"
android:icon="@drawable/baseline_contact_mail_24"
android:title="Contact Us" />
<item
android:id="@+id/nav_Logout"
android:icon="@drawable/ic_logout_21dp"
android:title="Log out" />
</menu>
</item>
</menu>
Step 5: Create a Sub Navigation drawer menu layout producte_menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_BackPress"
android:icon="@drawable/baseline_keyboard_return_24"
android:title="Product Services" />
<!-- <item-->
<!-- android:title="" />-->
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_Cloud_service"
android:icon="@drawable/baseline_cloud_24"
android:title="Cloud service" />
<item
android:id="@+id/nav_Public_Cloud"
android:icon="@drawable/baseline_public_24"
android:title="Public Cloud " />
<item
android:id="@+id/nav_Private_Cloud"
android:icon="@drawable/baseline_wifi_lock_24"
android:title="Private Cloud" />
<item
android:id="@+id/nav_Multi_Cloud"
android:icon="@drawable/baseline_usb_24"
android:title="Multi Cloud" />
<item
android:id="@+id/nav_Migration"
android:icon="@drawable/baseline_merge_type_24"
android:title="Migration As a Service" />
</group>
</menu>
Step 6: Build a second sub Navigation drawer menu layout resource_menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_BackPress1"
android:icon="@drawable/baseline_keyboard_return_24"
android:title="Resources" />
<!-- <item-->
<!-- android:title="" />-->
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_ARTICLE"
android:icon="@drawable/baseline_speaker_notes_24"
android:title="ARTICLE" />
<item
android:id="@+id/nav_BLOGS"
android:icon="@drawable/baseline_style_24"
android:title="BLOGS " />
<item
android:id="@+id/nav_WEBINER_SESSIONS"
android:icon="@drawable/ic_language_24dp"
android:title="WEBINER SESSIONS" />
<item
android:id="@+id/nav_Multi_CASE_STUDIES"
android:icon="@drawable/baseline_school_24"
android:title="CASE STUDIES" />
<item
android:id="@+id/nav_VIDEO"
android:icon="@drawable/baseline_subscriptions_24"
android:title="VIDEO" />
</group>
</menu>
Step 7: Open Your main.java file and implements these all Drawers.
public class Home_Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
if(!drawer.isDrawerOpen(Gravity.START))
drawer.openDrawer(Gravity.START);
else drawer.closeDrawer(Gravity.END);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.action_icons);
}
@SuppressLint("WrongConstant")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_Profile) {
Intent intent = new Intent(Home_Activity.this, About_Us.class);
startActivity(intent);
finish();
} else if (id == R.id.nav_Product) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.producte_menu);
navigationView.setNavigationItemSelectedListener(this);
} else if (id == R.id.nav_Resources) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.resource_menu);
navigationView.setNavigationItemSelectedListener(this);
} else if (id == R.id.nav_Events) {
} else if (id == R.id.nav_Blogs) {
Intent intent = new Intent(Home_Activity.this, Blogs_Screen.class);
startActivity(intent);
finish();
}else if (id == R.id.nav_Case_Studies) {
Intent intent = new Intent(Home_Activity.this, Case_Studie.class);
startActivity(intent);
finish();
}else if (id == R.id.nav_Privacy) {
Intent intent = new Intent(Home_Activity.this, Privacy_Policy.class);
startActivity(intent);
finish();
}else if (id == R.id.nav_ContactUs) {
Intent intent = new Intent(Home_Activity.this, ContactUs.class);
startActivity(intent);
finish();
} else if (id == R.id.nav_Logout) {
SessionManagement sessionManagement=new SessionManagement();
sessionManagement.Logout(Home.this);
Intent intent1=new Intent(Home.this,SignIn.class);
startActivity(intent1);
finish();
} else if (id == R.id.nav_BackPress) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.activity_home_drawer);
navigationView.setNavigationItemSelectedListener(this);
} else if (id == R.id.nav_Cloud_service) {
} else if (id == R.id.nav_Public_Cloud) {
}else if (id == R.id.nav_Private_Cloud) {
}else if (id == R.id.nav_Multi_Cloud) {
}else if (id == R.id.nav_Migration) {
} else if (id == R.id.nav_BackPress1) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.activity_home_drawer);
navigationView.setNavigationItemSelectedListener(this);
} else if (id == R.id.nav_ARTICLE) {
Intent intent = new Intent(Home_Activity.this, Articles_Screen.class);
startActivity(intent);
finish();
} else if (id == R.id.nav_BLOGS) {
Intent intent = new Intent(Home_Activity.this, Blogs_Screen.class);
startActivity(intent);
finish();
}else if (id == R.id.nav_WEBINER_SESSIONS) {
}else if (id == R.id.nav_Multi_CASE_STUDIES) {
Intent intent = new Intent(Home_Activity.this, Case_Studie.class);
startActivity(intent);
finish();
}else if (id == R.id.nav_VIDEO) {
}
return true;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
after complete, these steps run your example and testing happy coding…….





