Pages

Android Material Design (with Design support Library) - 4

Thursday, 4 June 2015



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
  • 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://schemas.android.com/apk/res-auto"  
   android:id="@+id/drawer_layout"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:fitsSystemWindows="true">  
   <!-- your content layout -->  
   <!--<FrameLayout  
     android:id="@+id/container"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent" />-->  
   <include layout="@layout/about_fragment"></include>  
   <android.support.design.widget.NavigationView  
     android:id="@+id/nav_draw"  
     android:layout_width="wrap_content"  
     android:layout_height="match_parent"  
     android:layout_gravity="start"  
     app:headerLayout="@layout/drawer_header"  
     app:menu="@menu/drawer" />  
 </android.support.v4.widget.DrawerLayout>  

drawer menus will be a collection of checkable menu items


 <?xml version="1.0" encoding="utf-8"?>  
 <menu xmlns:android="http://schemas.android.com/apk/res/android">  
   <group android:checkableBehavior="single">  
     <item  
       android:id="@+id/navigation_item_1"  
       android:checked="true"  
       android:icon="@drawable/ic_drawer_map"  
       android:title="@string/title_section1">  
     </item>  
     <item  
       android:id="@+id/navigation_item_2"  
       android:icon="@drawable/ic_drawer_people_met"  
       android:title="@string/title_section2" />  
     <item  
       android:id="@+id/navigation_item_3"  
       android:icon="@drawable/ic_drawer_experts"  
       android:title="@string/title_section3" />  
     <item  
       android:id="@+id/navigation_item_4"  
       android:icon="@drawable/ic_drawer_explore"  
       android:title="@string/title_section4" />  
     <item  
       android:id="@+id/navigation_item_5"  
       android:icon="@drawable/ic_drawer_social"  
       android:title="@string/title_section5" />  
     <item  
       android:id="@+id/navigation_item_6"  
       android:icon="@drawable/ic_drawer_social"  
       android:title="@string/title_section6" />  
     <item  
       android:id="@+id/navigation_subheader"  
       android:title="@string/navigation_subheader">  
       <menu>  
         <item  
           android:id="@+id/navigation_sub_item_1"  
           android:title="@string/navigation_sub_item_1" />  
         <item  
           android:id="@+id/navigation_sub_item_2"  
           android:title="@string/navigation_sub_item_2" />  
       </menu>  
     </item>  
   </group>  
 </menu>  

use navigation item Listener


 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener  

add override method


 @Override  
   public boolean onNavigationItemSelected(MenuItem menuItem) {  
     return false;  
   }  

in onCreate,


 NavigationView nav_draw = (NavigationView) findViewById(R.id.nav_draw);  
 nav_draw.setNavigationItemSelectedListener(this);  

Add Action, on Navigation Item select change onNavigationItemSelected method,


 @Override  
   public boolean onNavigationItemSelected(MenuItem menuItem) {  
     if (menuItem.getItemId() == R.id.navigation_item_1) {  
       Snackbar  
           .make(fab, "First item Selected", Snackbar.LENGTH_LONG)  
               //.setAction(R.string.snackbar_action, myOnClickListener)  
           .show();  
     } else if (menuItem.getItemId() == R.id.navigation_item_2) {  
       //startActivity(new Intent(MainActivity.this, DetailActivity.class));  
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
         getWindow().setExitTransition(new Explode());  
         Intent intent = new Intent(this, DetailActivity.class);  
         startActivity(intent,  
             ActivityOptionsCompat  
                 .makeSceneTransitionAnimation(this).toBundle());  
       } else {  
         startActivity(new Intent(MainActivity.this, DetailActivity.class));  
       }  
     } else if (menuItem.getItemId() == R.id.navigation_item_3) {  
       String url = "http://www.dj-android.blogspot.com";  
       Intent i = new Intent(Intent.ACTION_VIEW);  
       i.setData(Uri.parse(url));  
       startActivity(i);  
     } else if (menuItem.getItemId() == R.id.navigation_item_4) {  
       /*Snackbar  
           .make(root, "", Snackbar.LENGTH_LONG)  
               //.setAction(R.string.snackbar_action, myOnClickListener)  
           .setActionTextColor(getResources().getColor(R.color.colorRed))  
           .show();*/  
     }  
     menuItem.setChecked(true);  
     drawer_layout.closeDrawers();  
     return false;  
   }  

all this about navigation drawer view, no need to add extra fragment for drawer,

Floating Action Button


The Design library’s FloatingActionButton gives you a single consistent implementation, by default colored using the colorAccent from your theme.

 <android.support.design.widget.FloatingActionButton  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_margin="16dp"  
     android:clickable="true"  
     android:id="@+id/fab_action"  
     android:src="@drawable/ic_discuss"  
     app:layout_anchor="@id/appbar"  
     app:layout_anchorGravity="bottom|right|end" />  


Collapsing Toolbars




Adding a Toolbar directly to an AppBarLayout gives you access to the enterAlwaysCollapsed and exitUntilCollapsed scroll flags, but not the detailed control on how different elements react to collapsing. For that, you can use CollapsingToolbarLayout.


 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   android:id="@+id/main_content"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:fitsSystemWindows="true">  
   <android.support.design.widget.AppBarLayout  
     android:id="@+id/appbar"  
     android:layout_width="match_parent"  
     android:layout_height="200dip"  
     android:fitsSystemWindows="true"  
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">  
     <android.support.design.widget.CollapsingToolbarLayout  
       android:id="@+id/collapsing_toolbar"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:fitsSystemWindows="true"  
       app:contentScrim="?attr/colorPrimary"  
       app:expandedTitleMarginEnd="64dp"  
       app:expandedTitleMarginStart="48dp"  
       app:layout_scrollFlags="scroll|exitUntilCollapsed">  
       <ImageView  
         android:id="@+id/backdrop"  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         android:fitsSystemWindows="true"  
         android:scaleType="centerCrop"  
         android:src="@drawable/my_pic"  
         app:layout_collapseMode="parallax" />  
       <android.support.v7.widget.Toolbar  
         android:id="@+id/toolbar"  
         android:layout_width="match_parent"  
         android:layout_height="?attr/actionBarSize"  
         app:layout_collapseMode="pin"  
         app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />  
     </android.support.design.widget.CollapsingToolbarLayout>  
   </android.support.design.widget.AppBarLayout>  
   <android.support.v4.widget.NestedScrollView  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:padding="10dip"  
     app:layout_behavior="@string/appbar_scrolling_view_behavior">  
     <TextView  
       android:id="@+id/textView"  
       android:padding="10dip"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:elevation="5dp"  
       android:textColor="@color/textColorPrimary"  
       android:background="@drawable/myrect"  
       android:text="@string/intro"  
       android:textAppearance="?android:attr/textAppearanceMedium" />  
   </android.support.v4.widget.NestedScrollView>  
   <android.support.design.widget.FloatingActionButton  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_margin="16dp"  
     android:clickable="true"  
     android:id="@+id/fab_action"  
     android:src="@drawable/ic_discuss"  
     app:layout_anchor="@id/appbar"  
     app:layout_anchorGravity="bottom|right|end" />  
 </android.support.design.widget.CoordinatorLayout>  

check full code on github



Read more ...

Android Material Design - 3 (Android RecyclerView and CardView)

Saturday, 9 May 2015
Android Lollipop features two new widgets to make your life easier,   RecyclerView     and  CardView.



Download full example on Github.


RecyclerView

This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.

The RecyclerView and CardView both are available in the Android support library, to use support library in android studio.

your app root folder -> right click -> open module setting -> select your module -> select dependence tab -> click on + sign -> Library dependence, add both library.

com.android.support:recyclerview-v7:22.1.1
com.android.support:cardview-v7:22.1.1

now create layout for RecycleView :


 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin1"  
   android:paddingLeft="@dimen/activity_horizontal_margin1"  
   android:paddingRight="@dimen/activity_horizontal_margin1"  
   android:paddingTop="@dimen/activity_vertical_margin1"  
   tools:context=".MyActivity">  
   <android.support.v7.widget.RecyclerView  
     android:id="@+id/cardList"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent" />  
 </RelativeLayout>  


now, in your fragment add below code (you can also use activity just add code in onCreate)


  @Override  
   public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
     View rootView = inflater.inflate(R.layout.fragment_cardrecycle, container, false);  
     RecyclerView recList = (RecyclerView) rootView.findViewById(R.id.cardList);  
     recList.setHasFixedSize(true);  
     LinearLayoutManager llm = new LinearLayoutManager(getActivity());  
     llm.setOrientation(LinearLayoutManager.VERTICAL);  
     if (listitems.size() > 0 & recList != null) {  
       recList.setAdapter(new MyAdapter(listitems));  
     }  
     recList.setLayoutManager(llm);  
     return rootView;  
   }  

Before create adapter we ll design row items of RecycleView using CardView then after we 'll integrate both together.

create layout for CardView.


 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:card_view="http://schemas.android.com/apk/res-auto"  
   android:id="@+id/card_view"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:layout_margin="5dp"  
   card_view:cardCornerRadius="4dp">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="250dip"  
     android:orientation="vertical"  
     android:weightSum="4">  
     <LinearLayout  
       android:layout_width="match_parent"  
       android:layout_height="0dip"  
       android:layout_weight="3.2"  
       android:orientation="vertical">  
       <FrameLayout  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         android:layout_gravity="center_horizontal">  
         <ImageView  
           android:id="@+id/image_card_cover"  
           android:layout_width="match_parent"  
           android:layout_height="match_parent"  
           android:layout_gravity="center"  
           android:scaleType="centerCrop"  
           android:src="@drawable/my_pic" />  
         <LinearLayout  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"  
           android:layout_gravity="left|bottom"  
           android:background="@android:drawable/screen_background_dark_transparent"  
           android:orientation="vertical">  
           <TextView  
             android:id="@+id/text_card_name"  
             android:layout_width="match_parent"  
             android:layout_height="wrap_content"  
             android:paddingBottom="16dp"  
             android:paddingLeft="16dp"  
             android:paddingRight="16dp"  
             android:paddingTop="16dp"  
             android:text="Medium Text"  
             android:textAppearance="?android:attr/textAppearanceMedium"  
             android:textColor="@color/textColorPrimary"  
             android:textStyle="bold" />  
         </LinearLayout>  
         <android.support.v7.widget.Toolbar  
           android:id="@+id/card_toolbar"  
           android:layout_width="match_parent"  
           android:layout_height="?actionBarSize"  
           android:layout_gravity="left|top"  
           android:background="@null"  
           android:contentInsetStart="?actionBarInsetStart"  
           android:popupTheme="@style/ActionBarPopupThemeOverlay"  
           android:theme="@style/ActionBarThemeOverlay"  
           android:titleTextAppearance="@style/ActionBar.TitleText" />  
       </FrameLayout>  
     </LinearLayout>  
     <LinearLayout  
       android:layout_width="match_parent"  
       android:layout_height="0dip"  
       android:layout_weight="0.8"  
       android:gravity="center|right"  
       android:orientation="horizontal">  
       <ImageView  
         android:id="@+id/image_action_like"  
         android:layout_width="60dip"  
         android:layout_height="60dip"  
         android:padding="8dp"  
         android:src="@drawable/ic_action_action_favorite" />  
       <ImageView  
         android:id="@+id/image_action_flag"  
         android:layout_width="60dip"  
         android:layout_height="60dip"  
         android:padding="8dp"  
         android:src="@drawable/ic_action_action_turned_in" />  
       <ImageView  
         android:id="@+id/image_action_share"  
         android:layout_width="60dip"  
         android:layout_height="60dip"  
         android:padding="8dp"  
         android:src="@drawable/ic_action_social_share" />  
     </LinearLayout>  
   </LinearLayout>  
 </android.support.v7.widget.CardView>  


our CardView design look like..




The adapter provides access to the items in your data set, creates views for items, and replaces the content of some of the views with new data items when the original item is no longer visible.

create adapter,


 public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {  
     private ArrayList<CardViewModel> list;  
     // Provide a suitable constructor (depends on the kind of dataset)  
     public MyAdapter(ArrayList<CardViewModel> myDataset) {  
       list = myDataset;  
     }  
     // Create new views (invoked by the layout manager)  
     @Override  
     public ViewHolder onCreateViewHolder(ViewGroup parent,  
                        int viewType) {  
       // create a new view  
       View v = LayoutInflater.from(parent.getContext())  
           .inflate(R.layout.recycle_items, parent, false);  
       // set the view's size, margins, paddings and layout parameters  
       ViewHolder vh = new ViewHolder(v);  
       return vh;  
     }  
     // Replace the contents of a view (invoked by the layout manager)  
     @Override  
     public void onBindViewHolder(final ViewHolder holder, int position) {  
       // - get element from your dataset at this position  
       // - replace the contents of the view with that element  
       holder.text_card_name.setText(list.get(position).getCardName());  
       Drawable dr = getResources().getDrawable(list.get(position).getImageResourceId());  
       holder.image_card_cover.setImageDrawable(dr);  
       /*Palette.from(drawableToBitmap(dr)).generate(new Palette.PaletteAsyncListener() {  
         public void onGenerated(Palette p) {  
           // Use generated instance  
           //int titleBackColor = p.getVibrantColor(0);  
           int titleBackColor = p.getDarkVibrantColor(0);  
           //int titleBackColor = p.getVibrantSwatch();  
           ProgressDrawable titleBackDrawable;  
           if (titleBackColor != 0) {  
             titleBackDrawable = new ProgressDrawable(titleBackColor);  
           } else {  
             titleBackDrawable = new ProgressDrawable(getThemePrimaryColor());  
           }  
           //titleBackDrawable.setMax(holder.text_card_name.getHeight());  
           //titleBackDrawable.setProgress(holder.text_card_name.getHeight());  
           holder.text_card_name.setBackground(titleBackDrawable);  
         }  
       });*/  
     }  
     // Return the size of your dataset (invoked by the layout manager)  
     @Override  
     public int getItemCount() {  
       return list.size();  
     }  
   }  


here is ViewHolder class,



 // Provide a reference to the views for each data item  
   // Complex data items may need more than one view per item, and  
   // you provide access to all the views for a data item in a view holder  
   public static class ViewHolder extends RecyclerView.ViewHolder {  
     // each data item is just a string in this case  
     public TextView text_card_name;  
     public ImageView image_card_cover;  
     public ImageView image_action_like;  
     public ImageView image_action_flag;  
     public ImageView image_action_share;  
     public Toolbar maintoolbar;  
     public ViewHolder(View v) {  
       super(v);  
       text_card_name = (TextView) v.findViewById(R.id.text_card_name);  
       image_card_cover = (ImageView) v.findViewById(R.id.image_card_cover);  
       image_action_like = (ImageView) v.findViewById(R.id.image_action_like);  
       image_action_flag = (ImageView) v.findViewById(R.id.image_action_flag);  
       image_action_share = (ImageView) v.findViewById(R.id.image_action_share);  
       maintoolbar = (Toolbar) v.findViewById(R.id.card_toolbar);  
       maintoolbar.inflateMenu(R.menu.global);  
     }  
   }  


below is full source code of fragment,


 package com.djandroid.jdroid.materialdesign;  
 import android.content.res.TypedArray;  
 import android.graphics.Bitmap;  
 import android.graphics.Canvas;  
 import android.graphics.Color;  
 import android.graphics.drawable.BitmapDrawable;  
 import android.graphics.drawable.Drawable;  
 import android.os.Bundle;  
 import android.support.annotation.Nullable;  
 import android.support.v4.app.Fragment;  
 import android.support.v7.graphics.Palette;  
 import android.support.v7.widget.LinearLayoutManager;  
 import android.support.v7.widget.RecyclerView;  
 import android.support.v7.widget.Toolbar;  
 import android.util.TypedValue;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 import java.util.ArrayList;  
 /**  
  * Created by VCSDEV0100 on 5/4/2015.  
  */  
 public class CardRecycleFragment extends Fragment {  
   ArrayList<CardViewModel> listitems = new ArrayList<CardViewModel>();  
   //String items[] = {"Dhaval Sodha Parmar", "b", "v"};  
   RecyclerView recList;  
   @Override  
   public void onCreate(@Nullable Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setupListItems();  
   }  
   @Override  
   public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
     View rootView = inflater.inflate(R.layout.fragment_cardrecycle, container, false);  
     RecyclerView recList = (RecyclerView) rootView.findViewById(R.id.cardList);  
     recList.setHasFixedSize(true);  
     LinearLayoutManager llm = new LinearLayoutManager(getActivity());  
     llm.setOrientation(LinearLayoutManager.VERTICAL);  
     if (listitems.size() > 0 & recList != null) {  
       recList.setAdapter(new MyAdapter(listitems));  
     }  
     recList.setLayoutManager(llm);  
     return rootView;  
   }  
   @Override  
   public void onActivityCreated(@Nullable Bundle savedInstanceState) {  
     super.onActivityCreated(savedInstanceState);  
   }  
   public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {  
     private ArrayList<CardViewModel> list;  
     // Provide a suitable constructor (depends on the kind of dataset)  
     public MyAdapter(ArrayList<CardViewModel> myDataset) {  
       list = myDataset;  
     }  
     // Create new views (invoked by the layout manager)  
     @Override  
     public ViewHolder onCreateViewHolder(ViewGroup parent,  
                        int viewType) {  
       // create a new view  
       View v = LayoutInflater.from(parent.getContext())  
           .inflate(R.layout.recycle_items, parent, false);  
       // set the view's size, margins, paddings and layout parameters  
       ViewHolder vh = new ViewHolder(v);  
       return vh;  
     }  
     // Replace the contents of a view (invoked by the layout manager)  
     @Override  
     public void onBindViewHolder(final ViewHolder holder, int position) {  
       // - get element from your dataset at this position  
       // - replace the contents of the view with that element  
       holder.text_card_name.setText(list.get(position).getCardName());  
       Drawable dr = getResources().getDrawable(list.get(position).getImageResourceId());  
       holder.image_card_cover.setImageDrawable(dr);  
       /*Palette.from(drawableToBitmap(dr)).generate(new Palette.PaletteAsyncListener() {  
         public void onGenerated(Palette p) {  
           // Use generated instance  
           //int titleBackColor = p.getVibrantColor(0);  
           int titleBackColor = p.getDarkVibrantColor(0);  
           //int titleBackColor = p.getVibrantSwatch();  
           ProgressDrawable titleBackDrawable;  
           if (titleBackColor != 0) {  
             titleBackDrawable = new ProgressDrawable(titleBackColor);  
           } else {  
             titleBackDrawable = new ProgressDrawable(getThemePrimaryColor());  
           }  
           //titleBackDrawable.setMax(holder.text_card_name.getHeight());  
           //titleBackDrawable.setProgress(holder.text_card_name.getHeight());  
           holder.text_card_name.setBackground(titleBackDrawable);  
         }  
       });*/  
     }  
     // Return the size of your dataset (invoked by the layout manager)  
     @Override  
     public int getItemCount() {  
       return list.size();  
     }  
   }  
   // Provide a reference to the views for each data item  
   // Complex data items may need more than one view per item, and  
   // you provide access to all the views for a data item in a view holder  
   public static class ViewHolder extends RecyclerView.ViewHolder {  
     // each data item is just a string in this case  
     public TextView text_card_name;  
     public ImageView image_card_cover;  
     public ImageView image_action_like;  
     public ImageView image_action_flag;  
     public ImageView image_action_share;  
     public Toolbar maintoolbar;  
     public ViewHolder(View v) {  
       super(v);  
       text_card_name = (TextView) v.findViewById(R.id.text_card_name);  
       image_card_cover = (ImageView) v.findViewById(R.id.image_card_cover);  
       image_action_like = (ImageView) v.findViewById(R.id.image_action_like);  
       image_action_flag = (ImageView) v.findViewById(R.id.image_action_flag);  
       image_action_share = (ImageView) v.findViewById(R.id.image_action_share);  
       maintoolbar = (Toolbar) v.findViewById(R.id.card_toolbar);  
       maintoolbar.inflateMenu(R.menu.global);  
     }  
   }  
   public void setupListItems() {  
     listitems.clear();  
     CardViewModel item1 = new CardViewModel();  
     item1.setCardName("Dhawal Sodha Parmar");  
     item1.setImageResourceId(R.drawable.my_pic);  
     item1.setIsfav(0);  
     item1.setIsturned(0);  
     listitems.add(item1);  
     CardViewModel item2 = new CardViewModel();  
     item2.setCardName("Cart Item");  
     item2.setImageResourceId(R.drawable.header_bg);  
     item2.setIsfav(0);  
     item2.setIsturned(0);  
     listitems.add(item2);  
     /*CardViewModel item3 = new CardViewModel();  
     item3.setCardName("Cart Item");  
     item3.setImageResourceId(R.drawable.fc_tab_bg_new);  
     item3.setIsfav(0);  
     item3.setIsturned(0);  
     listitems.add(item3);*/  
     CardViewModel item4 = new CardViewModel();  
     item4.setCardName("Dhawal");  
     item4.setImageResourceId(R.drawable.my_profile);  
     item4.setIsfav(0);  
     item4.setIsturned(0);  
     listitems.add(item4);  
   }  
   public static Bitmap drawableToBitmap(Drawable drawable) {  
     if (drawable instanceof BitmapDrawable) {  
       return ((BitmapDrawable) drawable).getBitmap();  
     }  
     final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);  
     final Canvas canvas = new Canvas(bitmap);  
     drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());  
     drawable.draw(canvas);  
     return bitmap;  
   }  
   private int getThemePrimaryColor() {  
     final TypedValue typedValue = new TypedValue();  
     getActivity().getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);  
     int[] attribute = new int[]{R.attr.colorPrimary};  
     final TypedArray array = getActivity().obtainStyledAttributes(typedValue.resourceId, attribute);  
     return array.getColor(0, Color.MAGENTA);  
   }  
 }  


Download full example on Github.



WHAT IS NEXT?

Next time i will show you example of floating Toolbar when scroll content.

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

Any help and suggestions are appreciated. post comments below or you can contact me via E-mail.

Read more ...

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

Tuesday, 5 May 2015
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.

Read more ...