Skip to main content

Android Material Design - 2 (Good bay ActionBar, Hello ToolBar with Navigation Drawer)

hello all, after long time i have update my blog with android material Design theme, i am tying to demonstrate new things which is available with android lollipop. using different example i 'll show you, one sample app with material design which include ToolBar, Floting Actions, Tabs, Cardview, Recycle view.



Download full example on Github.

In this example we will take a look at the new Actionbar replacement called Toolbar.




It is introduced in Android Lollipop, API level 21 release, and hence it is available to use, out of box for the application that are targeted to 21 and above. However, as always Google provides fully supported Toolbar features to lower android os devices via AppCompact support library. In AppCompat, Toolbar is implemented in the android.support.v7.widget.Toolbar class.

Pre-requirements:

1. Android Studio 1.0.1 (Latest while writing the post)
2. Appcombat v7 Support library (To Support Pre Lollipop devices)

------------------------------------------------------------------------------------------------------------------------

1. Create an xml file named toolbar.xml under res ⇒ layout


 <?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="?attr/actionBarSize"  
   android:background="?attr/colorPrimary"  
   android:popupTheme="@style/ThemeOverlay.AppCompat.Light"  
   android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />  

2. Open the layout file of your main activity (activity_main.xml) and add the toolbar using <include/>tag.

 <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   tools:context=".MainActivity">  
   <LinearLayout  
     android:id="@+id/layout_main"  
     android:layout_width="match_parent"  
     android:layout_height="?attr/actionBarSize"  
     android:orientation="vertical">  
     <include  
       android:id="@+id/toolbar"  
       layout="@layout/toolbar" />  
   </LinearLayout>  
 </LinearLayout>  


3. create main.xml located under res ⇒ menu


 <menu 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"  
   tools:context=".MainActivity">  
   <item  
     android:id="@+id/action_example"  
     android:icon="@drawable/ic_action_action_autorenew"  
     android:title="@string/action_example"  
     app:showAsAction="ifRoom" />  
   <item  
     android:id="@+id/action_settings"  
     android:icon="@drawable/ic_action_action_autorenew"  
     android:orderInCategory="100"  
     android:title="@string/action_settings"  
     app:showAsAction="never" />  
 </menu>  


4. Now open your MainActivity.java, Extend the activity from AppCompatActivity (because ActionBarActivity has been deprecated in favor of the new AppCompatActivity, read more)


 public class MainActivity extends AppCompatActivity{  
   private Toolbar toolbar;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     toolbar = (Toolbar) findViewById(R.id.toolbar);  
     setSupportActionBar(toolbar);  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     // Inflate the menu; this adds items to the action bar if it is present.  
     getMenuInflater().inflate(R.menu.main, menu);  
     return true;  
   }  
   @Override  
   public boolean onOptionsItemSelected(MenuItem item) {  
     // Handle action bar item clicks here. The action bar will  
     // automatically handle clicks on the Home/Up button, so long  
     // as you specify a parent activity in AndroidManifest.xml.  
     int id = item.getItemId();  
     //noinspection SimplifiableIfStatement  
     if (id == R.id.action_settings) {  
       return true;  
     }  
     return super.onOptionsItemSelected(item);  
   }  
 }  


 add Navigation Drawer:





Adding navigation drawer is same as that we do before lollipop, you can use ListView for menu items but, i have seen google documentation of material design and i found some interesting in design guidelines.

 

so i have tried to flow guideline, thanks to android iosched app.

now create layout for drawer items in res ⇒ layout, create an xml layout named nav_drawer_row.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <com.djandroid.materialdesign.googleio.ScrimInsetsScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   android:id="@+id/navdrawer"  
   android:layout_width="@dimen/navigation_drawer_width"  
   android:layout_height="match_parent"  
   android:layout_gravity="start"  
   android:background="@color/textColorPrimary"  
   android:fitsSystemWindows="true"  
   app:insetForeground="#4000">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:orientation="vertical">  
     <FrameLayout  
       android:id="@+id/chosen_account_view"  
       android:layout_width="match_parent"  
       android:layout_height="@dimen/navdrawer_chosen_account_height">  
       <ImageView  
         android:id="@+id/profile_cover_image"  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         android:scaleType="centerCrop"  
         android:src="@drawable/my_pic"  
         android:tint="@color/session_photo_scrim" />  
       <RelativeLayout  
         android:id="@+id/chosen_account_content_view"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:paddingLeft="@dimen/keyline_1"  
         android:paddingRight="@dimen/keyline_1"  
         android:paddingTop="@dimen/keyline_1">  
         <com.djandroid.materialdesign.googleio.BezelImageView  
           android:id="@+id/profile_image"  
           android:layout_width="@dimen/navdrawer_profile_image_size"  
           android:layout_height="@dimen/navdrawer_profile_image_size"  
           android:scaleType="centerCrop"  
           android:src="@drawable/my_profile"  
           app:maskDrawable="@drawable/circle_mask" />  
         <ImageView  
           android:id="@+id/expand_account_box_indicator"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_alignParentBottom="true"  
           android:layout_alignParentEnd="true"  
           android:layout_marginLeft="16dp"  
           android:paddingBottom="16dp"  
           android:scaleType="center"  
           android:src="@drawable/ic_drawer_accounts_expand"  
           android:visibility="gone" />  
         <TextView  
           android:id="@+id/profile_email_text"  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"  
           android:layout_alignLeft="@id/profile_image"  
           android:layout_alignParentBottom="true"  
           android:layout_toLeftOf="@id/expand_account_box_indicator"  
           android:ellipsize="end"  
           android:maxLines="1"  
           android:paddingBottom="16dp"  
           android:singleLine="true"  
           android:text="dhaval0122@gmail.com"  
           android:textColor="@color/body_text_2_inverse"  
           android:textSize="@dimen/text_size_medium" />  
         <TextView  
           android:id="@+id/profile_name_text"  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"  
           android:layout_above="@id/profile_email_text"  
           android:layout_alignLeft="@id/profile_image"  
           android:layout_toLeftOf="@id/expand_account_box_indicator"  
           android:ellipsize="end"  
           android:maxLines="1"  
           android:singleLine="true"  
           android:text="Dhaval Sodha Parmar"  
           android:textColor="@color/body_text_1_inverse"  
           android:textSize="@dimen/text_size_large" />  
       </RelativeLayout>  
     </FrameLayout>  
     <FrameLayout  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content">  
       <!-- Account items -->  
       <LinearLayout  
         android:id="@+id/account_list"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:layout_marginTop="8dp"  
         android:orientation="vertical"  
         android:visibility="invisible" />  
       <!-- Drawer items -->  
       <LinearLayout  
         android:id="@+id/navdrawer_items_list"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:layout_marginBottom="8dp"  
         android:layout_marginTop="8dp"  
         android:orientation="vertical" />  
     </FrameLayout>  
   </LinearLayout>  
 </com.djandroid.materialdesign.googleio.ScrimInsetsScrollView>  


in above design i have user two custom classes ScrimInsetsScrollView and BezelImageView.
BezelImageView: that draws its contents inside a mask and draws a border drawable on top.
ScrimInsetsScrollView: status and navigation bars, overlay action bars.

now, in res ⇒ layout, create an xml layout named navdrawer_item.xml for drawer menu items

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="48dp"  
   android:background="?android:selectableItemBackground"  
   android:gravity="start|center_vertical"  
   android:orientation="horizontal"  
   android:paddingLeft="@dimen/keyline_1"  
   android:paddingRight="@dimen/keyline_1">  
   <ImageView  
     android:id="@+id/icon"  
     android:layout_width="24dp"  
     android:layout_height="24dp"  
     android:layout_marginRight="32dp" />  
   <TextView  
     android:id="@+id/title"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="start|center_vertical"  
     android:gravity="start|center_vertical"  
     android:text="@string/placeholder_lorem_ipsum"  
     android:textSize="14sp" />  
 </LinearLayout>  

in res ⇒ layout, create an xml layout named navdrawer_separator.xml for drawer menu items separator

 <?xml version="1.0" encoding="utf-8"?>  
 <View xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_marginTop="7dp"  
   android:layout_marginBottom="8dp"  
   android:gravity="start|center_vertical"  
   android:layout_width="match_parent"  
   android:background="#e5e5e5"  
   android:layout_height="1dp" />  


create a fragment named NavigationDrawFragment.java.


 package com.djandroid.materialdesign;  
 import android.app.Activity;  
 import android.content.Intent;  
 import android.content.SharedPreferences;  
 import android.os.Build;  
 import android.os.Bundle;  
 import android.os.Handler;  
 import android.preference.PreferenceManager;  
 import android.support.annotation.Nullable;  
 import android.support.v4.app.ActionBarDrawerToggle;  
 import android.support.v4.app.Fragment;  
 import android.support.v4.view.GravityCompat;  
 import android.support.v4.widget.DrawerLayout;  
 import android.support.v7.app.ActionBar;  
 import android.support.v7.app.AppCompatActivity;  
 import android.view.Gravity;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.ImageView;  
 import android.widget.ListView;  
 import android.widget.TextView;  
 import java.util.ArrayList;  
 /**  
  * Created by dhawal sodha parmar on 4/30/2015.  
  */  
 public class NavigationDrawFragment extends Fragment {  
   // delay to launch nav drawer item, to allow close animation to play  
   private static final int NAVDRAWER_LAUNCH_DELAY = 250;  
   // list of navdrawer items that were actually added to the navdrawer, in order  
   private ArrayList<Integer> mNavDrawerItems = new ArrayList<Integer>();  
   protected static final int NAVDRAWER_ITEM_FLOATING_ACTION = 0;  
   protected static final int NAVDRAWER_ITEM_TAB = 1;  
   protected static final int NAVDRAWER_ITEM_DJ = 2;  
   protected static final int NAVDRAWER_ITEM_ABOUT = 3;  
   protected static final int NAVDRAWER_ITEM_SOCIAL = 4;  
   protected static final int NAVDRAWER_ITEM_CARD = 5;  
   protected static final int NAVDRAWER_ITEM_INVALID = -1;  
   protected static final int NAVDRAWER_ITEM_SEPARATOR = -2;  
   protected static final int NAVDRAWER_ITEM_SEPARATOR_SPECIAL = -3;  
   private ViewGroup mDrawerItemsListContainer;  
   private View[] mNavDrawerItemViews = null;  
   // titles for navdrawer items (indices must correspond to the above)  
   private static final int[] NAVDRAWER_TITLE_RES_ID = new int[]{  
       R.string.title_section1,  
       R.string.title_section2,  
       R.string.title_section3,  
       R.string.title_section4,  
       R.string.title_section5,  
       R.string.title_section6  
   };  
   // icons for navdrawer items (indices must correspond to above array)  
   private static final int[] NAVDRAWER_ICON_RES_ID = new int[]{  
       R.drawable.ic_drawer_map, // Floating action  
       R.drawable.ic_drawer_people_met, // about  
       R.drawable.ic_drawer_experts, // dj  
       R.drawable.ic_drawer_explore,// about  
       R.drawable.ic_drawer_social, // social  
       R.drawable.ic_drawer_social, // social  
   };  
   private View mFragmentContainerView;  
   private DrawerLayout mDrawerLayout;  
   private ActionBarDrawerToggle mDrawerToggle;  
   //private boolean mUserLearnedDrawer;  
   private boolean mFromSavedInstanceState;  
   /**  
    * A pointer to the current callbacks instance (the Activity).  
    */  
   private NavigationDrawerCallbacks mCallbacks;  
   private Handler mHandler;  
   @Override  
   public void onCreate(@Nullable Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     if (savedInstanceState != null) {  
       mFromSavedInstanceState = true;  
     }  
     mHandler = new Handler();  
     populateNavDrawer();  
   }  
   @Override  
   public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
     View mRootView = (View) inflater.inflate(  
         R.layout.google_io_drawer, container, false);  
     mDrawerItemsListContainer = (ViewGroup) mRootView.findViewById(R.id.navdrawer_items_list);  
     createNavDrawerItems();  
     return mRootView;  
   }  
   public boolean isDrawerOpen() {  
     return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);  
   }  
   public void setUp(int fragmentId, DrawerLayout drawerLayout) {  
     mFragmentContainerView = getActivity().findViewById(fragmentId);  
     mDrawerLayout = drawerLayout;  
     // set a custom shadow that overlays the main content when the drawer opens  
     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);  
     // set up the drawer's list view with items and click listener  
     ActionBar actionBar = getActionBar();  
     actionBar.setDisplayHomeAsUpEnabled(true);  
     actionBar.setHomeButtonEnabled(true);  
     // ActionBarDrawerToggle ties together the the proper interactions  
     // between the navigation drawer and the action bar app icon.  
     mDrawerToggle = new ActionBarDrawerToggle(  
         getActivity(),          /* host Activity */  
         mDrawerLayout,          /* DrawerLayout object */  
         R.drawable.ic_drawer,       /* nav drawer image to replace 'Up' caret */  
         R.string.navigation_drawer_open, /* "open drawer" description for accessibility */  
         R.string.navigation_drawer_close /* "close drawer" description for accessibility */  
     ) {  
       @Override  
       public void onDrawerClosed(View drawerView) {  
         super.onDrawerClosed(drawerView);  
         if (!isAdded()) {  
           return;  
         }  
         getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()  
       }  
       @Override  
       public void onDrawerOpened(View drawerView) {  
         super.onDrawerOpened(drawerView);  
         if (!isAdded()) {  
           return;  
         }  
         //if (!mUserLearnedDrawer) {  
         // The user manually opened the drawer; store this flag to prevent auto-showing  
         // the navigation drawer automatically in the future.  
         // mUserLearnedDrawer = true;  
         //SharedPreferences sp = PreferenceManager  
         // .getDefaultSharedPreferences(getActivity());  
         // sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();  
         //}  
         getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()  
       }  
     };  
     // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,  
     // per the navigation drawer design guidelines.  
     //if (!mUserLearnedDrawer && !mFromSavedInstanceState) {  
     if (!mFromSavedInstanceState) {  
       mDrawerLayout.openDrawer(mFragmentContainerView);  
     }  
     // Defer code dependent on restoration of previous instance state.  
     mDrawerLayout.post(new Runnable() {  
       @Override  
       public void run() {  
         mDrawerToggle.syncState();  
       }  
     });  
     mDrawerLayout.setDrawerListener(mDrawerToggle);  
   }  
   private ActionBar getActionBar() {  
     return ((AppCompatActivity) getActivity()).getSupportActionBar();  
   }  
   private void createNavDrawerItems() {  
     if (mDrawerItemsListContainer == null) {  
       return;  
     }  
     mNavDrawerItemViews = new View[mNavDrawerItems.size()];  
     mDrawerItemsListContainer.removeAllViews();  
     int i = 0;  
     for (int itemId : mNavDrawerItems) {  
       mNavDrawerItemViews[i] = makeNavDrawerItem(itemId, mDrawerItemsListContainer);  
       mDrawerItemsListContainer.addView(mNavDrawerItemViews[i]);  
       ++i;  
     }  
   }  
   private View makeNavDrawerItem(final int itemId, ViewGroup container) {  
     boolean selected = getSelfNavDrawerItem() == itemId;  
     int layoutToInflate = 0;  
     if (itemId == NAVDRAWER_ITEM_SEPARATOR) {  
       layoutToInflate = R.layout.navdrawer_separator;  
     } else if (itemId == NAVDRAWER_ITEM_SEPARATOR_SPECIAL) {  
       layoutToInflate = R.layout.navdrawer_separator;  
     } else {  
       layoutToInflate = R.layout.navdrawer_item;  
     }  
     View view = getActivity().getLayoutInflater().inflate(layoutToInflate, container, false);  
     if (isSeparator(itemId)) {  
       // we are done  
       setAccessibilityIgnore(view);  
       return view;  
     }  
     ImageView iconView = (ImageView) view.findViewById(R.id.icon);  
     TextView titleView = (TextView) view.findViewById(R.id.title);  
     int iconId = itemId >= 0 && itemId < NAVDRAWER_ICON_RES_ID.length ?  
         NAVDRAWER_ICON_RES_ID[itemId] : 0;  
     int titleId = itemId >= 0 && itemId < NAVDRAWER_TITLE_RES_ID.length ?  
         NAVDRAWER_TITLE_RES_ID[itemId] : 0;  
     // set icon and text  
     iconView.setVisibility(iconId > 0 ? View.VISIBLE : View.GONE);  
     if (iconId > 0) {  
       iconView.setImageResource(iconId);  
     }  
     titleView.setText(getString(titleId));  
     formatNavDrawerItem(view, itemId, selected);  
     view.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         onNavDrawerItemClicked(itemId);  
       }  
     });  
     return view;  
   }  
   private void formatNavDrawerItem(View view, int itemId, boolean selected) {  
     if (isSeparator(itemId)) {  
       // not applicable  
       return;  
     }  
     ImageView iconView = (ImageView) view.findViewById(R.id.icon);  
     TextView titleView = (TextView) view.findViewById(R.id.title);  
     if (selected) {  
       view.setBackgroundResource(R.drawable.selected_navdrawer_item_background);  
     } else {  
       view.setBackgroundResource(R.drawable.navdrawer_item_background);  
     }  
     // configure its appearance according to whether or not it's selected  
     titleView.setTextColor(selected ?  
         getResources().getColor(R.color.navdrawer_text_color_selected) :  
         getResources().getColor(R.color.navdrawer_text_color));  
     iconView.setColorFilter(selected ?  
         getResources().getColor(R.color.navdrawer_icon_tint_selected) :  
         getResources().getColor(R.color.navdrawer_icon_tint));  
   }  
   private void onNavDrawerItemClicked(final int itemId) {  
     if (itemId == getSelfNavDrawerItem()) {  
       mDrawerLayout.closeDrawer(Gravity.START);  
       return;  
     }  
     // change the active item on the list so the user can see the item changed  
     setSelectedNavDrawerItem(itemId);  
     if (mCallbacks != null) {  
       mCallbacks.onNavigationDrawerItemSelected(itemId);  
     }  
     // fade out the main content  
       /*View mainContent = findViewById(R.id.main_content);  
       if (mainContent != null) {  
         mainContent.animate().alpha(0).setDuration(MAIN_CONTENT_FADEOUT_DURATION);  
       }*/  
     //}  
     mDrawerLayout.closeDrawer(Gravity.START);  
   }  
   /**  
    * Sets up the given navdrawer item's appearance to the selected state. Note: this could  
    * also be accomplished (perhaps more cleanly) with state-based layouts.  
    */  
   private void setSelectedNavDrawerItem(int itemId) {  
     if (mNavDrawerItemViews != null) {  
       for (int i = 0; i < mNavDrawerItemViews.length; i++) {  
         if (i < mNavDrawerItems.size()) {  
           int thisItemId = mNavDrawerItems.get(i);  
           formatNavDrawerItem(mNavDrawerItemViews[i], thisItemId, itemId == thisItemId);  
         }  
       }  
     }  
   }  
   @Override  
   public void onAttach(Activity activity) {  
     super.onAttach(activity);  
     try {  
       mCallbacks = (NavigationDrawerCallbacks) activity;  
     } catch (ClassCastException e) {  
       throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");  
     }  
   }  
   @Override  
   public void onDetach() {  
     super.onDetach();  
     mCallbacks = null;  
   }  
   /**  
    * Populates the navigation drawer with the appropriate items.  
    */  
   private void populateNavDrawer() {  
     mNavDrawerItems.clear();  
     mNavDrawerItems.add(NAVDRAWER_ITEM_FLOATING_ACTION);  
     mNavDrawerItems.add(NAVDRAWER_ITEM_TAB);  
     mNavDrawerItems.add(NAVDRAWER_ITEM_CARD);  
     mNavDrawerItems.add(NAVDRAWER_ITEM_SEPARATOR);  
     mNavDrawerItems.add(NAVDRAWER_ITEM_DJ);  
     mNavDrawerItems.add(NAVDRAWER_ITEM_ABOUT);  
     mNavDrawerItems.add(NAVDRAWER_ITEM_SEPARATOR);  
     mNavDrawerItems.add(NAVDRAWER_ITEM_SOCIAL);  
     //createNavDrawerItems();  
   }  
   private boolean isSeparator(int itemId) {  
     return itemId == NAVDRAWER_ITEM_SEPARATOR || itemId == NAVDRAWER_ITEM_SEPARATOR_SPECIAL;  
   }  
   public static void setAccessibilityIgnore(View view) {  
     view.setClickable(false);  
     view.setFocusable(false);  
     view.setContentDescription("");  
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
       view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);  
     }  
   }  
   /**  
    * Returns the navigation drawer item that corresponds to this Activity. Subclasses  
    * of BaseActivity override this to indicate what nav drawer item corresponds to them  
    * Return NAVDRAWER_ITEM_INVALID to mean that this Activity should not have a Nav Drawer.  
    */  
   protected int getSelfNavDrawerItem() {  
     return NAVDRAWER_ITEM_INVALID;  
   }  
   private boolean isSpecialItem(int itemId) {  
     return itemId != NAVDRAWER_ITEM_DJ;  
   }  
 }  

Open the layout file of your main activity (activity_main.xml) modify as below.


 <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   tools:context=".MainActivity">  
   <LinearLayout  
     android:id="@+id/layout_main"  
     android:layout_width="match_parent"  
     android:layout_height="?attr/actionBarSize"  
     android:orientation="vertical">  
     <include  
       android:id="@+id/toolbar"  
       layout="@layout/toolbar" />  
   </LinearLayout>  
   <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     xmlns:tools="http://schemas.android.com/tools"  
     android:id="@+id/drawer_layout"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent">  
     <FrameLayout  
       android:id="@+id/container"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent" />  
     <!-- As the main content view, the view below consumes the entire  
        space available using match_parent in both dimensions. -->  
     <!-- android:layout_gravity="start" tells DrawerLayout to treat  
        this as a sliding drawer on the left side for left-to-right  
        languages and on the right side for right-to-left languages.  
        If you're not building against API 17 or higher, use  
        android:layout_gravity="left" instead. -->  
     <!-- The drawer is given a fixed width in dp and extends the full height of  
        the container. -->  
     <fragment  
       android:id="@+id/navigation_drawer"  
       android:name="com.djandroid.materialdesign.NavigationDrawFragment"  
       android:layout_width="@dimen/navigation_drawer_width"  
       android:layout_height="match_parent"  
       android:layout_gravity="start"  
       tools:layout="@layout/fragment_navigation_drawer" />  
   </android.support.v4.widget.DrawerLayout>  
 </LinearLayout>  


now, open your MainActivity class, modify it


 public class MainActivity extends AppCompatActivity  
     implements NavigationDrawerCallbacks {  
   /**  
    * Fragment managing the behaviors, interactions and presentation of the navigation drawer.  
    */  
   private NavigationDrawFragment mNavigationDrawerFragment;  
   /**  
    * Used to store the last screen title. For use in {@link #restoreActionBar()}.  
    */  
   private CharSequence mTitle;  
   private ActionBarDrawerToggle mDrawerToggle;  
   DrawerLayout drawer_layout;  
   ActionBar actionBar;  
   Toolbar toolbar;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     toolbar = (Toolbar) findViewById(R.id.toolbar);  
     setSupportActionBar(toolbar);  
     mNavigationDrawerFragment = (NavigationDrawFragment)  
         getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);  
     mTitle = getTitle();  
     drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);  
     // Set up the drawer.  
     mNavigationDrawerFragment.setUp(  
         R.id.navigation_drawer,  
         drawer_layout);  
     mDrawerToggle = new ActionBarDrawerToggle(this, drawer_layout, R.string.drawer_open, R.string.drawer_close) {  
       //** Called when a drawer has settled in a completely open state. *//*  
       public void onDrawerOpened(View drawerView) {  
         super.onDrawerOpened(drawerView);  
         invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()  
       }  
       //** Called when a drawer has settled in a completely closed state. *//*  
       public void onDrawerClosed(View view) {  
         super.onDrawerClosed(view);  
         invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()  
       }  
     };  
     mDrawerToggle.setDrawerIndicatorEnabled(true);  
     drawer_layout.setDrawerListener(mDrawerToggle);  
   }  
   @Override  
   public void onNavigationDrawerItemSelected(int position) {  
     // update the main content by replacing fragments  
     switch (position) {  
       case NavigationDrawFragment.NAVDRAWER_ITEM_FLOATING_ACTION:  
         break;  
       case NavigationDrawFragment.NAVDRAWER_ITEM_TAB:  
         onSectionAttached(2);  
         break;  
       case NavigationDrawFragment.NAVDRAWER_ITEM_CARD:  
         onSectionAttached(6);  
         break;  
       case NavigationDrawFragment.NAVDRAWER_ITEM_DJ:  
         onSectionAttached(3);  
         break;  
       case NavigationDrawFragment.NAVDRAWER_ITEM_ABOUT:  
         onSectionAttached(4);  
         break;  
       case NavigationDrawFragment.NAVDRAWER_ITEM_SOCIAL:  
         onSectionAttached(5);  
         break;  
       default:  
         break;  
     }  
   }  
   public void onSectionAttached(int number) {  
     Log.e("number", "--->" + number);  
     switch (number) {  
       case 1:  
         mTitle = getString(R.string.title_section1);  
         break;  
       case 2:  
         mTitle = getString(R.string.title_section2);  
         break;  
       case 3:  
         mTitle = getString(R.string.title_section3);  
         break;  
       case 4:  
         mTitle = getString(R.string.title_section4);  
         break;  
       case 5:  
         mTitle = getString(R.string.title_section5);  
         break;  
       case 6:  
         mTitle = getString(R.string.title_section6);  
         break;  
     }  
   }  
   public void restoreActionBar() {  
     actionBar = getSupportActionBar();  
     //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
     actionBar.setDisplayShowTitleEnabled(true);  
     actionBar.setTitle(mTitle);  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     if (!mNavigationDrawerFragment.isDrawerOpen()) {  
       // Only show items in the action bar relevant to this screen  
       // if the drawer is not showing. Otherwise, let the drawer  
       // decide what to show in the action bar.  
       getMenuInflater().inflate(R.menu.main, menu);  
       restoreActionBar();  
       return true;  
     }  
     return super.onCreateOptionsMenu(menu);  
   }  
   @Override  
   public boolean onOptionsItemSelected(MenuItem item) {  
     // Handle action bar item clicks here. The action bar will  
     // automatically handle clicks on the Home/Up button, so long  
     // as you specify a parent activity in AndroidManifest.xml.  
     int id = item.getItemId();  
     //noinspection SimplifiableIfStatement  
     if (id == R.id.action_settings) {  
       customNotification();  
       return true;  
     }  
     if (mDrawerToggle.onOptionsItemSelected(item)) {  
       return true;  
     }  
     return super.onOptionsItemSelected(item);  
   }  
   @Override  
   public boolean onPrepareOptionsMenu(Menu menu) {  
     mDrawerToggle.syncState();  
     return super.onPrepareOptionsMenu(menu);  
   }  
   @Override  
   public void onConfigurationChanged(Configuration newConfig) {  
     super.onConfigurationChanged(newConfig);  
     mDrawerToggle.onConfigurationChanged(newConfig);  
   }  
 }  



Download full example on Github.


WHAT IS NEXT?

next, i 'll update detailed example of recycle view and Card view.

NOTE: my all example related material design is only for Android API 14 to 21.

till then any suggestions are appreciated.

Comments

  1. how can select the 1st iteam as soon as open app and navaigation menu should be in closed state.

    ReplyDelete

Post a Comment

Popular posts from this blog

ANDROID - Adding ActionBar Navigation Tabs

Note: if you are develop App < 3.0 Android OS then use  ActionBarSherlock   support library. ActionBarSherlock is an extension of the  support library  designed to facilitate the use of the action bar design pattern across all versions of Android with a single API. Create new Android Project : in Main Activity package com.AlarmManager; import android.os.Bundle; import android.view.View; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; public class AlarmManagerActivity extends SherlockFragmentActivity { public static String ACTIVE_TAB = "activeTab"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); super.onCreate(savedInstanceState); // setContentView(R.lay

Android Material Design (with Design support Library) - 4

Introducing  Design Support Library  , to use Material design component in older version API - android 2.1 and above.  You’ll find a navigation drawer view, floating labels for editing text, a floating action button, snackbar, tabs, and a motion and scroll framework to tie them together. I have used my previous example, so its easy for demonstrate.  Note: Update your Android SDK support  repositories, support library if not updated i  used compileSdkVersion 'android-MNC' for Android M but you can change it to build in older API add  dependencies in build.gradle file compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' compile 'com.android.support:support-v4:22.2.0' start with  navigation drawer , its very easy to use lets, design for drawer <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l