Friday, 30 January 2015

Spinner in Toolbar Example in Android

Step: 1
======
SpinToolbarActivity.java

package com.pratap.cardviews1;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinToolbarActivity extends AppCompatActivity {

 private Toolbar toolbar;

 private Spinner spinner_nav;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.spintoolbaractivity);
  toolbar = (Toolbar) findViewById(R.id.toolbar);
  spinner_nav = (Spinner) findViewById(R.id.spinner_nav);

  if (toolbar != null) {
   setSupportActionBar(toolbar);
   getSupportActionBar().setDisplayShowTitleEnabled(false);

  }
  addItemsToSpinner();

 }

 // add items into spinner dynamically
 public void addItemsToSpinner() {

  ArrayList<String> list = new ArrayList<String>();
  list.add("Top News");
  list.add("Politics");
  list.add("Business");
  list.add("Sports");
  list.add("Movies");

  // Custom ArrayAdapter with spinner item layout to set popup background

  CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(
    getApplicationContext(), list);

  
  
  // Default ArrayAdapter with default spinner item layout, getting some
  // view rendering problem in lollypop device, need to test in other
  // devices

  /*
   * ArrayAdapter<String> spinAdapter = new ArrayAdapter<String>(this,
   * android.R.layout.simple_spinner_item, list);
   * spinAdapter.setDropDownViewResource
   * (android.R.layout.simple_spinner_dropdown_item);
   */

  spinner_nav.setAdapter(spinAdapter);

  spinner_nav.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> adapter, View v,
     int position, long id) {
    // On selecting a spinner item
    String item = adapter.getItemAtPosition(position).toString();

    // Showing selected spinner item
    Toast.makeText(getApplicationContext(), "Selected  : " + item,
      Toast.LENGTH_LONG).show();
   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

   }
  });

 }

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

 @Override
 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 == R.id.action_settings) {
   Toast.makeText(getApplicationContext(), "Settings Clicked",
     Toast.LENGTH_SHORT).show();
   return true;
  } else if (id == R.id.action_search) {
   Toast.makeText(getApplicationContext(), "Search Clicked",
     Toast.LENGTH_SHORT).show();
   return true;
  } else if (id == R.id.action_add) {
   Toast.makeText(getApplicationContext(), "Add Clicked",
     Toast.LENGTH_SHORT).show();
   return true;
  } else if (id == R.id.action_delete) {
   Toast.makeText(getApplicationContext(), "Delete Clicked",
     Toast.LENGTH_SHORT).show();
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}


toolbar.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
     >

    <Spinner
        android:id="@+id/spinner_nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v7.widget.Toolbar>

spintoolbaractivity.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c9c9c9"
    android:orientation="vertical" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/spintoolbar" />


</LinearLayout>


Step: 2
======
CustomSpinnerAdapter.java


package com.pratap.cardviews1;

import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

// Custom Adapter for Spinner
public class CustomSpinnerAdapter extends ArrayAdapter<String> {

 private Context context1;
 private ArrayList<String> data;
 public Resources res;
 LayoutInflater inflater;

 public CustomSpinnerAdapter(Context context, ArrayList<String> objects) {
  super(context, R.layout.spinner_row, objects);

  context1 = context;
  data = objects;

  inflater = (LayoutInflater) context1
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 }

 @Override
 public View getDropDownView(int position, View convertView, ViewGroup parent) {
  return getCustomView(position, convertView, parent);
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  return getCustomView(position, convertView, parent);
 }

 // This funtion called for each row ( Called data.size() times )
 public View getCustomView(int position, View convertView, ViewGroup parent) {

  View row = inflater.inflate(R.layout.spinner_row, parent, false);

  TextView tvCategory = (TextView) row.findViewById(R.id.tvCategory);

  tvCategory.setText(data.get(position).toString());

  return row;
 }
}



spinner_row.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
   android:background="@drawable/spinner_selector"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvCategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textSize="18sp"
        />

</RelativeLayout>

values/styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="AppTheme.Base" />

    <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
        <!-- your app branding color for the app bar -->
        <item name="colorPrimary">@color/md_teal_500_primary</item>

        <!-- darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">@color/md_teal_700</item>

        <!-- theme UI controls like checkboxes and text fields -->
        <item name="colorAccent">@color/md_teal_900</item>
     
        
    </style>

</resources>



Screenshot:
======





















Update:
======

Since the spinner dropdown is not like the new LollyPop Design like Spinner:

We need to add small code for the spinner control

 android:dropDownVerticalOffset="-52dp" for Kitkat and below

 android:dropDownVerticalOffset="0dp" for LollyPop



 <Spinner
        android:id="@+id/spinner_nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:dropDownVerticalOffset="@dimen/dropDownVerticalOffset" />


Tuesday, 13 January 2015

Endless RecyclerView OnScrollListener Example in android


Note:

I have updated this post.Please see the updated post



If you have thousands of records in your database server, rather than getting all records loading at a time, try to load some x number of records in the onscroll event and update the ui,

Example:
If you have 1000 records in the server db, get 50 records first time, if the user reached to the last record in the ui, then again load 50 more records in the onscrolllistener event.

Step: 1
======
In this example, i am using following github code snippet for endless RecyclerView

Credit goes to: WoongBi Kim
https://gist.github.com/ssinss/e06f12ef66c51252563e

Step: 2
======

Now, Create an Activity with RecyclerView in the XML Layout file.

CardViewActivity.java


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
package com.pratap.cardviews1;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

public class CardViewActivity extends AppCompatActivity {

 private Toolbar toolbar;

 private RecyclerView mRecyclerView;
 private RecyclerView.Adapter mAdapter;
 private LinearLayoutManager mLayoutManager;

 private List<Student> studentList;

 // on scroll

 private static int current_page = 1;

 private int ival = 1;
 private int loadLimit = 10;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  toolbar = (Toolbar) findViewById(R.id.toolbar);

  studentList = new ArrayList<Student>();

  loadData(current_page);

  if (toolbar != null) {
   setSupportActionBar(toolbar);
   getSupportActionBar().setTitle("Android Students");

  }

  mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

  // use this setting to improve performance if you know that changes
  // in content do not change the layout size of the RecyclerView
  mRecyclerView.setHasFixedSize(true);

  mLayoutManager = new LinearLayoutManager(this);

  // use a linear layout manager
  mRecyclerView.setLayoutManager(mLayoutManager);

  // create an Object for Adapter
  mAdapter = new CardViewDataAdapter(studentList);

  // set the adapter object to the Recyclerview
  mRecyclerView.setAdapter(mAdapter);

  mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(
    mLayoutManager) {
   @Override
   public void onLoadMore(int current_page) {
    // do somthing...

    loadMoreData(current_page);

   }

  });

 }
// By default, we add 10 objects for first time.
 private void loadData(int current_page) {

  // I have not used current page for showing demo, if u use a webservice
  // then it is useful for every call request

  for (int i = ival; i <= loadLimit; i++) {
   Student st = new Student("Student " + i, "androidstudent" + i
     + "@gmail.com", false);

   studentList.add(st);
   ival++;

  }

 }
 // adding 10 object creating dymically to arraylist and updating recyclerview when ever we reached last item
 private void loadMoreData(int current_page) {

  // I have not used current page for showing demo, if u use a webservice
  // then it is useful for every call request

  loadLimit = ival + 10;

  for (int i = ival; i <= loadLimit; i++) {
   Student st = new Student("Student " + i, "androidstudent" + i
     + "@gmail.com", false);

   studentList.add(st);
   ival++;
  }

  mAdapter.notifyDataSetChanged();
  
 }

}

activity_main.xml


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:scrollbars="vertical" />

</LinearLayout>



Step: 3
======
EndlessRecyclerOnScrollListener.java


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.pratap.cardviews1;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public abstract class EndlessRecyclerOnScrollListener extends
  RecyclerView.OnScrollListener {
 public static String TAG = EndlessRecyclerOnScrollListener.class
   .getSimpleName();

 private int previousTotal = 0;
 private boolean loading = true;
 private int visibleThreshold = 5;
 int firstVisibleItem, visibleItemCount, totalItemCount;

 private int current_page = 1;

 private LinearLayoutManager mLinearLayoutManager;

 public EndlessRecyclerOnScrollListener(
   LinearLayoutManager linearLayoutManager) {
  this.mLinearLayoutManager = linearLayoutManager;
 }

 @Override
 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  super.onScrolled(recyclerView, dx, dy);

  visibleItemCount = recyclerView.getChildCount();
  totalItemCount = mLinearLayoutManager.getItemCount();
  firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

  if (loading) {
   if (totalItemCount > previousTotal) {
    loading = false;
    previousTotal = totalItemCount;
   }
  }
  if (!loading
    && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
   // End has been reached

   // Do something
   current_page++;

   onLoadMore(current_page);

   loading = true;
  }
 }

 public abstract void onLoadMore(int current_page);
}

Step: 4
======
Now create an adapter for the RecyclerView

CardViewDataAdapter.java


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.pratap.cardviews1;

import java.util.List;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

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

 private List<Student> stList;

 public CardViewDataAdapter(List<Student> students) {
  this.stList = students;

 }

 // Create new views
 @Override
 public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
   int viewType) {
  // create a new view
  View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.cardview_row, null);

  // create ViewHolder

  ViewHolder viewHolder = new ViewHolder(itemLayoutView);
  return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {

  viewHolder.tvName.setText(stList.get(position).getName());

  viewHolder.tvEmailId.setText(stList.get(position).getEmailId());
  
  viewHolder.singlestudent=stList.get(position);

 }

 // Return the size arraylist
 @Override
 public int getItemCount() {
  return stList.size();
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {

  public TextView tvName;
  public TextView tvEmailId;

  public Student singlestudent;

  public ViewHolder(View itemLayoutView) {
   super(itemLayoutView);

   tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);

   tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
   // Onclick event for the row to show the data in toast
   itemLayoutView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

     Toast.makeText(
       v.getContext(),
       "Data : \n" + singlestudent.getName() + " \n"
         + singlestudent.getEmailId(),
       Toast.LENGTH_SHORT).show();

    }
   });

  }

 }

}

cardview_row.xml


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    card_view:cardCornerRadius="5dp"
    card_view:cardUseCompatPadding="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="name"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvEmailId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tvName"
            android:text="email"
            android:textColor="@android:color/black"
            android:textSize="18sp" />
    </RelativeLayout>

</android.support.v7.widget.CardView>


Student.java 

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.pratap.cardviews1;

import java.io.Serializable;

public class Student implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 private String name;

 private String emailId;

 private boolean isSelected;

 public Student() {

 }

 public Student(String name, String emailId) {

  this.name = name;
  this.emailId = emailId;

 }

 public Student(String name, String emailId, boolean isSelected) {

  this.name = name;
  this.emailId = emailId;
  this.isSelected = isSelected;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getEmailId() {
  return emailId;
 }

 public void setEmailId(String emailId) {
  this.emailId = emailId;
 }

 public boolean isSelected() {
  return isSelected;
 }

 public void setSelected(boolean isSelected) {
  this.isSelected = isSelected;
 }

}

ScreenShots:
=========


  


Source Code 
=========

Download Link

Friday, 9 January 2015

RecyclerView with CheckBox Example

 Example to show Selected items with checkbox in the RecyclerView.

Step: 1
======
Create a Model class with name, emailId, isSelected variables

Student.java

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.pratap.cardviews1;

import java.io.Serializable;

public class Student implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 private String name;

 private String emailId;

 private boolean isSelected;

 public Student() {

 }

 public Student(String name, String emailId) {

  this.name = name;
  this.emailId = emailId;

 }

 public Student(String name, String emailId, boolean isSelected) {

  this.name = name;
  this.emailId = emailId;
  this.isSelected = isSelected;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getEmailId() {
  return emailId;
 }

 public void setEmailId(String emailId) {
  this.emailId = emailId;
 }

 public boolean isSelected() {
  return isSelected;
 }

 public void setSelected(boolean isSelected) {
  this.isSelected = isSelected;
 }

}


Step: 2
======
Now Create an Activity with Recycler with button in the xml layout file.

CardViewActivity.java


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.pratap.cardviews1;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CardViewActivity extends AppCompatActivity {

 private Toolbar toolbar;

 private RecyclerView mRecyclerView;
 private RecyclerView.Adapter mAdapter;
 private RecyclerView.LayoutManager mLayoutManager;

 private List<Student> studentList;

 private Button btnSelection;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  toolbar = (Toolbar) findViewById(R.id.toolbar);
  btnSelection = (Button) findViewById(R.id.btnShow);

  studentList = new ArrayList<Student>();

  for (int i = 1; i <= 15; i++) {
   Student st = new Student("Student " + i, "androidstudent" + i
     + "@gmail.com", false);

   studentList.add(st);
  }

  if (toolbar != null) {
   setSupportActionBar(toolbar);
   getSupportActionBar().setTitle("Android Students");

  }

  mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

  // use this setting to improve performance if you know that changes
  // in content do not change the layout size of the RecyclerView
  mRecyclerView.setHasFixedSize(true);

  // use a linear layout manager
  mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

  // create an Object for Adapter
  mAdapter = new CardViewDataAdapter(studentList);

  // set the adapter object to the Recyclerview
  mRecyclerView.setAdapter(mAdapter);

  btnSelection.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    String data = "";
    List<Student> stList = ((CardViewDataAdapter) mAdapter)
      .getStudentist();

    for (int i = 0; i < stList.size(); i++) {
     Student singleStudent = stList.get(i);
     if (singleStudent.isSelected() == true) {

      data = data + "\n" + singleStudent.getName().toString();
      /*
       * Toast.makeText( CardViewActivity.this, " " +
       * singleStudent.getName() + " " +
       * singleStudent.getEmailId() + " " +
       * singleStudent.isSelected(),
       * Toast.LENGTH_SHORT).show();
       */
     }

    }

    Toast.makeText(CardViewActivity.this,
      "Selected Students: \n" + data, Toast.LENGTH_LONG)
      .show();
   }
  });

 }

}


activity_main.xml


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:scrollbars="vertical" />

    <Button
        android:id="@+id/btnShow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="Show Selected"
        android:background="#00796B" 
        android:textColor="@color/TextPrimaryColor"/>

</LinearLayout>



Step: 3
======

Create an Adapter for the RecyclerView and Pass the list of object to the adapter.

CardViewDataAdapter.java




1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
package com.pratap.cardviews1;

import java.util.List;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

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

 private List<Student> stList;

 public CardViewDataAdapter(List<Student> students) {
  this.stList = students;

 }

 // Create new views
 @Override
 public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
   int viewType) {
  // create a new view
  View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.cardview_row, null);

  // create ViewHolder

  ViewHolder viewHolder = new ViewHolder(itemLayoutView);

  return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {

  final int pos = position;

  viewHolder.tvName.setText(stList.get(position).getName());

  viewHolder.tvEmailId.setText(stList.get(position).getEmailId());

  viewHolder.chkSelected.setChecked(stList.get(position).isSelected());

  viewHolder.chkSelected.setTag(stList.get(position));

  
  viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    CheckBox cb = (CheckBox) v;
    Student contact = (Student) cb.getTag();

    contact.setSelected(cb.isChecked());
    stList.get(pos).setSelected(cb.isChecked());

    Toast.makeText(
      v.getContext(),
      "Clicked on Checkbox: " + cb.getText() + " is "
        + cb.isChecked(), Toast.LENGTH_LONG).show();
   }
  });

 }

 // Return the size arraylist
 @Override
 public int getItemCount() {
  return stList.size();
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {

  public TextView tvName;
  public TextView tvEmailId;

  public CheckBox chkSelected;

  public Student singlestudent;

  public ViewHolder(View itemLayoutView) {
   super(itemLayoutView);

   tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);

   tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
   chkSelected = (CheckBox) itemLayoutView
     .findViewById(R.id.chkSelected);

  }

 }

 // method to access in activity after updating selection
 public List<Student> getStudentist() {
  return stList;
 }

}



cardview_row.xml


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    card_view:cardCornerRadius="5dp"
    
    card_view:cardUseCompatPadding="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="name"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvEmailId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tvName"
            android:text="email"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/chkSelected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            />
    </RelativeLayout>

</android.support.v7.widget.CardView>


ScreenShots
=========



Sample Demo
===========


















Source Code of the Project
=========
Download Link


Lottie Android Example

Render After Effects animations natively on Android using this Great Library https://github.com/airbnb/lottie-android Let's see ho...