. f Android navigation drawer tutorial | Android navigation drawer with activities | Android Sliding Navigation Drawer – Example ~ Android Developers Blog

Sunday, 24 August 2014

Android navigation drawer tutorial | Android navigation drawer with activities | Android Sliding Navigation Drawer – Example

Hello Droid Guys
     Today,  I am going to share the tutorial of  "Android sliding navigation drawer". You also find many tutorial on Google which helps you to show navigation drawer but most of them are using fragment to do that. Here, are the few good tutorial which I follows:
Android Navigation Drawer
Navigation drawer activity

1. https://developer.android.com/design/patterns/navigation-drawer.html
2. http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

Here, I am going show sliding menu using activity. We can also achieve
the functionality of  Navigation drawer with activities.

This Android tutorial describes How to implement a navigation drawer using  Support Library the DrawerLayout API.

1. Create Drawer Layout :
            To create a navigation drawer, We first declare user interface with a
DrawerLayout as the root view of layout.

Inside the Drawer Layout, add one view that contains the main content for the
screen (your primary layout when the drawer is hidden) and another view that
contains the contents of the navigationdrawer. In this example, I am using a DrawerLayout
with two child. One a Relative layout with webView(the main content), and other
with a ListView for the navigation drawer. The webview is my activity content view.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.v4.widget.DrawerLayout  
  3.   
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.   
  6.     android:id="@+id/drawer_layout"  
  7.   
  8.     android:layout_width="match_parent"  
  9.   
  10.     android:layout_height="match_parent" >  
  11.     <!--  
  12.           main content  
  13.      -->  
  14.   
  15.     <RelativeLayout  
  16.   
  17.         android:layout_width="fill_parent"  
  18.   
  19.         android:layout_height="fill_parent"  
  20.   
  21.         android:background="#ffffff" >  
  22.   
  23.         <WebView  
  24.   
  25.             android:id="@+id/webview"  
  26.   
  27.             android:layout_width="fill_parent"  
  28.   
  29.             android:layout_height="fill_parent"  
  30.   
  31.             android:background="#ffffff" />  
  32.   
  33.     </RelativeLayout>  
  34.   
  35.     <!--  
  36.         navigation list item  
  37.     -->  
  38.     <FrameLayout  
  39.   
  40.         android:id="@+id/content_frame"  
  41.   
  42.         android:layout_width="match_parent"  
  43.   
  44.         android:layout_height="match_parent" />  
  45.   
  46.     <ListView  
  47.   
  48.         android:id="@+id/left_drawer"  
  49.   
  50.         android:layout_width="240dp"  
  51.   
  52.         android:layout_height="match_parent"  
  53.   
  54.         android:layout_gravity="left"  
  55.   
  56.         android:background="#2A323D"  
  57.   
  58.         android:cacheColorHint="#00000000"  
  59.   
  60.         android:choiceMode="singleChoice" />  
  61.   
  62.   
  63.   
  64. </android.support.v4.widget.DrawerLayout>  


2. Initialize the Drawer List :
  1. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
  2. mDrawerList = (ListView) findViewById(R.id.left_drawer);  
  3.   
  4. mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,  
  5.         GravityCompat.START);  
  6. // set up the drawer's list view with items and click listener  
  7. mDrawerList.setAdapter(new MenuDrawerListAdapter(this, menuItemTitles,  
  8.             Config.drawerMenuItemsIconIds));  
  9. mDrawerList.setOnItemClickListener(new DrawerItemClickListener());  


3. Handle Navigation List click event :

  1. /* The click listener for ListView in the navigation drawer */  
  2. public class DrawerItemClickListener implements  
  3.     ListView.OnItemClickListener {  
  4.     @Override  
  5.     public void onItemClick(AdapterView parent, View view, int position,  
  6.         long id) {  
  7.         switch (position) {  
  8.         case 0: {  
  9.         Intent main = new Intent(getApplicationContext(),  
  10.                 MainActivity.class);  
  11.         startActivity(main);  
  12.         finish();  
  13.         break;  
  14.             }  
  15.         case 1: {  
  16.         Intent list = new Intent(getApplicationContext(),  
  17.                 ListActivity.class);  
  18.         startActivity(list);  
  19.         finish();  
  20.         break;  
  21.         }  
  22.               
  23.         default:  
  24.         break;  
  25.          }  
  26.     }  
  27. }  

Download Code : Navigation Drawer Demo

Hope this will help someone.
Enjoy Coding...   :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.