Home forword btn Android Studio forword btn Navigation Drawer with Multiple Activity in Android Studio

Android Studio Navigation Drawer with Multiple Activity in Android Studio

Navigation Drawer with Multiple Activity in Android Studio

Navigation Drawer with Multiple Activity in Android Studio

Navigation Drawer with Multiple Activity in Android Studio, In this tutorial how to create navigation drawer in android studio with different and multiple activities. Today i will show you how to create navigation drawer with multiple activity and on items clicks step by step follow me.

Step 1: Create New Project - Add the following dependency in your build.gradle file in app section.

 

 

implementation 'com.google.android.material:material:1.2.0'

And change the
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> to
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

 

Step 2: Create a new Layout in resources folder under layout and layout name navheader.xml then add the following code in navheader.xml file.

 

 

<?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:background="#253137"
android:orientation="vertical"
>

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/ic_android_black_24dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0A3B76"
android:textSize="15sp"
android:layout_gravity="center"
android:text="Navigation Drawer"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="15sp"
android:layout_gravity="center"
android:text="With Multiple Activity"/>

</androidx.appcompat.widget.LinearLayoutCompat>

 

 

Step 3: Now create new layout content_main.xml for toolbar and add the following code in your content_main.xml file.

 

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="Navigation Drawer"
app:titleTextColor="#F8F6F7"
app:layout_constraintEnd_toEndOf="parent"
android:background="#020515"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

Step 4: Then now create menu resources file first create a new resources folder under the resources folder name menu then create a menu resources file name is iconmenu.xml and add the following code in your iconmenu.xml file.

 

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="Home"/>

<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_about"
android:title="About Us" />

<item
android:id="@+id/nav_contact"
android:icon="@drawable/ic_contact"
android:title="Contact Us" />

</menu>

 

Step 5: Add the two string name in string xml file under the value folder.

 

 <string name="open">Open</string>
   <string name="close">Close</string>

 

   
Step 6: Now Completed the header and menu then now add the following code in activity_main.xml file.

 

 

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Main Activty"
android:textColor="@color/colorAccent"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textSize="30sp"/>
</RelativeLayout>
<include
android:layout_height="match_parent"
android:layout_width="match_parent"
layout="@layout/content_main"/>

<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navmenu"
app:menu="@menu/iconmenu"
app:headerLayout="@layout/navheader"
android:layout_gravity="start"/>


</androidx.drawerlayout.widget.DrawerLayout >

 

 

Step 7: Now create two more activity AboutActivity and ContactActivity then now add the following code in your MainActivity.java file.

 

 

package com.softechbharat.navigationdrawerwithactivity;

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.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

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

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
DrawerLayout drawerLayout;
NavigationView navigationView;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navmenu);
navigationView.setItemIconTintList(null);
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){

case R.id.nav_home:
Intent h= new Intent(MainActivity.this,MainActivity.class);
h.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(h);
break;
case R.id.nav_about:
Intent i= new Intent(MainActivity.this,AboutActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
break;
case R.id.nav_contact:
Intent g= new Intent(MainActivity.this,ContactActivity.class);
g.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(g);
break;

}

 

drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}

 

 

Step 8: Now MainActivity layout and code is completed then now add the following code in your activity_about.xml file.

 

 

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context=".AboutActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About Activty"
android:textColor="@color/colorAccent"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textSize="30sp"/>
</RelativeLayout>
<include
android:layout_height="match_parent"
android:layout_width="match_parent"
layout="@layout/content_main"/>

<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navmenu"
app:menu="@menu/iconmenu"
app:headerLayout="@layout/navheader"
android:layout_gravity="start"/>


</androidx.drawerlayout.widget.DrawerLayout >

 

 

Step 9: Add the following code in your AboutActivity.java file.

 

 

package com.softechbharat.navigationdrawerwithactivity;

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.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

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

public class AboutActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
DrawerLayout drawerLayout;
NavigationView navigationView;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navmenu);
navigationView.setItemIconTintList(null);
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){

case R.id.nav_home:
Intent h= new Intent(AboutActivity.this,MainActivity.class);
h.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(h);
break;
case R.id.nav_about:
Intent i= new Intent(AboutActivity.this,AboutActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
break;
case R.id.nav_contact:
Intent g= new Intent(AboutActivity.this,ContactActivity.class);
g.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(g);
break;

}

 

drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}

}

 

 

Step 10: AboutActivity completed and now add the below code in activity_contact.xml file.

 

 

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context=".ContactActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Activty"
android:textColor="@color/colorAccent"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textSize="30sp"/>
</RelativeLayout>
<include
android:layout_height="match_parent"
android:layout_width="match_parent"
layout="@layout/content_main"/>

<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navmenu"
app:menu="@menu/iconmenu"
app:headerLayout="@layout/navheader"
android:layout_gravity="start"/>


</androidx.drawerlayout.widget.DrawerLayout >

 

 

Step 11: The last add the below code in your ContactActivity.java file.

 

 

package com.softechbharat.navigationdrawerwithactivity;

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.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

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

public class ContactActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
DrawerLayout drawerLayout;
NavigationView navigationView;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navmenu);
navigationView.setItemIconTintList(null);
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){

case R.id.nav_home:
Intent h= new Intent(ContactActivity.this,MainActivity.class);
h.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(h);
break;
case R.id.nav_about:
Intent i= new Intent(ContactActivity.this,AboutActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
break;
case R.id.nav_contact:
Intent g= new Intent(ContactActivity.this,ContactActivity.class);
g.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(g);
break;

}

 

drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}

}

 

 

Step 12: Now layout and coding work completed and save the project and run.

Related Tags:
intent in navigation drawer android, navigation drawer with multiple activities in android studio, how to use same navigation drawer in all activities, android navigation drawer with activities, navigation drawer with submenu in android, intent in navigation drawer android, navigation drawer in android studio example, navigation drawer with multiple activity in android studio source code.

Tags: navigation drawer, navigation drawer with multiple activity, navigation drawer example, navigation drawer source code

Leave Your Comment

Login to post a comment