How to display values of  EditText in recyclearview in android

Recyclerview is the best view group for display items and it prevents memory wastage. Recyclerview is an efficient way to implement a scrollable list. We can use Recyclerview for multipurpose like for display contacts, songs, etc.

In this article, you will learn to display inserted values of  EditText in recycler view. We display items in recycler view with the help of the adapter class and those items are stored in ArrayList.So to display edit text values in recycler view we have to add inserted edit text values in ArrayList.

Structure : Edittext inserted values → Arraylist → adapter → recyclearview

To understand this you should have basic knowledge of the recycler view. so let's start.


Step1: Create your XML 

First, create your XML file as your requirement, add Edittext in your XML file, and give proper ID to your edit text. In my case, I have added only one edit text in my activity. here is XML code :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="add item"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.278"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.976" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD"
        android:textColor="#000000"
        app:backgroundTint="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="@+id/edittext1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/edittext1"
        app:layout_constraintTop_toTopOf="@+id/edittext1" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclearview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/edittext1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Now, let's move on second step.


Step2: Create an Adapter class for bind items with recycler view

Now create adapter.java class and extends this class as below shown :
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {}
Now inside this class, create inner class MyViewHolder (this inner class is for initializing item views). and initialize your views inside the constructor:
public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView item_textview;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            item_textview=itemView.findViewById(R.id.item_textview);
        }
    }
 Now we have to create fields and generate a constructor for initializing these fields as shown below:

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
    @NonNull
    private Context context;
    private ArrayList<String> item_text;

    public Adapter(@NonNull Context context, ArrayList<String> item_text) {
        this.context = context;
        this.item_text = item_text;
    }
And implement all abstract methods inside the Adapter class :

    @Override
    public Adapter.MyViewHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        View view=layoutInflater.inflate(R.layout.item_layout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull  Adapter.MyViewHolder holder, int position) {
        holder.item_textview.setText(item_text.get(position));
    }

    @Override
    public int getItemCount() {
        return item_text.size();
    }


  • Adapter.java complete code:

package com.example.recyclearview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
    @NonNull
    private Context context;
    private ArrayList<String> item_text;

    public Adapter(@NonNull Context context, ArrayList<String> item_text) {
        this.context = context;
        this.item_text = item_text;
    }

    @Override
    public Adapter.MyViewHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        View view=layoutInflater.inflate(R.layout.item_layout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull  Adapter.MyViewHolder holder, int position) {
        holder.item_textview.setText(item_text.get(position));
    }

    @Override
    public int getItemCount() {
        return item_text.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView item_textview;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            item_textview=itemView.findViewById(R.id.item_textview);
        }
    }
}


Step3: Prepare your recycler view item_layout:

We will need one more layout Resource File to design Recyclerview-Item. Because this Item layout will be needed later in the Adapter class. 

To create a new layout resource file do the following :

  1. Right-click on the " res folder
  2. Select new
  3. Select Android Resource File
  4. Give a specific name to the file and press ok

item_layout.xml -

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/item_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="TextView"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>


Step4: MainActivity.java class:

After completing the 3 steps, now we have to implement our MainActivity class. 

But how we can display values of  EditText in recycler view? so for that, we will create ClickListener on ADD button and we have to create ArrayList.

So when the user clicks on the ADD button, we will add edit text values in that ArrayList.

Now we have to set our Adapter class with recycler view to display our Arraylist in the recyclearview.

For that, add MainActivity.java :

package com.example.recyclearview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    Button button;
    ArrayList<String> items;
    Adapter adapter;
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=findViewById(R.id.edittext1);
        button=findViewById(R.id.button);
        recyclerView=findViewById(R.id.recyclearview);
        items=new ArrayList<>();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                items.add(editText.getText().toString());
                adapter.notifyDataSetChanged();
            }
        });

        adapter=new Adapter(MainActivity.this,items);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    }
}


  • Run the app

After completing all these steps you will get a result like this:


So as you can see we have Successfully implemented Edit text with recycler view. But when you restart your app, those recycler view values will be gone because we had not store those values in any database.

So if you want to display those values until the user deletes them, then you can use SQLite or Room database.

So this is a simple example of  Edit text with recyclearview. and in this way, we can display values of  EditText in recyclearview in android.

Leave a comment if you had any queries regarding this post and I will reply as soon as possible. Anyway, if you enjoy reading my content, you can subscribe to my blog to receive all the latest updates. Happy Learning!

Share this article with your friends✌!!