Thursday, July 10, 2014

Android L Developer Preview

As you all know last month at Google I/O we got glimpse of the L Developer Preview. It provides us an advance look at the upcoming release for the Android platform, which offers new features for users and app developers. Android is getting a major overhaul with its next version, which is currently being referred to as Android L. The new version will tout more than 5,000 new APIs, multiple new features, a new runtime engine, and a major user interface makeover. The Android SDK has been released and, for the first time, you will be able to preview the new version on a Nexus 5 or 7 device.

I thought let's give a try to L Developer Preview, so I downloaded the SDK & setup my development environment. After that I installed the L Preview System Image on Nexus 5. To flash the image onto your device please follow these instructions. You can have a look at Nexus 5 with L Developer Preview as follows:-

Now it's time to get our hands on L Developer Preview APIs & I’m creating a github repository, where you can find this example. This sample app is focused on the new API's for preview version of Android-L, such as Material Design UI, RecyclerView, CardView,; Floating Button, & Ripples.

The Android L Developer Preview includes support for material design apps. Material design is a comprehensive guide for visual, motion, and interaction design across platforms and devices. To use the Material theme on your app, you only have to set the parent of your app style to :-
  • @android:style/Theme.Material
  • @android:style/Theme.Material.Light 
  • @android:style/Theme.Material.DarkActionBar. 
You can also customize the color palette, and default animations for touch feedback and activity transitions. Animations are an important element of Material Design. A new set of APIs also let you create custom animations. The new animation APIs let you :-
  • Respond to touch events with touch feedback animations
  • Hide and show views with reveal effect animations
  • Switch between activities with custom activity transition animations
  • Create more natural animations with curved motion
  • Animate changes in one or more view properties with view state change animations
  • Show animations in state list drawables between view state changes
The Android L Developer Preview includes two new widgets for displaying complex lists and cards in your app and can be found in the latest support-v7 version. These widgets have material design style by default.
  1. RecyclerView
  2. CardView
RecyclerView - This new widget is more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. As we already know that in the ListView to increase the performance we have to use the ViewHolder pattern. This is simply a java class that holds the references to the widget in the row layout of the ListView (for example ImageView, & TextView which I used in sample app). Using this pattern we avoid to call several times findViewById() method to get the UI widget reference making the ListView scrolling smoother. Even if this pattern was suggested as best-practice we could implement our Adapter without using this pattern. 

RecyclerView enforces this pattern making it the core of this UI widget and we have to use it in our Adapter. RecyclerView introduces a new Adapter that must be used to represent the underlying data in the widget. This new adapter is called RecyclerView.Adapter. This component stands behind the UI widget and determines how the rows, in the ListView, have to be rendered and what information to show. It’s a similar approach to the ones you already used, but with some peculiarities, such as a required ViewHolder. You will have to override two main methods: 
  1. onCreateViewHolder(ViewGroup parent, int viewType)
  2. onBindViewHolder(ViewHolder holder, int position)
First method to inflate the view and its view holder, and second method to bind data to the view. The good thing about this is that first method is called only when we really need to create a new view. No need to check if it’s being recycled.

CardView - CardView widget lets you display important pieces of information inside cards that have a consistent look and feel. CardView extends the FrameLayout class. You can specify properties of CardView as follows:
  • To set the corner radius in your layouts, use the android:cardCornerRadius attribute.
  • To set the corner radius in your code, use the CardView.setRadius method.
  • To set the background color of a card, use the android:cardBackgroundColor attribute.
Floating Button - Android-L introduces a new way to manage view with elevation and shadows. You can also define the ripple area for touch feedback.

Ripples - This is touch feedback & one of the important elements in Material Design, to give immediate feedback to users what they touch? where they touch? by animate the state color change.

In addition to Ripples, Android L also has Shadows to give 3d effect using elevation property. The elevation of a view determines the size of its shadow: views with higher Z values cast bigger shadows. I tried to use some APIs in my sample app & the output is as follows:-

Link to Github Repository