Navigation Drawer

MainActivity.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
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.MenuItem;
import android.widget.Toast;
 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  public Toolbar toolbar;
  public TabLayout tabLayout;
  public ViewPager viewPager;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   toolbar = (Toolbar) findViewById(R.id.toolbar);
   setSupportActionBar(toolbar);
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   getSupportActionBar().setDisplayShowTitleEnabled(true);
   DrawerLayout 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.addDrawerListener(toggle);
   toggle.syncState();
   NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
   navigationView.setNavigationItemSelectedListener(this);
  
 }
  @Override 
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
   int id = item.getItemId();
  
  
  if (id == R.id.idItem1) {
    //Write Your Logic Here 
    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
   
  } else if (id == R.id.idItem2) {
    //Write Your Logic Here 
    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
   
  } else if (id == R.id.idItem3) {
    //Write Your Logic Here
    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
   
  } else if (id == R.id.idItem4) {
    //Write Your Logic Here 
    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
   
  } else if (id == R.id.idItem5) {
    //Write Your Logic Here 
    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
   
  } else if (id == R.id.idItem6) {
    //Write Your Logic Here 
    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
   
  } else if (id == R.id.idItem7) {
    //Write Your Logic Here
    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
   
  }
   DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
   drawer.closeDrawer(GravityCompat.START);
  
  return true;
 }
}


activity_main.xml
<?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/swipe_layout" 
     android:layout_width="match_parent"
     android:layout_height="match_parent" /> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_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_menu_drawer" /> 
</android.support.v4.widget.DrawerLayout>  


swipe_layout.xml
<android.support.design.widget.CoordinatorLayout
    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="match_parent">
    <LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"> 
        <android.support.design.widget.AppBarLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:background="?attr/colorAccent"> 
            <android.support.v7.widget.Toolbar 
             android:id="@+id/toolbar" 
             android:layout_width="match_parent"
             android:layout_height="?attr/actionBarSize"
             android:background="?attr/colorAccent" 
             app:layout_scrollFlags="scroll|enterAlways" /> 
        </android.support.design.widget.AppBarLayout> 
        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent">
            <TextView 
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:gravity="center"
             android:text="Example of Navigation Drawer"
             android:textSize="20sp" />  
        </LinearLayout> 
    </LinearLayout> 
</android.support.design.widget.CoordinatorLayout> 

Share this