First of all, to add DrawerLayout in main.xml file and inside drawer layout to add lable or listview.
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1"> <include layout="@layout/layout_toolbar" /> <FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent">
//Include layout of main screen
<include android:id="@+id/home"
layout="@layout/fragment_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </FrameLayout> </LinearLayout>
<LinearLayout
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="left|start" android:orientation="vertical">
//Add here lable or listview you want
<LinearLayout
android:id="@+id/linear_dashboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="20dp"
android:orientation="vertical"> <ImageView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/text_dashboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="Dashboard"
android:textColor="@color/white"
android:textSize="14dp" /> </LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
//layout_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/action_back"> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent" android:gravity="center"
android:text="App Name"
android:textColor="@color/white"
android:layout_height="wrap_content">
</TextView> </LinearLayout> </android.support.v7.widget.Toolbar>
//fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"> <LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent" android:gravity="center"
android:text="App Name"
android:textColor="@color/white"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
Now, In java add following code for Navigation Drawer
public class MainActivity extends AppCompatActivity {
//First to make variable of Components
Toolbar toolbar;
public static DrawerLayout drawerLayout; private ActionBarDrawerToggle drawerToggle;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
//First to take Reference of Components
toolbar = (Toolbar) findViewById(R.id.toolbar); spinner_menu = (Spinner) findViewById(R.id.spinner_menu); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
// To add Following method in your class setSupportActionBar(toolbar); initDrawerLayout();
}
private void initDrawerLayout() { drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { @Override
public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } @Override
public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } };
drawerToggle.setDrawerIndicatorEnabled(false); Drawable drawable = ResourcesCompat.getDrawable(getResources(),
R.drawable.ic_drawer,getTheme()); drawerToggle.setHomeAsUpIndicator(drawable); drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (drawerLayout.isDrawerVisible(GravityCompat.START)) { drawerLayout.closeDrawer(GravityCompat.START); } else { drawerLayout.openDrawer(GravityCompat.START); } } }); drawerLayout.setDrawerListener(drawerToggle); } @Overrideprotected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState(); } @Overridepublic void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); drawerToggle.onConfigurationChanged(newConfig); }
}