Android RecyclerView with EditText Example

Android RecyclerView with EditText example tutorial guide you to add edittext in the every cell or child of the recyclerview.

I have used a Model class to maintain proper data and the value of edittext in the recyclerview.

You will also learn how to fetch value of the EditText of all the rows. We will pass these values to the Next Activity.

Most common problems regarding this topic are

  1. Value of edittext get change when scrolling
  2. Edittext loses content on scroll in recyclerview

  3. Edittext loses focus in recyclerview

This tutorial will solve all the above problems in this Android RecyclerView with EditText Example.

We will use the TextWatcher class to control the changes of the edittext.

Check out the output of Android RecyclerView With Edittext tutorial.

Download Source Code For Android RecyclerView With EditText Tutorial

[sociallocker]Recyclerview_Edittext [/sociallocker]

Also Read,

1. Make RecyclerView item

Create a resource file and give it a name rv_item.xml

This file shows how a single cell of recyclerview will be look like.

2. Updating build.gradle(Module:app)

Add below lines in build.gradle(Module:app)

Above lines will add support for recyclerview and cardview. By default, it is not supported in android studio project.

Full code

3. Preparing Model Class

Make a new java class named EditModel.java

  • In our recyclerview, only the value of edittext is changing so we need to maintain it with model class.
  • Above class is that model who holds the getter and setter methods for the value edittext.

4. Adapter class

Create a new class and it’s name should be CustomAdapter.java

First of all, consider the below code

    • First line will create the object of the LayoutInflater class.
    • Second will create one Arraylist with objects of the EditModel class.
    • Then I have created a constructor of the adapter class.
    • Compiler will get a context and arraylist from the parameter of this constructor.

Now look at the below code

  • In the above code, we are setting the value of the EditText.
  • Here, we are using that Arraylist with the objects of the EditModel class.
  • Main logic is present in adapter class. We will change and store the value of edittexts in this.
    Look at the following code

  • When user changes the value of edittext, above lines will be excuted.
  • When text is being changed, we will store the new value of edittext in arraylist.
  • Arraylist is made public in the adapter class. This arraylist can be used anywhere in the whole application.

5. Making next activity

Make a new activity with name NextActivity.java

Add below source code in this class

  • In this next activity, we will show all the values of edittexts.
  • Here, we have used that public arraylist to fetch the values.

Updating activity_next.xml

Copy below code

6. Changing MainActivity.java

Update MainActivity.java class with following source code

Pay attention at the below code

  • Above line is creating a data structure. Compiler will use populateList() method for this purpose.

Following is the code for populateList() method.

  • In this method, compiler will run one for loop for seven times.
  • In the each iteration, it will create one object of the EditModel class. Then it will set the value of the edittext.

Read the following code

  • Compiler will run the above code when the user clicks on the button.
  • It will open a Next Activity via above code snippet.

Add below code in activity_main.xml

I have taken one button and one recyclerview in the main layout.

Thus, edittext is not losing focus neither it’s value is changed on scroll.

We always try to solve the hard and complex real world problems regarding android development.

Keep visiting other tutorials for other wonderful logic and time saving techniques.

So, it was all for the tutorial.

If you have any questions regarding logic of adapter class or anything else, then feel free to ask in comment section.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

16 Responses

  1. Ammi Singh says:

    thanks you sir for this post

  2. manjoor alam says:

    your example is not working in my project

  3. Subhash Salian says:

    I tried working on your example with multiple edittextboxes, but somehow it didn’t work. The data is getting displayed correctly, but I am not able to input any characters into the edittextboxes.

  4. Subhash Salian says:

    hi Hardik,

    The post is very good and you have explained it very well. I am trying with multiple edittextboxes using your example, however i find that the edittextboxes are not receiving focus. I have implemented textwatcher for each edittextboxes in the Viewholder class. Please share your inputs.

    Thanks,

    Subhash

    • Hardik Parsania says:

      I guess you are trying to add two or more edittexts in one single row.
      Are you able to input text in one single edittext among all or non of edittext is receiving focus?

      • ahmad t says:

        i’m right now facing this problem.
        i need to add tow edit texts in one row how can do that.

        • Hardik Parsania says:

          Add edittext in row_layout.xml
          Add edittext in model class same as we have addedfirst edittext.
          Then also add code in adapter class for your second edittext same as i have added for first edittext.

  5. Shreya Garg says:

    i tried your way it didnt worked for me….can u pls help

  6. davod_basiri says:

    hi sir i tried your way but have one error
    void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference

    • Hardik Parsania says:

      I have not set any onclick method for textview in this example. You are getting this error because you have not initialize your textview.

      I think you are integrating this project directly in your existing project.

      Make separate project and follow this example. Once you learn it completely then try to integrate it with your existing project.

  7. davod_basiri says:

    tried your way it didnt worked
    ‘void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference

  8. Omar says:

    I know this is incredibly late but I was curious how are you able to call “setEditTextValue” without instantiating the model class.I’m doing something very similar instead of an array list I’m getting my data from an Sqlite database. I would really appreciate any feedback!

    • Hardik Parsania says:

      I have used setEditTextValue in adapter class and in Main Activity. Which setEditTextValue you are taiking about?

Leave a Reply

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