Sliding Drawer in Android

Introduction

 
A sliding drawer is the same as a custom ListView or a simple ListView. The only difference is that a Sliding Drawer remains hidden in the start. It only appears when we slide it out. After clicking on an item, it hides again. Today I will show a demo of a sliding drawer.
 
Now we move to the code part of this article.
 
In this project, I used two activities. Our first activity is activity_main activity_main.xml. The XML code of this activity is,
  1. <android.support.v4.widget.DrawerLayout    
  2.     xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:id="@+id/drawer_layout"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent">    
  6.     
  7.     <FrameLayout    
  8.         android:id="@+id/content_frame"    
  9.         android:layout_width="match_parent"    
  10.         android:layout_height="match_parent" />    
  11.     
  12.     <ListView android:id="@+id/left_drawer"    
  13.         android:layout_width="240dp"    
  14.         android:layout_height="match_parent"    
  15.         android:layout_gravity="start"    
  16.         android:choiceMode="singleChoice"    
  17.         android:divider="@android:color/transparent"    
  18.         android:dividerHeight="0dp"    
  19.         android:background="#fff"/>    
  20. </android.support.v4.widget.DrawerLayout>   
    The graphical layout of activity_main.xml is,
     
    Graphical Layout of activity
     
    In this activity we use a FrameLayout and a ListView. FrameLayout is used to occupy space on the screen. It is designed to block out an area on the screen. The main thing that a frame layout does is to hold a single child view. In our program when a user clicks on an item of a ListView we show the name of that item in the frame layout. In the ListView we will show a TextView present in the menu_detail_fragment activity.
    1. <android.support.v4.widget.DrawerLayout    
    2.     xmlns:android="http://schemas.android.com/apk/res/android"    
    3.     android:id="@+id/drawer_layout"    
    4.     android:layout_width="match_parent"    
    5.     android:layout_height="match_parent">    
    6. </android.support.v4.widget.DrawerLayout>   
      In the preceding line of code we define a drawer type layout for activity. DrawerLayout is not present in the Layout Option of an activity.
       
      DrawerLayout
       
      We must externally write code for DrawerLayout. In this activity we create a DrawerLayout and provide an id (drawer_Layout) to this DrawerLayout.
       
      The second Activity is menu_detail_fragment.xml. The following is the XML code of this activity,
      1. <?xml version="1.0" encoding="utf-8"?>    
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
      3.     android:orientation="vertical"    
      4.     android:layout_width="match_parent"    
      5.     android:gravity="center"    
      6.     android:background="#5ba4e5"    
      7.     android:layout_height="match_parent">    
      8.     <TextView    
      9.         android:layout_width="wrap_content"    
      10.         android:layout_height="wrap_content"    
      11.         android:textSize="40px"    
      12.         android:textColor="#ffffff"    
      13.         android:layout_gravity="center"    
      14.         android:id="@+id/detail"/>    
      15.     
      16. </LinearLayout>   
      Graphical Layout
       
      Graphical Layout
       
      In this activity we use a simple TextView. In this textview we will insert some data and then bind a textview with a ListView of the activity_main activity.
       
      Now I will explain the source code of this project.
       
      MainActivity.java file- This Java file contains the code for the activity_main activity. The source code of this activity is:
      1. public class MainActivity extends Activity {    
      2.     String[] menu;    
      3.     DrawerLayout dLayout;    
      4.     ListView dList;    
      5.     ArrayAdapter < String > adapter;    
      6.     
      7.     @Override    
      8.     protected void onCreate(Bundle savedInstanceState) {    
      9.         super.onCreate(savedInstanceState);    
      10.         setContentView(R.layout.activity_main);    
      11.     
      12.     
      13.         menu = new String[] {    
      14.             "Home""Android""Windows""Linux""Raspberry Pi""WordPress""Videos""Contact Us"    
      15.         };    
      16.         dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);    
      17.     
      18.         dList = (ListView) findViewById(R.id.left_drawer);    
      19.         adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, menu);    
      20.         dList.setAdapter(adapter);    
      21.         dList.setSelector(android.R.color.holo_blue_dark);    
      22.     
      23.         dList.setOnItemClickListener(new OnItemClickListener() {    
      24.     
      25.             @Override    
      26.             public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {    
      27.     
      28.                 dLayout.closeDrawers();    
      29.                 Bundle args = new Bundle();    
      30.                 args.putString("Menu", menu[position]);    
      31.     
      32.                 Fragment detail = new DetailFragment();    
      33.                 detail.setArguments(args);    
      34.                 FragmentManager fragmentManager = getFragmentManager();    
      35.                 fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();    
      36.     
      37.             }    
      38.     
      39.         });    
      40.     }    
      41. }   
        Now I will explain the preceding code in a number of sections for a better understanding:
         
        Code Section 1
        1. String[] menu;    
        2. DrawerLayout dLayout;    
        3. ListView dList;    
        4. ArrayAdapter<String> adapter;   
          In the preceding code, we create an array of string types (menu). In this array, we provide the name of an item for the ListView. We create an object of a DrawerLayout type(dLayout). In this object, we get the reference of our DrawerLayout. We also create an object of a ListView type(dList). In this object, we hold the reference of the ListView in the menu_detail_fragment activity. The last object is the ArrayAdapter type(adapter). ArrayAdapter is used for binding the data with the ListView.
           
          Code Section 2
          1. menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};    
          2. dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);    
          3.     
          4. dList = (ListView) findViewById(R.id.left_drawer);    
          5. adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);    
          6. dList.setAdapter(adapter);    
          7. dList.setSelector(android.R.color.holo_blue_dark);   
            In the preceding code first of all we insert data into a menu(string type array). Then we get the reference of drawer_layout(DrawerLayout) in the dLayout variable. We also get the reference of the left_drawer ListView in the dList variable. Then we insert the value of the menu(string array) in the adapter variable(ArrayAdapter). Finally we bind this ArrayAdapter object with the ListView using the setAdapter method.
             
            Code Section 3
            1. dList.setOnItemClickListener(new OnItemClickListener() {    
            2.     
            3.     @Override    
            4.     public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {    
            5.     
            6.         dLayout.closeDrawers();    
            7.         Bundle args = new Bundle();    
            8.         args.putString("Menu", menu[position]);    
            9.     
            10.         Fragment detail = new DetailFragment();    
            11.         detail.setArguments(args);    
            12.         FragmentManager fragmentManager = getFragmentManager();    
            13.         fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();    
            14.     
            15.     }    
            16.     
            17. });   
              In the preceding code, we set an OnItemClick event for the ListView. When someone clicks on an item of the ListView then this event will fire. First of all, on the item click event, we close the Drawer using the closeDrawers method. We create an object of the Bundle class. The Bundle class is mainly used for passing the data to another activity. We pass the value of the ListView item using the Bundle object. We create an object of Fragment type. We assign the object of the DetailFragment class into this fragment object. Then we create an object of the FragmentManager class. Using this FragmentManager object we replace the contents of content_frame(FrameLayout) with the data of the item of the ListView.
               
              Code of the DetailFragment.java file: This Java file contains the code for the menu_detail_fragment activity.
              1. public class DetailFragment extends Fragment {    
              2.     TextView text;    
              3.     @Override    
              4.     public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {    
              5.         View view = inflater.inflate(R.layout.menu_detail_fragment, container, false);    
              6.         String menu = getArguments().getString("Menu");    
              7.         text= (TextView) view.findViewById(R.id.detail);    
              8.         text.setText(menu);    
              9.         return view;    
              10.     }    

              In the preceding code we use an inflate method of the LayoutInflater class. The LayoutInflater class dynamically adds or removes items for a Layout. The Inflate method reads an XML file and converts the contents of the XML file into a View type object. Using the getString method we retrieve the data passed from the MainActivity class. We assign this value to the TextView and finally return the View.
               
              Now our project is ready. When we run this project it will look as in the following.
               
              sliding navigationDrawer
               
              Now go to the left side and slide out the screen. When we slide out the drawer, then the screen will look as in the following:
               
              slide out the screen
               
              Now click on an item of the ListView. When we click on an item of the ListView it displays the data of the ListView item into the frame Layout as in the following:
               
              Layout
               
              android


              Similar Articles