Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing Material Design in Android

anjhero
December 20, 2014

Implementing Material Design in Android

Presented at Google DevFest Kathmandu 2014

anjhero

December 20, 2014
Tweet

More Decks by anjhero

Other Decks in Technology

Transcript

  1. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  2. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  3. Paper   Ink   The material on which ink is

    drawn.   Every pixel drawn by an app on a piece of paper  
  4. Paper   Ink   The material on which ink is

    drawn.   Every pixel drawn by an app on a piece of paper   A  very  good  a+ernoon  to   you  
  5. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  6. Toolbar Two ways to use toolbar •  Use toolbar as

    an actionbar •  Use it as a standalone toolbar
  7. Toolbar Two ways to use toolbar •  Use toolbar as

    an actionbar •  Use it as a standalone toolbar
  8. Toolbar Two ways to use toolbar •  Use toolbar as

    an actionbar •  Use it as a standalone toolbar •  Not as an actionbar •  You need to populate the Toolbar with content/ actions manually
  9. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  10. Appcompat // include the library in your project dependencies {

    compile 'com.android.support:appcompat- v7:21.+' }
  11. <style name="AppTheme" parent="Theme.AppCompat"> <!-- Used as the action bar background

    --> <item name="colorPrimary">@color/ color_primary</item> <!-- Used in the status bar --> <item name="colorPrimaryDark">@color/ color_primary_dark</item> <!-- colorAccent used to tint widgets--> <item name="colorAccent">@color/accent</item> <!-- disable the window action bar --> <item name="windowActionBar">false</item> </style>
  12. <style name="AppTheme" parent="Theme.AppCompat"> <!-- Used as the action bar background

    --> <item name="colorPrimary">@color/ color_primary</item> <!-- Used in the status bar --> <item name="colorPrimaryDark">@color/ color_primary_dark</item> <!-- colorAccent used to tint widgets--> <item name="colorAccent">@color/accent</item> <!-- disable the window action bar --> <item name="windowActionBar">false</item> </style>
  13. <style name="AppTheme" parent="Theme.AppCompat"> <!-- Used as the action bar background

    --> <item name="colorPrimary">@color/ color_primary</item> <!-- Used in the status bar --> <item name="colorPrimaryDark">@color/ color_primary_dark</item> <!-- colorAccent used to tint widgets--> <item name="colorAccent">@color/accent</item> <!-- disable the window action bar --> <item name="windowActionBar">false</item> </style>
  14. <style name="AppTheme" parent="Theme.AppCompat"> <!-- Used as the action bar background

    --> <item name="colorPrimary">@color/ color_primary</item> <!-- Used in the status bar --> <item name="colorPrimaryDark">@color/ color_primary_dark</item> <!-- colorAccent used to tint widgets--> <item name="colorAccent">@color/accent</item> <!-- disable the window action bar --> <item name="windowActionBar">false</item> </style>
  15. <style name="AppTheme" parent="Theme.AppCompat"> <!-- Used as the action bar background

    --> <item name="colorPrimary">@color/ color_primary</item> <!-- Used in the status bar --> <item name="colorPrimaryDark">@color/ color_primary_dark</item> <!-- colorAccent used to tint widgets--> <item name="colorAccent">@color/accent</item> <!-- disable the window action bar --> <item name="windowActionBar">false</item> </style>
  16. Palette also lets you extract   Colors for Title and

    Body   Vibrant   Title color generated from Vibrant   Body text color generated from Vibrant  
  17. // include the library in your project dependencies { compile

    'com.android.support:palette- v7:21.+' }
  18. // initialization Palette.generate(Bitmap bitmap); Palette.generateAsync(Bitmap bitmap, PaletteAyncListener listener); // fetch

    color int vibrant = palette.getVibrantColor(DEFAULT_COLOR); // set color myView.setBackgroundColor(vibrant);
  19. // fetch Swatch vibrantSwatch = palette.getVibrantSwatch(); // fetch colors from

    Swatch int color = vibrantSwatch.getRgb(); int titleColor = vibrantSwatch.getTitleTextColor(); Int bodyColor = vibrantSwatch.getBodyTextColor();
  20. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  21. // include the library in your project dependencies { compile

    'com.android.support:cardview- v7:21.+' } CardView
  22. // include the library in your project dependencies { compile

    'com.android.support:recyclerview- v7:21.+' } RecyclerView
  23. private RecyclerView mRecyclerView; private RecyclerView.LayoutManager mLayoutManager; private RecyclerView.Adapter mAdapter; @Override

    protected void onCreate(Bundle savedInstanceState) { ... mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new MyAdapter(dataset); mRecyclerView.setAdapter(mAdapter); } RecyclerView
  24. private RecyclerView mRecyclerView; private RecyclerView.LayoutManager mLayoutManager; private RecyclerView.Adapter mAdapter; @Override

    protected void onCreate(Bundle savedInstanceState) { ... mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new MyAdapter(dataset); mRecyclerView.setAdapter(mAdapter); } RecyclerView
  25. private RecyclerView mRecyclerView; private RecyclerView.LayoutManager mLayoutManager; private RecyclerView.Adapter mAdapter; @Override

    protected void onCreate(Bundle savedInstanceState) { ... mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new MyAdapter(dataset); mRecyclerView.setAdapter(mAdapter); } RecyclerView
  26. class ViewHolder extends RecyclerView.ViewHolder { // override the superclass implementation

    public ViewHolder(View v) { super(v); // do your findViewById's here } } RecyclerView
  27. Class MyAdapter extends RecyclerView.Adapter<ViewHolder> { @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup,

    int i) { return null; } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { } @Override public int getItemCount() { return 0; } } RecyclerView
  28. class BaseAdapter extends RecyclerView.Adapter<ViewHolder> { @Override public ViewHolder onCreateViewHolder(ViewGroup group,

    int i) { // set layout and return the ViewHolder View v = LayoutInflater.from(context).inflate(layout, group, false); ViewHolder vh = new ViewHolder(v); return vh; } ... } RecyclerView
  29. class BaseAdapter extends RecyclerView.Adapter<ViewHolder> { @Override public void onBindViewHolder(ViewHolder vh,

    int i) { // load appropriate data into views vh.getBody().setText(data.get(i)); } ... } RecyclerView
  30. class BaseAdapter extends RecyclerView.Adapter<ViewHolder> { @Override public int getItemCount() {

    // return your dataset size return data.size(); } ... } RecyclerView
  31. References … Grokking Android http://goo.gl/LSXySl Antonio Leiva http://goo.gl/DQIpYv BigNerdRanch http://goo.gl/YFvI4Z

    Android docs http://goo.gl/ZqGD2v Android Developers Backstage http://goo.gl/k4Wpsd
  32. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  33. // Communicate between Activity A and B // Activity A

    pair = new Pair<View, String>(commonView, IMG); ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activit y, pair)); startActivity(intent, options.toBundle()); Activity Transitions
  34. // Communicate between Activity A and B // Activity B

    ViewCompat.setTransitionName(commonView, IMG); Activity Transitions
  35. <!-- include ripple drawables in your drawable-v21 file -- >

    <!-- unbounded ripple --> <ripple ... android:color="?attr/colorPrimaryDark"/> Ripple
  36. <!-- ripple with mask --> <ripple ... android:color="?attr/colorPrimaryDark"> <item android:id="@android:id/mask">

    <shape android:shape="rectangle"> <solid android:color="?attr/colorPrimary"/> </shape> </item> </ripple> Ripple
  37. // use static method to generate required reveal Animator reveal

    = ViewAnimationUtils.createCircularReveal(view, x, y, startRadius, endRadius) Reveal
  38. // use static method to generate required reveal Animator reveal

    = ViewAnimationUtils.createCircularReveal(view, x, y, startRadius, endRadius) // register a listener to hide/unhide view reveal.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { view.setVisibility(VISIBILITY); } }); Reveal
  39. Vector Drawables •  This lets you create a drawable based

    on an XML vector graphic. •  It can be defined in an XML file with the <vector> element, which wasn't previously available.
  40. <!-- Include in your drawables-v21 file --> <vector ...> <path

    ... android:pathData="@string/heart"/> <group ... android:name="heart_group"> <path ... android:name="heart"/> </group> </vector> Vector Drawables
  41. <!-- Include in your drawables-v21 file --> <vector ...> <path

    ... android:pathData="@string/heart"/> <group ... android:name="heart_group"> <path ... android:name="heart"/> </group> </vector> Vector Drawables
  42. <!-- Include in your drawables-v21 file --> <vector ...> <path

    ... android:pathData="@string/heart"/> <group ... android:name="heart_group"> <path ... android:name="heart"/> </group> </vector> Vector Drawables
  43. <!-- Include in your drawables-v21 file --> <vector ...> <path

    ... android:pathData="@string/heart"/> <group ... android:name="heart_group"> <path ... android:name="heart"/> </group> </vector> Vector Drawables
  44. <!-- Include in your drawables-v21 file --> <animated-vector android:drawable="@drawable/heart"> <target

    android:name="heart_group" android:animation="@anim/appear" /> <target android:name="heart" android:animation="@anim/gray_to_pink" /> </animated-vector> AnimatedVectorDrawables
  45. <!-- Include in your drawables-v21 file --> <animated-vector android:drawable="@drawable/heart"> <target

    android:name="heart_group" android:animation="@anim/appear" /> <target android:name="heart" android:animation="@anim/gray_to_pink" /> </animated-vector> AnimatedVectorDrawables
  46. <!-- Include in your drawables-v21 file --> <animated-vector android:drawable="@drawable/heart"> <target

    android:name="heart_group" android:animation="@anim/appear" /> <target android:name="heart" android:animation="@anim/gray_to_pink" /> </animated-vector> AnimatedVectorDrawables
  47. <!-- Include in your drawables-v21 file --> <animated-vector android:drawable="@drawable/heart"> <target

    android:name="heart_group" android:animation="@anim/appear" /> <target android:name="heart" android:animation="@anim/gray_to_pink" /> </animated-vector> AnimatedVectorDrawables
  48. References … Android Docs http://goo.gl/UCB6Nx http://goo.gl/IG7qre http://goo.gl/0z2Qys Roman Guy http://goo.gl/irAKpI

    Square Island http://goo.gl/mvVP8X Vector Drawable Generator http://goo.gl/F6TRlg
  49. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  50. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  51. Floating Action Button The primary action of your app Small

    icon badging When your app has a large icon and small icon
  52. Floating Action Button The primary action of your app Head's

    up notifications FullScreen activity (fullScreenIntent) Notification has high priority and uses ringtones or vibrations
  53. Lock screen notifications VISIBILITY_PUBLIC // lock screen notifications VISIBILITY_SECRET //

    doesn't show any part of this notification on the lock screen VISIBILITY_PRIVATE // shows basic information, such as the notification's icon and the content title, but hides the notification's full content.
  54. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  55. What’s new in overview? Configurable overview cards xml (manifest) Icon

    - android:icon Label text - android:label Top Bar Color - colorPrimary value of android:theme
  56. What’s new in overview? Configurable overview cards Programmatically ActivityManager.TaskDescription td

    = new ActivityManager.TaskDescription(caption, bitmap, color); setTaskDescription(td);
  57. What’s new in overview? Support for multi-tasking // Done with

    intent flags Intent.FLAG_ACTIVITY_NEW_DOCUMENT Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS
  58. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources
  59. Jobscheduler An API for scheduling various types of jobs against

    the framework that will be executed in your application's own process.
  60. Jobscheduler •  Configurable •  Persists across reboot (Yayyy !!!) • 

    Easy to use API •  Easy to handle failures and retries
  61. public class MyJobService extends JobService { @Override public boolean onStartJob(JobParameters

    jobParameters) { ... } @Override public boolean onStopJob(JobParameters jobParameters) { ... } }
  62. JobService Write a JobService •  Add to manifest •  Requires

    android.permission.BIND_JOB_SERVICE •  Override onStartJob() •  Override onStopJob()
  63. JobService – onStartJob() Override this method with the callback logic

    for your job Call jobFinised() after your job is done Return true if your service needs to process the work (on a separate thread). false if there's no more work to be done for this job.
  64. JobService – onStopJob() Called by the system before you've had

    a chance to call jobFinished() When the conditions you defined no longer persists Free up resources held by your logic Return true to indicate to reschedule this job based on the retry criteria provided at job creation-time. false to drop the job.
  65. 1.  Intro 2.  App bar 3.  Color 4.  Cards 5. 

    Animations 6.  FAB 7.  Notifications 8.  Overview 9.  Project Volta 10.  Resources