
RecyclerView with CheckBox Android
In this tutorial we can used to recyclerView with checkbox widget.in this recyclerView is a every list position to display checkbox.You can checked item in checkbox to position wise declared.
Now this tutorial is simple implements recyclerView and create a custom layout file then declared checkbox and then bind this layout file in the recyclerView adapter and then display list size of adapter layout views.
In this recyclerView you can checked or unchecked item in checkbox.you can multiple item checked or unchecked in this recyclerView.
we can make example of recyclerView with checkbox and how it works in recyclerView with checkbox .
Create activity_main.xml File Following this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.bhaumik.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view_fruits" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> |
Create item_fruits.xml Layout File Follow this Code
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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="8dp" app:cardCornerRadius="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_fruit_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@+id/empName" android:text="TextView" android:layout_weight="1" android:textColor="#000" android:padding="@dimen/_20dp" android:layout_toLeftOf="@+id/checkBox" android:layout_toStartOf="@+id/checkBox" /> <TextView android:id="@+id/tv_fruit_price" android:layout_width="0dp" android:layout_height="wrap_content" android:textColor="#000" android:layout_weight="1" android:layout_toLeftOf="@+id/checkBox" android:layout_toStartOf="@+id/checkBox" android:text="TextView" /> <CheckBox android:id="@+id/checkBox_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="" android:layout_marginRight="@dimen/_10dp"/> </LinearLayout> </android.support.v7.widget.CardView> |
Declared String in res/values/strings.xml file Follow and Copy / Paste String array in String Resource.
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 |
<string-array name="item_fruits"> <item>Apple</item> <item>Banana</item> <item>Mango</item> <item>Orange</item> <item>Cherry</item> <item>Grape</item> <item>Coconut</item> <item>Blueberry</item> <item>Strawberry</item> <item>Pear</item> <item>Watermelon</item> </string-array> <string-array name="item_price"> <item>100</item> <item>40</item> <item>55</item> <item>25</item> <item>80</item> <item>120</item> <item>15</item> <item>30</item> <item>70</item> <item>85</item> <item>75</item> </string-array> |
Create Fruits.java File to Getter and Setter Methods implements.
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 |
package com.example.bhaumik; import java.io.Serializable; public class Fruits implements Serializable { String name; String price; boolean isSelected; public Fruits(String name, String price, boolean isSelected) { this.name = name; this.price = price; this.isSelected = isSelected; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public boolean isSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } } |
Create MainActivity.java File Follow this Code.
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 |
package com.example.bhaumik; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.example.bhaumik.FruitAdapter; import com.example.bhaumik.Fruits; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { FruitAdapter adapter; List<Fruits> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_fruits); recyclerView.setLayoutManager(new LinearLayoutManager(this)); String[] fruits = getResources().getStringArray(R.array.item_fruits); String[] price = getResources().getStringArray(R.array.item_price); for (int i=0;i<fruits.length;i++){ Fruits fruits1 = new Fruits(fruits[i],price[i],false); list.add(fruits1); } adapter = new FruitAdapter(MainActivity.this,list); recyclerView.setAdapter(adapter); } } |
Create FruitAdapter.java File Follow this code.
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 |
package com.example.bhaumik; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; import com.example.bhaumik.R; import com.example.bhaumik.Fruits; import java.util.ArrayList; import java.util.List; public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.FruitHolder>{ Context context; List<Fruits> list = new ArrayList<>(); public FruitAdapter(Context context, List<Fruits> list) { this.context = context; this.list = list; } @Override public FruitHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_fruits,parent,false); return new FruitHolder(view); } @Override public void onBindViewHolder(final FruitHolder holder, final int position) { final Fruits fruits = list.get(position); holder.tv_name.setText(fruits.getName()); holder.tv_price.setText(fruits.getPrice()); holder.checkBox.setChecked(fruits.isSelected()); holder.checkBox.setTag(list.get(position)); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String data = ""; Fruits fruits1 = (Fruits)holder.checkBox.getTag(); fruits1.setSelected(holder.checkBox.isChecked()); list.get(position).setSelected(holder.checkBox.isChecked()); for (int j=0; j<list.size();j++){ if (list.get(j).isSelected() == true){ data = data + "\n" + list.get(j).getName().toString() + " " + list.get(j).getPrice().toString(); } } Toast.makeText(context, "Selected Fruits : \n " + data, Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return list.size(); } public static class FruitHolder extends RecyclerView.ViewHolder{ TextView tv_name,tv_price; CheckBox checkBox; public FruitHolder(View itemView) { super(itemView); tv_name = itemView.findViewById(R.id.tv_fruit_name); tv_price = itemView.findViewById(R.id.tv_fruit_price); checkBox = itemView.findViewById(R.id.checkBox_select); } } public List<Fruits> getFruitsList(){ return list; } } |

In This Example of used to Simple RecyclerView and create custom layout xml file to declared two textView and checkBox. and then in this layout file bind in recyclerView adapter and declared string resource of string array in item_price and item_fruits. and get this string resource in string array of MainActivity.java file and Add List of fruist item. Create Fruits.java class to declared getter setter in name and price of string variable and then one boolean value isSelected.
Now in this list of item to display recyclerView display fruits is list of size.then user can checked in checkbox of list of position to display toast message.you can multiple selected item to display multiple string show in toast message and you can unchecked item to display remove the string of show message.