Friday, 12 December 2014

RecyclerView Tutorial

Android has introduced a new widget for displaying data collections which is a more advanced and flexible version of ListView. RecyclerView incorporates ListView and ViewHolder reusability concept very efficiently.

RecyclerView is very useful with large data collections whose content changes at runtime.

RecyclerView includes:

  1. Layout managers for positioning items. 
  2. Default animatios for common item operations , such as removal or addition of items.

Custom animation and layout managers can be defined as per the requirement.

Layout Manager:

RecyclerView provides 3 built-in layout managers:

  1. LinearLayoutManager shows items in a vertical or horizontal scrolling list. 
  2. GridLayoutManage shows items in a grid. 
  3. StaggeredGridLayoutManager shows items in a staggered grid. 

For custom layout manager, extend the RecyclerView.LayoutManager class.


  1. Animations for adding and removing items are enabled by default.
  2. For custom animation, extend the RecyclerView.ItemAnimator class and use the RecyclerView.setItemAnimator() method.

RecyclerView Demo:

  1. Create a new Android project.
  2. Add the v7 support library, for eclipse follow the link and for gradle add: 
     3.  Add RecyclerView in xml:

     android:scrollbars="vertical" />

   4. Create Adapter file
      by extending RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{

 private String[] mData;

 public MyRecyclerViewAdapter(String[] mData) {
  this.mData = mData;

 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  View v = LayoutInflater.from(viewGroup.getContext())
    .inflate(R.layout.list_item_layout, viewGroup, false);

  ViewHolder vh = new ViewHolder(v);
  return vh;


 public void onBindViewHolder(ViewHolder viewHolder, int i) {

 public int getItemCount() {
  if (mData != null)
   return mData.length;
  return 0;

 public static class ViewHolder extends RecyclerView.ViewHolder {
  // each data item is just a string in this case
  public TextView mTextView;

  public ViewHolder(View itemView) {
   mTextView = (TextView)itemView.findViewById(;;

   5. Create Activity

public class MyActivity extends Activity implements View.OnClickListener{

    private RecyclerView mRecyclerView;
    private MyRecyclerViewAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private GridLayoutManager mGridLayoutManager;
    private StaggeredGridLayoutManager mStaggeredGridLayoutManager;

    private String[] mSampleData = {"What is Android Lollipop?", "What is Android 5.0?",                     "What is RecyclerView?", "What is difference between RecyclerView and ListView?", "How does this demo app help?"};
    protected void onCreate(Bundle savedInstanceState) {

        mRecyclerView = (RecyclerView) findViewById(;

        mAdapter = new MyRecyclerViewAdapter(mSampleData);

    private void applyLinearLayoutManager(){
        mLayoutManager = new LinearLayoutManager(this);

    private void applyGridLayoutManager(){
        mGridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);

    private void applyStaggeredGridLayoutManager(){
        mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    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();
        if (id ==
        else if (id ==
        else if (id ==
        return super.onOptionsItemSelected(item);

    public void onClick(View v) {
        int pos = mRecyclerView.getChildPosition(v);
        Toast.makeText(getApplicationContext(), mSampleData[pos], Toast.LENGTH_LONG).show();

Get ready for Jack and Jill !!!

Android is working over new toolchain and it has introduced Jack and Jill available for testing with non-production versions of the apps, at the core of the new toolchain.

JACK - Java Android Compiler Kit

Jack is a new Android toolchain that comprises a compiler from Java programming language source to the Android dex file format. Jack has its own .jack library format and provides most tool chain features as part of a single tool: repackaging, shrinking, obfuscation and multidex.

JILL - Jack Intermediate Library Linker

Jill is a tool that translates existing .jar files to the .jack library format.


With the new tool chain enabled, Jill will translate any libraries you are referencing to a new Jack library file (.jack). This prepares them to be quickly merged with other .jack files. The Android Gradle plugin and Jack collect any .jack library files, along with your source code, and compiles them into a set of dex files. During the process, Jack also handles any requested code minification. The output is then assembled into an APK file as normal. Support for multiple dex files is also included.

Jack using Gradle

Jack and Jill are available in Build Tools version 21.1.1, and greater, via the SDK Manager. Complementary Gradle and Android Studio support is also already available in the Android 1.0.0+ Gradle plugin.

Using Gradle, add useJack in your build config.

android {
    buildToolsRevision '21.1.1'
    defaultConfig {
      // Enable the experimental Jack build tools.
      useJack = true

Official Blog:

Tool Doc: