Recyclerview with checkbox android

Содержание
  1. Recyclerview with checkbox android
  2. Android RecyclerView Checkbox
  3. Adding RecyclerView Library
  4. RecyclerView with CheckBox Example Output
  5. RecyclerView with CheckBox Example Code
  6. RecyclerView Item Layout with CheckBox
  7. Activity Layout
  8. RecyclerView Adapter
  9. Activity
  10. About
  11. RecyclerView Checkbox Android Example Get Checked Item Position
  12. Step 1: Create a new project in Android Studio.
  13. Step 2: Updating build.gradle(Module:app) file
  14. Step 3: Coding cardview.xml
  15. Step 4: Making rv_item.xml
  16. Step 5: Making Model class
  17. Step 6: Adding integer.xml
  18. Step 7: Code for CustomAdapter.java
  19. Describing CustomAdapter
  20. Step 8: Making NextActivity
  21. Step 9: Finally Update activity_main.xml and MainActivity.java
  22. Updating activity_main.xml
  23. Updating MainActivity.java
  24. Explaining Above
  25. Select All
  26. Deselect All
  27. Android Recyclerview for Installed Apps with Checkbox
  28. Layout
  29. Build Grade
  30. Viewing Installed Apps
  31. View Adapter
  32. Main Activity
  33. Android RecyclerView with RadioButton or CheckBox – Examples
  34. Concepts you will Learn
  35. Demos
  36. Example 1: Android RecyclerView CheckBoxes – Maintain Checked States
  37. We are Building a Vibrant YouTube Community
  38. Step 1: Dependencies
  39. Step 2: Create Main Activity Layout
  40. activity_main.xml
  41. Step 3: Create a RecyclerView Item
  42. model.xml
  43. Step 4: MainActivity.java
  44. Specify Package and add Imports
  45. Full Code
  46. Example 2: SingleChoice RecyclerView – with RadioButtons
  47. Step 1: Gradle Scripts
  48. Step 2: Create Layouts
  49. 1. activity_main.xml
  50. 2. activity_detail.xml
  51. 3. model.xml
  52. Step 4: Write Java Code.
  53. Example 3: Kotlin Android RecyclerView – CardView with CheckBox,Images,Text and ItemClick
  54. Video Tutorial
  55. What You Learn
  56. Tools Used
  57. (a). Build.gradle
  58. (b). activity_main.xml
  59. (c). model.xml
  60. (d). MainActivity.kt
  61. Example 4: Android MySQL – RecyclerView CheckBoxes – INSERT,SELECT,SHOW
  62. Video Tutorial(ProgrammingWizards TV Channel)
  63. Android MySQL RecyclerView CheckBoxes Insert, Select Show Example
  64. MySQL Table
  65. Project Structure
  66. 1. PHP Code
  67. 2. Gradle Scripts
  68. 3. Resoources
  69. 3. Java Code.
  70. AndroidManifest.xml
  71. Download
  72. Oclemy

Recyclerview with checkbox android

Android RecyclerView Checkbox

September 12, 2017

If there is a functional requirement in your application that needs list of items to be displayed in UI and allow user to choose multiple items from it, then read this post which explains how to implement the feature using RecyclerView and CheckBox.

I’ll take product filter screen of an ecommerce application as an example to show how to implement RecyclerView with CheckBox. The example screen will display list of brands in recycler view with checkbox for each option so that user can select multiple brands from the list.

Adding RecyclerView Library

To add RecyclerView library to your project, first add below entry to module build.gradle file.

RecyclerView with CheckBox Example Output

Below screen shot shows the output of our e-commerce brand filter example.

RecyclerView with CheckBox Example Code

Following sections provide explanation and code of layouts and classes created for recycler view with checkbox example. When you run below code, screen containing list of brands with options to select items will be shown. On clicking item or on clicking checkbox, it will display message.

RecyclerView Item Layout with CheckBox

First we need to define layout which will be used to display items in RecyclerView. Below layout is defined for our example. It is a constraint layout with two text views to display brand name and product count and check box.

Activity Layout

For our example, activity layout or screen layout contains TextView for title and RecyclerView.

RecyclerView Adapter

RecyclerView adapter binds data to views in item layout. For each item, item click event listener and checkbox state change listener is added.

Activity

Activity gets data, adds recycler view adapter, recycler view item decorator and recycler view layout manager to recycler view.

About

Android app development tutorials and web app development tutorials with programming examples and code samples.

Источник

RecyclerView Checkbox Android Example Get Checked Item Position

Warm welcome to RecyclerView Checkbox Android Studio example.

In this RecyclerView Checkbox Android example, we will cover single selection, multiple selection and also select all scenario.

There is a common problem like “checkbox unchecked when I scroll recyclerview.” We will solve this problem in this RecyclerView Checkbox Android tutorial.

We will also send selected or checked items to next activity.

You can learn how to use onItemClickListener, onClickListener, onItemTouchListener etc. for recyclerview.

Now checkout the output of RecyclerView Checkbox Android example, then we will develop it.

Step 1: Create a new project in Android Studio.

Prepare a new project in android studio with empty activity as a default activity of your project.

Step 2: Updating build.gradle(Module:app) file

Add below source code into dependencies<>

Step 3: Coding cardview.xml

Create a new file into drawable folder and name it cardview.xml

Add below source code into it.

  • This file is used to create gradient effect with two different colors.
  • With help of this file, we will add some attractive design to our cardviews.
  • It will add this effect at the upper side of every card. You show this design in the above output video as well.

Step 4: Making rv_item.xml

Create a new layout resource file named rv_item.xml

Copy following source code into it

  • Above file is the common layout for each row of the recyclerview.
  • Adapter will create a view for every row item using this file.
  • I have taken one textview first. Here I have used it’s background design as a file named cardview.xml that we created in the previous step.
  • Now after that, I have taken one checkbox and one textview side by side (with horizontal orientation in LinearLayout)
  • textview represents the name of the animal.

Step 5: Making Model class

Create a new JAVA class named Model.java and paste below

Step 6: Adding integer.xml

Create a new file named integer.xml in values directory and add below

Step 7: Code for CustomAdapter.java

Let’s create Custom Adapter for RecyclerView.

Make a new JAVA class named CustomAdapter.java and add below

Describing CustomAdapter

  • Above code will set a tag for position.

Now look at below source code

  • We will get position (which we set above using setTag() method) in onClickmethod.

Step 8: Making NextActivity

Create a new activity named “NextActivity

Add below source code into NextActivity.java

Copy following code into activity_next.xml

Step 9: Finally Update activity_main.xml and MainActivity.java

Updating activity_main.xml

Add following into activity_main.xml

Updating MainActivity.java

Copy folowing into MainActivity.java

Explaining Above

Look at the below line

  • Compiler will create a string array named animallist. It includes the names of the animals.

Consider the below code snippet

  • This line is preparing a arraylist to generate data.
  • Compiler is using getModel() method to have useful data.

Following is the code structure for getModel() method.

  • Compiler will run a for loop with four iterations.
  • In every iteration, compiler will create one object of the Model class.
  • Then it will set the name of the animal using string array (animallist). It will set the value of checkbox using isSelect boolean variable which is coming from the parameter.

Select All

We have taken a select all button in the activity_main.xml file.

Compiler will run the following code when the user clicks this button.

  • On the click of the button, compiler will generate new model arraylist with the help of getModel() method.
  • Here a parameter of getModel() method is set as a true. So data set will check all the checkboxes of the recyclerview rows.
Читайте также:  Developing application with android

Deselect All

  • I have taken a deselect all button in the activity_main.xml file.
  • System will execute the below code when user clicks this button.
  • Here, system will run the getModel() method with the parameter value as a false.
  • So all the checkboxes are deselected.

This was all for RecyclerView Checkbox Android tutorial. Thank you.

Источник

Android Recyclerview for Installed Apps with Checkbox

One of the most common reasons that you would want to use the android recyclerview is if you want to show a list of some sort. In this example, let’s take a look at how you can use the android recyclerview to show a list of installed apps on your device. Within each item, we will also include a checkbox. Here’s what it will look like:

Layout

Let’s first take care of creating the layout files. Here is my layout file for the main activity (activity_main.xml). I am using a FrameLayout, with the **Recyclerview **and **F****loating Action Button.** The floating action button is not necessary for this tutorial.

Next, let’s take care of the layout for each individual item in the list. I am calling this main_line_view.xml. It contains the following contents:

Build Grade

In your build Gradle (of the module: App), make sure that you include the following:

Update the version to match the SDK that you are targeting.

Viewing Installed Apps

Now let’s work on getting the list of apps installed on your device. The only important line of code that you will need is the following:

However, I also want to grab app name and icon. To keep things easy and re-usable, I created a class called AppManager and added the following contents:

The first function called loadApps gets the list of installed applications on the device. You will notice that I created my own custom class called AppInfo. This is because I wanted to store particular values and info from each class. Here is what the class AppInfo looks like:

So loadApps essentially saves a list of type AppInfo. I also implement a custom sort method that will sort the apps by their app name. You can then grab the list using the getApps method.

View Adapter

Now it’s time to implement a view adapter. If you worked with listviews in the pass, then this will look very familiar. Essentially, a view adapter controls how the recyclerview will show the view. It also maps all of our items from the main_line_view.xml to functions in the adapter. So here’s what the adapter looks like:

The only thing I want to point out if how I am handling the checkbox click. You will see the I implemented a listener that wraps the entire item. When it’s clicked, I set the selected boolean to the opposite of what it was. Finally, I used the line notifyDataSetChanged so that the recyclerview will refresh the list.

Main Activity

Finally, we can finish it up in our main activity by using the following lines:

That’s it! You can now have an android app that will display a list of installed apps using the recyclerview with a checkbox next to each item.

About Bennett Notes

A collection of thoughts, notes and projects from Dave Bennett.

Источник

Android RecyclerView with RadioButton or CheckBox – Examples

In this tutorial we will look at creating and using a custom recyclerview with checkboxes or radiobuttons and how to handle their states.

Concepts you will Learn

This tutorial, via examples, aims to help you learn the following:

  1. How to create recyclerview with checkboxes or radiobuttons and appropriately handle their states.
  2. How to render data from mysql database, including boolean data, in a recyclerview with checkboxes.
  3. How to search/filter recyclerview with checkboxes while maintainning their checked/unchecked states.

Demos

Here are the some of the demos that will be created:

CheckBox Demo

RadioButton Demo

Example 1: Android RecyclerView CheckBoxes – Maintain Checked States

This is an android recyclerview checkboxes tutorial. We want to see how to first render checkboxes, images and text in a custom recyclerview with cardviews. Then get the selected or checked cardviews and show n a Toast message. We work with multiple checkboxes so we can multi-select or single-select items in our RecyclerView.

This will teach us how to properly retain checked states of our checkboxes in our recyclerview despite the recycling of our cardviews with checkboxes.

Then the user can click a floating action button to get the selected items and show in a Toast message.

We are Building a Vibrant YouTube Community

Here’s this tutorial in Video Format.

Step 1: Dependencies

We do not need any special dependency for this project. All we use are the standard androidx dependencies like recyclerview and cardview.

Step 2: Create Main Activity Layout

activity_main.xml

This will be our main activity layout and we write in XML. XML is a markup language that is not only used to create user interfaces but also exchnage data.In this case we use to create our android user interface.

This layout will be inflated into our main activity. At the root we will have a RelativeLayout.

We have a TextView which is our headerTextView to display the heading our android application.

Then we have a RecyclerView which in this case is our adapterview. This RecyclerView will render images, text and checkboxes.

Below the RecyclerView we will have a FloatingActionButton. When users click this FAB button, we will show the items that have been selected in the recyclerview. Remember our recyclerview will be comprising cardviews that have images, text and checkboxes.

Step 3: Create a RecyclerView Item

model.xml

This layout will model a single cardview to be rendered among other cards in our recyclerview.

This layout will model all these cardviews. At the root we have the CardView as our element. CardView resides in the android.support.v7.widget namespace.

I have set the Card elevation to 5dp and card cornder radius to 10dp .

Inside it I have a RelativeLayout to organize my widgets relative to each other.

First we have the ImageView to render our Image. Then TextViews to render our texts.

To the far right of our CardView we will have a CheckBox that will hold the selection property of each cardview. The CheckBoxes can be checked or unchecked.

Step 4: MainActivity.java

Specify Package and add Imports

Our java class will reside in a package, so we specify one and add the appropriate imports.

These include our RecyclerView from the android.support.v7.widget package and CheckBox from android.widget package.

Create Activity

An activity is an android component that representing a user interface.

To turn our ordinary class into an activity, we will make it derive from AppCompatActivity :

Create our POJO

Then we will create a POJO class, Plain OLd Java Object. This will represents a single Teacher object.

Each SpiritualTecaher will have several properties. Each object will be rendered in it’s own CardView in our RecyclerView.

Читайте также:  Showing popup in android

One of those properties will be the isSelected property, a boolean value that will hold for us the checked state of our CardView.

Create our View Holder

We will create our RecyclerView ViewHolder class by deriving from the RecyclerView.ViewHolder class.

Also the class will be implementing the View.OnClickListener interface. This will allow us to capture click events of our each CardView.

Create RecyclerView Adapter

Then we come create our RecyclerView.Adapter class, the class that will be our adapter class.

As an adapter class this class has two primary roles:

  1. Inflate our Custom Model Layout into a View Object.
  2. Bind data set to o our inflate views.

The data to be bound will be coming from a custom array, in this case an array of SpiritualTeachers:

This class will also maintain an arraylist that will hold our checkeed items.

Users will check the recyclerview items and we hold those checked items in this arraylist. Then later on we can display these checked items in a Toast message.

Inflate Layout into View

This will take place in our RecyclerView adapter, as we agreed. We need a LayoutInflater class to do this.

Then as you can see we’ve passed the inflate view object into our RecyclerView ViewHolder constructor.
Then returned the View Holder instance.

Binding Data

We bind data inside the onBindViewHolder method:

First we’ve received the position of the item in our recyclerview and used it to get the current teacher from our array.

Then set TextView, imageView and CheckBox widgets with values from that object.

Maintain Checked and UnChecked Items

We then listen to click events of our CardView. Obviously clicking the CheckBox will change the state of that checkbox.

We check for these changes. If the checkbox is clicked, we will add the item in our arraylist.

If it’s unchecked we remove the item from the arraylist.

Let’s look at the full source code.

Full Code

Here’s the full MainActivity code.

Example 2: SingleChoice RecyclerView – with RadioButtons

Android SingleChoice RecyclerView Tutorial

In this tutorial we want to see how to create a single choice recyclerview. That is a recyclerview with
radiobuttons. User can select a single item and we open the item’s detail activity thus showing it’s data.

Why SingleChoice RecyclerView?

Well the alternative is multi-choice recyclerview, that is recyclerview with checkboxes. However in some cases
you just want the user to be able to select a single item from the recyclerview. This is where radiobuttons come in.

However, in our case we will open a detail activity when the user selects a given radiobutton. Then we use intents to
pass data to the detail activity.

What do we show?

Well we show images, text and radiobuttons. Normally images get rendered in imageview. The text
will be rendered in textview. On the other hand the radiobuttons will
show for us a compound state, basically checked or not.

Step 1: Gradle Scripts

(a) Build.gradle

No special dependency is required. We add CardView and recyclerview as our dependencies.

The CardView will hold our Galaxy objects. The RecyclerView will hold multiple cardviews. AppCompat will give us the
AppCompatActivity from which our MainActivity extends.

Step 2: Create Layouts

We have three layouts

1. activity_main.xml

This layout will get inflated into MainActivity. It simply contains our RecyclerView. I have given mine a custom
background color.

2. activity_detail.xml

The Layout for our Detail activity. Contains imageview and textviews.to show
our data.

3. model.xml

This is our item view layout. It will get inflated into our recyclerview view items. This will be done by our RecyclerView
adapter.

Step 4: Write Java Code.

We have the following java classes for this project:

  1. Galaxy.java
  2. RecyclerAdapter.java
  3. DetailsActivity.java
  4. MainActivity.java
(a).Galaxy.java

This will represent our data object. It is our model class and represents a galaxy with properties like:

  1. Name.
  2. Description
  3. Image.

Here’s the full code.

You can see that we’ve implemented the Serializable interface. This allows us to serialize our data object so
that it can be transported to the DetailsActivity activity as a whole via intent.

(b). RecyclerAdapter.java

This is our adapter class. It will inflate our model layout into a view object that can be rendered by the recyclerview.
It will also bind data to those inflated view objects.

(c). DetailsActivity.java

Our detail activity class. This will show the details of the clicked galaxy.

(d). MainActivity.java

Our main activity class. This is where we reference our recyclerview and define our data source.

Download

You can download the full source code below or watch the video from the link provided.

No. Location Link
1. GitHub Direct Download
2. GitHub Browse
3. YouTube Video Tutorial
4. YouTube ProgrammingWizards TV Channel

Example 3: Kotlin Android RecyclerView – CardView with CheckBox,Images,Text and ItemClick

Kotlin Android RecyclerView with CheckBoxes, Images, text and ItemClick supports Tutorial and Example.

I want to show you how you can work with the recyclerview in this tutorial and do common things like using it with checkboxes, images, textview and also supporting click events. All these we do in a single MainActivity.kt file.

Video Tutorial

If you prefer a video tutorial then check here:

What You Learn

Here are the concepts you learn:

  1. How to use Recyclerview in android with Kotlin Programming Language.
  2. How to use CheckBox with recyclerview and handle the checkbox states appropriately.
  3. How to create a recyclerview with images and text.
  4. How to support item click event in recyclerview.

Tools Used

Here are the tools I used for this project:

  1. Programming Language : Kotlin
  2. Framework – Android
  3. IDE – Android Studio.
  4. Emulator – Nox Player
  5. OS : Windows 8.1

Here are the demo for this project:

(a). Build.gradle

Our app level build.gradle .
This is where we add our dependencies. Especially we are interested in three main of those:

  1. AppCompat – To give us the AppCompatActivity from which our MainActivity will derive.
  2. Design Support – To give us the RecyclerView.
  3. CardView – To give us Cardview.

(b). activity_main.xml

This is our main activity’s layout. Here are the components we use:

  1. RelativeLayout – To arrange our other widgets relative to each other.
  2. TextView – To render header text.
  3. RecyclerView – To render our data.
  4. Floating Action Button – To show the checked items when clicked.

(c). model.xml

This layout will define our item views for our recyclerview, it will model them. Basically it will be rendering data for a single SpiritualTeacher object. At the root we have a CardView. Then we also have:

  1. ImageView – To render image for the SpiritualTeacher object.
  2. TextVIew – To render his name as well as description.
  3. CheckBox – To allow us select and unselect the CardViews.

(d). MainActivity.kt

This is our main activity. This is our only file and we will define several inner kotlin classes here. This class will be deriving from the AppCompatActivity.

Create Data Object

We will start by defining an inner class called SpiritualTeacher :

That class is our data object. It will receive it’s parameters via the constructor. Moreover we are maintaining a simple boolean as you can see that will hold for us the selection state of the CardViews. In fact this is the magic to allow us select and unselect our items in our recyclerview. Thus our recyclerview will not lose the state even as we scroll and the recyclerview items get recycled. This is because we are holding them as data in our object and they are not affected by the recycling.

Create RecyclerVew Adapter class

Normally the Adapter has two main roles. First to inflate our custom layouts and secondly to bind data to the resultant widgets. However recyclerview always goes further by allowing us to recycle items. This saves memory consumption as inflation is an expensive process and would impact negatively on our app if we were to do it for every item in the recyclerview.

So we will create an adapter:

We’ve made it derive from the RecyclerView.Adapter class.

Inside the adapter first we will create an arraylist that will hold for us the teachers that have been checked:

It is inside the onCreateViewHolder() method where we will be inflating our model.xml into a View object. For that we use the LayoutInflater class.

Here’s the full source code.

Example 4: Android MySQL – RecyclerView CheckBoxes – INSERT,SELECT,SHOW

This is an android mysql recyclerview with checkboxes tutorial. We see how to work with both text and boolean values with our mysql database.

  • Today we explore how to work with Boolean and text values with MySQL and RecyclerView.
  • First we insert data to MySQL database.
  • The components we use as input views include chcekbox,spinner and material edittext.
  • The user types his favorite spacecraft’s name in the edittext,then selects that spaccecraft’s propellant in a spinner.
  • He then checks/unchecks a checkbox whether that technology exists now or not.
  • We save that data to MySQL on button click.
  • On retrieve’ button click,we select data from MySQL and bind this text and boolean to our Checked RecyclerView,RecyclerView with CheckBoxes.

First let me say that we have the full,well commented source code above for download as a full reference.We have included the PHP file that we used.You’ll place it in your server’s root directory and modify things like username,table name etc.

Video Tutorial(ProgrammingWizards TV Channel)

Well we have a video tutorial as an alternative to this. If you prefer tutorials like this one then it would be good you subscribe to our YouTube channel.

Basically we have a TV for programming where do daily tutorials especially android.

Android MySQL RecyclerView CheckBoxes Insert, Select Show Example

Let’s look at our example:

MySQL Table

Here’s our mysql table structure:

Project Structure

Here’s our project structure.

1. PHP Code

Here are our php code:

(a) Constants.php

Our PHP class to hold database constants like server name, database name, username and password. These credentials will be required for us to be able to connect to MySQL database.

(b) CRUD.php

This PHP file will allow us receive data from HTTP client by listening to and handling requests. We handle those requests by initiating the CRUD actions defined in our DBAdapater.php class.

(c) DBAdapter.php

This PHP file will contain functions that allow us perform CRUD operations on our MySQL database. Such actions include inserting and retrieving data to and from the database.

(d) Index.php

2. Gradle Scripts

Here are our gradle scripts in our build.gradle file(s).

(a). build.gradle(app)

Here’s our app level build.gradle file. We have the dependencies DSL where we add our dependencies.

This file is called app level build.gradle since it’s located in the app folder of the project.

If you are using Android Studio version 3 and above use implementation keyword while if you are using a version less than 3 then still use the compile keyword.

Once you’ve modified this build.gradle file you have to sync your project. Android Studio will indeed prompt you to do so.

We are using Fast Android Networking Library as our HTTP Client. You may use newer versions.

3. Resoources

Android platform provides a powerful and flexible way of adding static content as a resource.

These static content will also be packaged into the APK file. The static content will be stored either as a resource or as an asset.

Resources belong to a given type. These types can be:

  1. Drawable.
  2. Layout.
  3. Value.

Let’s start by looking at the layout resources

(a). model.xml

Our row model layout.

(b). content_main.xml
(c). activity_main.xml

This layout will get inflated into the main activity’s user interface. This will happen via the Activity’s setContentView() method which will require us to pass it the layout.

We will do so inside the onCreate() method of Activity.

3. Java Code.

Android apps can be mainly written in Java or Kotlin. These days however there are many frameworks like Flutter also which use languages like Dart.

In this class we are using Java programming language.

We will have these classes in our project.

(a). Spacecraft.java

First lets define our fictional spacecraft’s properties :

(b). MySQLClient.java

We are using Android Networking Library for our network calls.It does it in the background thread and is easy to use and fast.It shall be returning us JSOn responses that we can easily parse.Below is our MySQL client.

(c). MyViewHolder.java

Our ViewHolder class.

(d). MyAdapter.java

Below is our RecyclerView.Adapter class.Given that MySQL doesn’t have a native boolean data type.We shall be mapping our boolean checkbox values to nullable integers.

(d). MainActivity.java

This is our launcher activity as the name suggests. This means it will be the main entry point to our app in that when the user clicks the icon for our app, this activity will get rendered first.

We override a method called onCreate() . Here we will start by inflating our main layout via the setContentView() method.

Our main activity is actually an activity since it’s deriving from the AppCompatActivity.

AndroidManifest.xml

Make sure you add permission for internet connectivity.

Download

Hey,everything is in source code reference that is well commented and easy to understand and can be downloaded below.

Also check our video tutorial it’s more detailed and explained in step by step.

No. Location Link
1. GitHub Direct Download
2. GitHub Browse
3. YouTube Video Tutorial

report this ad

Oclemy

Thanks for stopping by. My name is Oclemy(Clement Ochieng) and we have selected you as a recipient of a GIFT you may like ! Together with Skillshare we are offering you PROJECTS and 1000s of PREMIUM COURSES at Skillshare for FREE for 1 MONTH. To be eligible all you need is by sign up right now using my profile .

Источник

Читайте также:  Top tv box 2021 android smart tv 4k
Оцените статью