Share
Embed
Implementing Material Design in Android
Presented at Google DevFest Kathmandu 2014
anjhero
December 20, 2014
Transcript
163 slides
-
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
Everything you see resides on a paper meet
MATERIAL DESIGN -
Paper Ink The material on which ink is
drawn. Every pixel drawn by an app on a piece of paper -
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 -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
App bar A bar that provides: • Information of where
you are • Important actions -
App bar A bar that provides: • Information of where
you are • Important actions -
Toolbar Two ways to use toolbar • Use toolbar as
an actionbar • Use it as a standalone toolbar -
@Override public void onCreate(Bundle savedInstanceState) { ... toolbar = (Toolbar)
findViewById(R.id.toolbar); setActionBar(toolbar); } -
Toolbar Two ways to use toolbar • Use toolbar as
an actionbar • Use it as a standalone toolbar -
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 -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
Appcompat // include the library in your project dependencies {
compile 'com.android.support:appcompat- v7:21.+' } -
<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> -
<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> -
<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> -
<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> -
<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> -
Palette also lets you extract Colors for Title and
Body Vibrant Title color generated from Vibrant Body text color generated from Vibrant -
// include the library in your project dependencies { compile
'com.android.support:palette- v7:21.+' } -
// initialization Palette.generate(Bitmap bitmap); Palette.generateAsync(Bitmap bitmap, PaletteAyncListener listener); // fetch
color int vibrant = palette.getVibrantColor(DEFAULT_COLOR); // set color myView.setBackgroundColor(vibrant); -
// fetch Swatch vibrantSwatch = palette.getVibrantSwatch(); // fetch colors from
Swatch int color = vibrantSwatch.getRgb(); int titleColor = vibrantSwatch.getTitleTextColor(); Int bodyColor = vibrantSwatch.getBodyTextColor(); -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
// include the library in your project dependencies { compile
'com.android.support:cardview- v7:21.+' } CardView -
// include the library in your project dependencies { compile
'com.android.support:recyclerview- v7:21.+' } RecyclerView -
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 -
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 -
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 -
class ViewHolder extends RecyclerView.ViewHolder { // override the superclass implementation
public ViewHolder(View v) { super(v); ... } } RecyclerView -
class ViewHolder extends RecyclerView.ViewHolder { // override the superclass implementation
public ViewHolder(View v) { super(v); // do your findViewById's here } } RecyclerView -
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 -
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 -
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 -
class BaseAdapter extends RecyclerView.Adapter<ViewHolder> { @Override public int getItemCount() {
// return your dataset size return data.size(); } ... } RecyclerView -
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 -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
// 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 -
// Communicate between Activity A and B // Activity B
Activity Transitions -
// Communicate between Activity A and B // Activity B
ViewCompat.setTransitionName(commonView, IMG); Activity Transitions -
<!-- include ripple drawables in your drawable-v21 file -- >
<!-- unbounded ripple --> <ripple ... android:color="?attr/colorPrimaryDark"/> Ripple -
<!-- 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 -
// use static method to generate required reveal Animator reveal
= ViewAnimationUtils.createCircularReveal(view, x, y, startRadius, endRadius) Reveal -
// 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 -
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. -
Vector Drawables Define an object geometrically • Points • Lines
• Curves • Color information -
<!-- 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 -
<!-- 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 -
<!-- 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 -
<!-- 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 -
<!-- 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 -
<!-- 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 -
<!-- 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 -
<!-- 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 -
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 -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
Floating Action Button The primary action of your app Notiication
Color Notification.Builder.setColor() -
Floating Action Button The primary action of your app Small
icon badging When your app has a large icon and small icon -
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 -
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. -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
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 -
What’s new in overview? Configurable overview cards Programmatically ActivityManager.TaskDescription td
= new ActivityManager.TaskDescription(caption, bitmap, color); setTaskDescription(td); -
What’s new in overview? Support for multi-tasking // Done with
intent flags Intent.FLAG_ACTIVITY_NEW_DOCUMENT Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources -
Jobscheduler An API for scheduling various types of jobs against
the framework that will be executed in your application's own process. -
Jobscheduler • Configurable • Persists across reboot (Yayyy !!!) •
Easy to use API • Easy to handle failures and retries -
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, new ComponentName(this,MyJobService.class)) .setOverrideDeadline(MAX_TIME) .setMinimumLatency(LATENCY) .setRequiredNetworkType(JobInfo.NETWORK_UNMETE
RED) .setRequiresCharging(true) .setExtras(bundle) .setPersisted(true) .setPeriodic(TIME) .build(); -
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, new ComponentName(this,MyJobService.class)) .setOverrideDeadline(MAX_TIME) .setMinimumLatency(LATENCY) .setRequiredNetworkType(JobInfo.NETWORK_UNMETE
RED) .setRequiresCharging(true) .setExtras(bundle) .setPersisted(true) .setPeriodic(TIME) .build(); -
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, new ComponentName(this,MyJobService.class)) .setOverrideDeadline(MAX_TIME) .setMinimumLatency(LATENCY) .setRequiredNetworkType(JobInfo.NETWORK_UNMETE
RED) .setRequiresCharging(true) .setExtras(bundle) .setPersisted(true) .setPeriodic(TIME) .build(); -
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, new ComponentName(this,MyJobService.class)) .setOverrideDeadline(MAX_TIME) .setMinimumLatency(LATENCY) .setRequiredNetworkType(JobInfo.NETWORK_UNMETE
RED) .setRequiresCharging(true) .setExtras(bundle) .setPersisted(true) .setPeriodic(TIME) .build(); -
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, new ComponentName(this,MyJobService.class)) .setOverrideDeadline(MAX_TIME) .setMinimumLatency(LATENCY) .setRequiredNetworkType(JobInfo.NETWORK_UNMETE
RED) .setRequiresCharging(true) .setExtras(bundle) .setPersisted(true) .setPeriodic(TIME) .build(); -
public class MyJobService extends JobService { @Override public boolean onStartJob(JobParameters
jobParameters) { ... } @Override public boolean onStopJob(JobParameters jobParameters) { ... } } -
JobService Write a JobService • Add to manifest • Requires
android.permission.BIND_JOB_SERVICE • Override onStartJob() • Override onStopJob() -
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. -
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. -
References … Android docs http://goo.gl/Z1AqSn http://goo.gl/dNqUBj http://goo.gl/R18l1d Mark Murphy's talk
on Droidcon London http://goo.gl/dluNts -
1. Intro 2. App bar 3. Color 4. Cards 5.
Animations 6. FAB 7. Notifications 8. Overview 9. Project Volta 10. Resources