original source: https://updateunlimited.blogspot.com.au/2015/12/double-sided-nav-drawers.html
Hello guys, this post will teach you how to add both Left and Right Navigation Drawers in your Android App. You may need one Nav Drawer on the right to give your users a different set of options, not to distract them from the main navigation items on the left.
I have used Android Studio 1.5.1 Build 141.2456560. So here we go.
1) Navigation Drawer Activity
When creating a New Project, choose Navigation Drawer Activity. This makes things easy.
If this option is not available, you can the simply choose Blank Activity and later make the project structure similar to Navigation Drawer Activity. See the layout files here.
If you run the app at this point, you will see the Left Nav Drawer.
2) Navigation Views
<?xml version=“1.0” encoding=“utf-8”?>
<android.support.v4.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_main”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
<android.support.design.widget.NavigationView
android:id=“@+id/nav_left_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” />
<android.support.design.widget.NavigationView
android:id=“@+id/nav_right_view”
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:layout_gravity=“end”
android:fitsSystemWindows=“true”
app:headerLayout=“@layout/nav_header_main”
app:menu=“@menu/activity_right_drawer” />
</android.support.v4.widget.DrawerLayout>
view rawactivity_main.xml hosted with ❤ by GitHub
As you can see I have added 2 Navigation Views inside Drawer Layout…one for left-side and other for right-side. For the Right-side, add android:layout_gravity=“end”Other thing you will notice is app:menu.The easiest way to add menu items within the NavDrawer is through XML.
3) Drawer Menu Items
Inside menu folder, add two XMLs — activity_main_drawer.xml (for left-drawer) and activity_right_drawer.xml (for right-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_camera”
android:icon=“@drawable/ic_menu_camera”
android:title=“Import” />
<item
android:id=“@+id/nav_gallery”
android:icon=“@drawable/ic_menu_gallery”
android:title=“Gallery” />
<item
android:id=“@+id/nav_slideshow”
android:icon=“@drawable/ic_menu_slideshow”
android:title=“Slideshow” />
<item
android:id=“@+id/nav_manage”
android:icon=“@drawable/ic_menu_manage”
android:title=“Tools” />
</group>
<item android:title=“Communicate”>
<menu>
<item
android:id=“@+id/nav_share”
android:icon=“@drawable/ic_menu_share”
android:title=“Share” />
<item
android:id=“@+id/nav_send”
android:icon=“@drawable/ic_menu_send”
android:title=“Send” />
</menu>
</item>
</menu>
view rawactivity_main_drawer.xml hosted with ❤ by GitHub
<?xml version=“1.0” encoding=“utf-8”?>
<menu xmlns:android=“http://schemas.android.com/apk/res/android”>
<group android:checkableBehavior=“single”>
<item android:title=“Account”>
<menu>
<item
android:id=“@+id/nav_settings”
android:icon=“@drawable/ic_menu_share”
android:title=“Settings” />
<item
android:id=“@+id/nav_logout”
android:icon=“@drawable/ic_menu_send”
android:title=“Logout” />
</menu>
</item>
<item
android:id=“@+id/nav_help”
android:icon=“@drawable/ic_menu_camera”
android:title=“Help” />
<item
android:id=“@+id/nav_about”
android:icon=“@drawable/ic_menu_gallery”
android:title=“About” />
</group>
</menu>
view rawactivity_right_drawer.xml hosted with ❤ by GitHub4) Icon to open Right-Drawer
To the main.xml file which shows Settings in an overflow menu on the top-right, make these changes. You can add any icon of your choice.
<?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/action_openRight”
android:orderInCategory=“100”
android:title=“@string/action_settings”
android:icon=“@drawable/ic_open_right”
app:showAsAction=“always” />
</menu>
view rawmain.xml hosted with ❤ by GitHub5) Navigation Item Selected Listeners
package com.blog.navdrawer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, “Replace with your own action”, Snackbar.LENGTH_LONG)
.setAction(“Action”, null).show();
}
});
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView leftNavigationView = (NavigationView) findViewById(R.id.nav_left_view);
leftNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle Left navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
Toast.makeText(MainActivity.this, “Left Drawer – Import”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_gallery) {
Toast.makeText(MainActivity.this, “Left Drawer – Gallery”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_slideshow) {
Toast.makeText(MainActivity.this, “Left Drawer – Slideshow”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_manage) {
Toast.makeText(MainActivity.this, “Left Drawer – Tools”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(MainActivity.this, “Left Drawer – Share”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(MainActivity.this, “Left Drawer – Send”, Toast.LENGTH_SHORT).show();
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
NavigationView rightNavigationView = (NavigationView) findViewById(R.id.nav_right_view);
rightNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle Right navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_settings) {
Toast.makeText(MainActivity.this, “Right Drawer – Settings”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_logout) {
Toast.makeText(MainActivity.this, “Right Drawer – Logout”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_help) {
Toast.makeText(MainActivity.this, “Right Drawer – Help”, Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_about) {
Toast.makeText(MainActivity.this, “Right Drawer – About”, Toast.LENGTH_SHORT).show();
}
drawer.closeDrawer(GravityCompat.END); /*Important Line*/
return true;
}
});
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (drawer.isDrawerOpen(GravityCompat.END)) { /*Closes the Appropriate Drawer*/
drawer.closeDrawer(GravityCompat.END);
} else {
super.onBackPressed();
System.exit(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
int id = item.getItemId();
if (id == R.id.action_openRight) {
drawer.openDrawer(GravityCompat.END); /*Opens the Right Drawer*/
return true;
}
return super.onOptionsItemSelected(item);
}
}
view rawMainActivity.java hosted with ❤ by GitHub
The only important line to notice is drawer.closeDrawer(GravityCompat.END); for the Right NavDrawer. Otherwise the code is simple,well-commented and easy to implement.