Android

Recyclerview in Android Studio with examples

Description:

Recyclerview in Android Studio with examples:- hello guys welcome back this is another Android application development tutorial in this article, I am going to show you how to display a list of items in your Android studio project so, I will show you some examples

this is an example of a list of item

Recyclerview in android studio
Figure 1

Here is another example

Recyclerview in android studio
Figure2

so in this case the items are images.

so now in this article, I’m going to show you how to display the figure1 type of list in your Android studio project.

so in figure2 the items are images this is an example of a grid layout manager and figure1 is an example of a linear layout manager.  so in linear layout manager, there is only one column and n number of rows but in the case of a grid layout manager, there is n number of rows and n number of columns. so in the android framework, there is a widget available called a listview to display the figure1 type of list. but in this article we are going to working with recyclerview in android studio, recyclerview is nothing but an advanced version of listview. so in this article, we are going to work with a recyclerview in android studio to display a list of items similar to figure1. so I will show you how to display the list with an example. So let get started.


so we can start with a new Android studio project,

Recyclerview in android studio

select empty activity and click next button

Recyclerview in android studio

Then select the application name, in my case, I select the name “RecyclerViewDemo” and click on the finish button .

Recyclerview in android studio

Our project is created successfully.

Recyclerview in android studio

Before you start working on recyclerview in android studio make sure you added the dependency for using the recyclerview in Android studio project.



Adding the Recyclerview Dependency in Android Studio

The recyclerview is not a direct part of the android framework you how to add a recyclerview as a support library so you can add a support library through the Gradle in your Android studio project.

so now here I’m going to show you how to add a recyclerview  in android studio project so for that just open the Gradle script files, open the module level Gradle file

Recyclerview in android studio

at the bottom of this file, we have a section called the dependencies

Recyclerview in android studio

Now here the number of dependencies are available, so now here I’m going to add the dependency for using a recyclerview using the following dependency

Recyclerview in android studio

after adding the recyclerview dependency sync the project.

Recyclerview in android studio

so here the project Sync finished successfully.  so we successfully added the recyclerview in android studio project.


Adding Items in String file for RecyclerView in Android Studio:

first, we need a list of items, for that simply click on res folder, in res folder click on values folder, in values folder double click on Strings.xml file as you can see below figure.

Recyclerview in android studio

In String.xml file I create a list of programming languages,

so now we have a string array available inside the string stored XML file with the name programming_languages.


Adding RecyclerView Layout in Android Studio:

so now we can add the recyclerview to the layout activity_main.xml.  so now currently there is only one activity available in this project. When you open this layout file here is only one textview is available and the root element is a constrained layout.

So first of all change the layout from a constrained layout to a relative layout.

so the first step we need to add a recyclerview to the layout file

I set the layout_width to match the parent and height also match the parent.

Then I set the recycler as id.

For scrolling view we need a scroll property, therefore I add the scrollbars. in the preview window, you can see the list of items is displayed

Recyclerview in android studio

so that’s enough for RecyclerView.


Main Component of RecyclerView in Android Studio:

so for a recyclerview we need three things first one is the layout manager, the second one is the view holder, and the third one is the adapter.

Layout Manager:

Recyclerview in android studio

in this example there is only one column is available and it is a linear layout manager.

Recyclerview in android studio
Figure2

but in this example we have two columns is available. so here we used a grid layout manager so the grid layout manager arrays items in two rows and columns so you can use a number of rows and the number of columns.

so that is the purpose of a layout manager the layout manager array item on the list into rows and columns. so for a recyclerview we need the layout manager.

View holder:

for example here

Recyclerview in android studio
Figure2

each item on the recyclerview in this example each item contain an image view and a text view the image will display the image and the text view display a name for the image so each item is represented using a class called the view holder so which is the second thing for the recyclerView in android studio.


Adapter:

The responsibility of the adapter is to place the view holder into the proper position in the recyclerview or in the list

RecyclerView in Android Studio:

now open the MainActivity.java file. so first thing I create a handler for the recyclerview so first I am going to create a handler so

then I create some variables for the layout manager I named it layout manager

so here we need to display a list similar to this one

Recyclerview in android studio

so we need a simple linear layout manager we need only one column.

so I initialize the layout manager it is a linear layout manager and for the constructor of that class you need to pass one parameter at least one parameter that is context so here I pass the activity context. So now the layout manager is ready.

in this statement we to tell the recyclerview we need the layout manager for that i call the method and set to layout manager and i pass the layout manager object so in this statement we set the layout manager for the recyclerview.

so now we need to display the items that means we have to create the adapter and the view holder class so the view holder class is responsible for each item on the recycler view.



Creating Separate Layout file for RecyclerView:

so before going to create the adapter, I need to create a separate layout in our case we have to display only a text, simple text that means we need a simple text view for each item on the list for that here I’m going to create a separate layout file

simply right-click on layout then select new in new select layout Resource file

Recyclerview in android studio

Then set the name of the layout file, and change the root element  from constraint to textview, and press the ok button

Recyclerview in android studio

And paste the below code in text_layout file:

so this TextView display each item in a recyclerview.


Creating Adapter for RecyclerView:

So now we can create the adapter so right-click the package and create a new Java class here I named it as RecyclerAdapter

Recyclerview in android studio

Recyclerview in android studio

so this class represents an adapter for the recyclerview so you have to extends adapter, adapter for reyclerview.

Recyclerview in android studio

so here we got some errors because you need to implement some methods now you need to implement three methods oncreateview holder, onbindviewholder, and getitemcount for that just click on the red bulb and click on implement methods

Recyclerview in android studio

Select all three methods and press the ok button.

Recyclerview in android studio


full Project codes:

Add the below code in RecyclerAdapter.java file:

MainActivity.java file code:



activity_main.xml file code:

String.xml file code:

text_layout.xml file code:


The final output of RecyclerView in Android Studio:

Recyclerview in android studio

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button