Navigation Drawer In Android

How to implement Navigation Drawer in Android?

 
With the use of a navigation drawer, we can switch between all the functionalities of an application easily. Here I am going to describe to you the steps for implementing navigation drawer in Android application.  Following are the main steps for creating a navigation drawer
  1. Create Drawer Layout
  2. Initialize the Drawer List
  3. Handle Navigation Click Events
  4. Listen for Drawer Open And Close Events.
  5. Open And Close with the App Icon. 

Create a Drawer Layout 

 
Drawer Layout acts as a top-level container for window content that allows for interactive drawer views to be pulled out from the edge of the window. Drawer positioning and layout is controlled using android:layout_gravity attribute on child views whose value=side of the view you want the drawer to emerge from left or right. To use drawer layout position your primary content views as the first child with a width and height of match_parent. Add the drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly used match_parent for height with a fixed width. DrawerLayout.DrawerListner can be used to monitor the state and motion of drawer views. Any drawers that positioned the left/start should always contain content for navigation around the application, whereas any drawer positioned to right/end should always contain actions to take on the current content.
 

Initialize the Drawer list 

 
The list should be populated by an adapter. Use setOnItemClickListner to receive click events in the navigation drawer's list.
 

Handle Navigation Click Events 

 
When the user selects an item in the drawer's list, the system call onItemClick() on the onItemClickListner given to setOnItemClickListner() 
 
Listen for Navigation Drawer Open And Close Events 
 
To listen for drawer open and close events, call setDrawerListner() on your drawer layout and pass it an implementation of DrawerLayout.DrawerLister. This interface provide callbacks for onDraweropen() and onDrawerClosed() . If your activity includes the actionbar, you can instead extend the ActionBarDrawerToggle class. The ActionBarDrawerToggle implements DrawerLayout.DrawerListner so you can still override those callbacks, but it also facilitates the proper interaction behavior between the action bar icon and the navigation drawer. 
 
Open And Close with The App Icon 
 
To make  ActionBarDrawerToggle works create an instance of this with a constructor which requires the following arguments
  • Activity hosting the drawer
  • Drawer Layout
  • A drawable resource to use as the drawer indicator
  • Standard navigation icon which can be downloaded.
  • String resource to describe the "open drawer" action
  • String resource to describe the "Close drawer" action
Now I am going to describe how we can create a drawer layout using an Android studio.
 
For that first you create your layout fie, 
  1. <android.support.v4.widget.DrawerLayout  
  2. xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_width="match_parent"  
  4. android:id="@+id/drawerlayout"  
  5. android:layout_height="match_parent">  
  6.     <FrameLayout  
  7. android:id="@+id/mainContent"  
  8. android:layout_width="match_parent"  
  9. android:layout_height="match_parent">  
  10.     </FrameLayout>  
  11.     <ListView  
  12. android:background="#FF8800"  
  13. android:divider="@null"  
  14. android:id="@+id/listview"  
  15. android:layout_width="match_parent"  
  16. android:layout_gravity ="left"  
  17. android:layout_height="match_parent">  
  18.     </ListView>  
  19.  </android.support.v4.widget.DrawerLayout>   
Please don't forget to add the layout gravity for the listview,
 
Now I created an array in the string.xml file. 
  1. <array name="listItems">  
  2.     <item>Menu One</item>  
  3.     <item>Menu Two</item>  
  4.     <item>Menu Three</item>  
  5.     <item>Menu Four</item>  
  6. </array>  
  7.  Initialise all the ui elements in the mainActivity.java file in the onCreate()  
  8. listView = (ListView) findViewById(R.id.listview);  
  9. drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);  
  10. final String []array = getResources().getStringArray(R.array.listItems);  
  11. listView.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,array));  
  12. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  13. @Override  
  14. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {  
  15. listView.setItemChecked(i, true);  
  16.         setTitle(array[i]);  
  17.         Toast.makeText(MainActivity.this, array[i],Toast.LENGTH_SHORT).show();  
  18.     }  
  19. });   
Now initialize the ActionBarDrawerToggle like the following,
  1. actionBarListner = new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);  
  2. drawerLayout.setDrawerListener(actionBarListner);  
  3. getSupportActionBar().setHomeButtonEnabled(true);  
  4. getSupportActionBar().setDisplayHomeAsUpEnabled(true);  
Now create the following methods thats also must have for the drawer layout,
 
This method is for any configuration changes like the orientation or the device size changes,
  1. @Override  
  2. public void onConfigurationChanged(Configuration newConfig) {  
  3. super.onConfigurationChanged(newConfig);  
  4. actionBarListner.onConfigurationChanged(newConfig);  
  5. }  
This  will be called after the activty id created,
  1. @Override  
  2. protected void onPostCreate(Bundle savedInstanceState) {  
  3. super.onPostCreate(savedInstanceState);  
  4. actionBarListner.syncState();  
  5. }  
This will set the title for the action bar,
 
  1. public void setTitle(String title){  
  2.     getSupportActionBar().setTitle(title);  
  3. }  
  4.   
  5. @Override  
  6. public boolean onOptionsItemSelected(MenuItem item) {  
  7. if(actionBarListner.onOptionsItemSelected(item))  
  8. return true;  
  9. return super.onOptionsItemSelected(item);  
  10. }  
 Please see the screen shots also,
 
 
 
Read more articles on Android


Similar Articles