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
<?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
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:
To create a new layout resource file do the following :
- Right-click on the " res " folder
- Select new
- Select Android Resource File
- 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 if you want to display those values until the user deletes them, then you can use SQLite or Room database.
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✌!!

0 Comments