Slide 1

Slide 1 text

Introducing the Android Design Support Library Riccardo Ciovati / GDG Milano

Slide 2

Slide 2 text

Agenda ➔ Overview of the Android Design Support Library (~30m) ➔ Live Demo: Building a simple Material-style app with AppCompat and Design Support Library (~45m) ➔ Q&A - Discussion (~15m)

Slide 3

Slide 3 text

What is the DSL? ➔ A set of components which aim to help developers building Material applications ➔ Backcompatible to Android 2.1 (don’t do that, minSdkVersion="16" FTW) ➔ To be used with the AppCompat Library ➔ First version, expect bugs!

Slide 4

Slide 4 text

Getting Started It all begins with the Gradle dependency: dependencies { compile 'com.android.support:design:22.2.0' } build.gradle

Slide 5

Slide 5 text

FloatingActionButton Floating action buttons are used for a promoted action. Guideline page: http://goo.gl/Pw9Twn ➔ Comes in two sizes: ◆ Default: 56dp ◆ Mini: 40dp ➔ Interface Button-like

Slide 6

Slide 6 text

SnackBar Snackbars provide lightweight feedback about an operation by showing a brief message at the bottom of the screen. Snackbars can contain an action. Guideline page: http://goo.gl/QyMXXR Interface Toast-like

Slide 7

Slide 7 text

TextInputLayout Widget for building Material text fields. Guideline page: http://goo.gl/wazbxZ ➔ Provides support for: ◆ Floating labels ◆ Display errors

Slide 8

Slide 8 text

TextInputLayout layout/activity_text_input_layout.xml surnameInputLayout.setError("Can't be empty!"); TextInputLayoutActivity.java

Slide 9

Slide 9 text

NavigationView Widget for implementing the Navigation Drawer pattern. Guideline page: http://goo.gl/sZZz3N ➔ To be used with DrawerLayout ➔ Handles items positioning and styling

Slide 10

Slide 10 text

NavigationView layout/activity_navigation_view.xml

Slide 11

Slide 11 text

NavigationView layout/activity_navigation_view.xml

Slide 12

Slide 12 text

NavigationView menu/navigation_view_items.xml

Slide 13

Slide 13 text

NavigationView public class NavigationViewActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation_view); //Setup NavigationView nv = (NavigationView) findViewById(R.id. navigation_view); nv.setNavigationItemSelectedListener(this); } @Override public boolean onNavigationItemSelected(MenuItem menuItem) { menuItem.setChecked(true); Toast.makeText(this, menuItem.getTitle(),Toast.LENGTH_SHORT).show(); return true; } } menu/navigation_view_items.xml

Slide 14

Slide 14 text

TabLayout Helps you creating a Material tabs: Guideline page: http://goo.gl/Jpsqd4 ➔ Play nicely with ViewPager ➔ Different type of tabs: ◆ Scrollable ◆ Fixed

Slide 15

Slide 15 text

TabLayout layout/activity_tab_layout.xml

Slide 16

Slide 16 text

TabLayout @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_layout); DummyAdapter adapter = new DummyAdapter (getSupportFragmentManager()); ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(adapter); final Resources res = getResources(); int normalColor = res.getColor(R.color.dark_grey); int selectedColor = res.getColor(android.R.color.white); TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout); tabLayout.setTabTextColors(normalColor, selectedColor); tabLayout.setupWithViewPager(viewPager); } layout/activity_tab_layout.xml

Slide 17

Slide 17 text

TabLayout @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_layout); DummyAdapter adapter = new DummyAdapter (getSupportFragmentManager()); ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(adapter); final Resources res = getResources(); int normalColor = res.getColor(R.color.dark_grey); int selectedColor = res.getColor(android.R.color.white); TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout); tabLayout.setTabTextColors(normalColor, selectedColor); tabLayout.setupWithViewPager(viewPager); } layout/activity_tab_layout.xml

Slide 18

Slide 18 text

CoordinatorLayout “CoordinatorLayout is a super-powered FrameLayout.” ➔ Coordinate the interaction between CoordinatorLayout’s children by using/defining Behaviours ➔ Anchor views to other CoordinatorLayout’s children

Slide 19

Slide 19 text

CoordinatorLayout.Behaviour ➔ Define which views a view depends on and how to react when those views change ➔ A few implementation available in the DSL: ◆ AppBarLayout.ScrollingViewBehavior ◆ SwipeDismissBehavior ➔ Build your own for custom views or custom logic

Slide 20

Slide 20 text

Example: FloatingActionButton.Behaviour Goal: Translate the FAB so that it won’t be covered by the SnackBar. ➔ This is actually already implemented but it’s a nice example to understand the mechanism.

Slide 21

Slide 21 text

Example: FloatingActionButton.Behaviour public class ExampleBehaviour extends Behavior { @Override public boolean layoutDependsOn(final CoordinatorLayout parent, final FloatingActionButton child, final View dependency) { return dependency instanceof Snackbar.SnackbarLayout; } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { if (dependency instanceof Snackbar.SnackbarLayout) { float dependencyTranslationY = ViewCompat.getTranslationY(dependency); float translationY = Math.min(0, dependencyTranslationY - dependency.getHeight()); ViewCompat.animate(child).translationY(translationY); return true; } return false; } }

Slide 22

Slide 22 text

Example: FloatingActionButton.Behaviour public class ExampleBehaviour extends Behavior { @Override public boolean layoutDependsOn(final CoordinatorLayout parent, final FloatingActionButton child, final View dependency) { return dependency instanceof Snackbar.SnackbarLayout; } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { if (dependency instanceof Snackbar.SnackbarLayout) { float dependencyTranslationY = ViewCompat.getTranslationY(dependency); float translationY = Math.min(0, dependencyTranslationY - dependency.getHeight()); ViewCompat.animate(child).translationY(translationY); return true; } return false; } }

Slide 23

Slide 23 text

Example: FloatingActionButton.Behaviour How do I use my own defined behaviour? ➔ Annotate your custom view with @DefaultBehavior(ExampleBehavior.class) ➔ In the layout file: app:layout_behavior=”package.ExampleBehavior” More complex example by Ian Lake: https://goo.gl/nT34cD

Slide 24

Slide 24 text

Anchoring layout/activity_fab.xml

Slide 25

Slide 25 text

AppBarLayout Helps you implementing various scrolling techniques, as shown in the guideline: http://goo.gl/JzqUbo Scrolling widgets must implement the NestedScrollingChild interface: ➔ RecyclerView ➔ NestedScrollView

Slide 26

Slide 26 text

AppBarLayout - Basic layout/activity_fab.xml

Slide 27

Slide 27 text

AppBarLayout - Scrolling flags enterAlways: this flag ensures that any downward scroll will cause this view to become visible, enabling the ‘quick return’ pattern

Slide 28

Slide 28 text

AppBarLayout - Scrolling flags enterAlwaysCollapsed: When your view has declared a minHeight and you use this flag, your View will only enter at its minimum height (i.e., ‘collapsed’), only re-expanding to its full height when the scrolling view has reached it’s top.

Slide 29

Slide 29 text

AppBarLayout - Scrolling flags exitUntilCollapsed: this flag causes the view to scroll off until it is ‘collapsed’ (its minHeight) before exiting

Slide 30

Slide 30 text

CollapsingToolbarLayout CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar.

Slide 31

Slide 31 text

Demo Apps ➔ Cheesesquare: https://github. com/chrisbanes/cheesesquare ➔ Slides code: https://github.com/rciovati/design-library- demo

Slide 32

Slide 32 text

References & Links ➔ Android Developers blog: http://goo.gl/g0ZoSA ➔ “Exploring the new Android Design Support Library”: https://goo. gl/R4KsSL ➔ Serie by Styling Android: http://goo.gl/6pZOMA ➔ Serie by Antonio Leiva: http://goo.gl/RCGylx ➔ “Introduction to CoordinatorLayout”: https://goo.gl/TuJJ1z ➔ “androiddesignsupport” StackOverflow tag