Android RecyclerView provides advanced and powerful features to display a large amount of data in your app. In this article, we learn How To Add, Remove, and Update Items In Android RecyclerView with the help of an example.
Here are resources, which help you to learn more about Android RecyclerView.
- RecyclerView Implementation With CardView Basic Example In Android.
- Fetch JSON data using Volley library and display it in RecyclerView — Android Studio
In this tutorial, we work for the following solution with the help of an example in Android Studio:
- How to Add and Remove RecyclerView Items In Android Studio.
- How to Update RecyclerView Items In Android Studio.
Features Of Application
- Display the list of items in RecyclerView.
- Add a New Item in RecyclerView on the click of a button.
- Remove an item from the RecyclerView on the click of an item button.
- Update an item of the RecyclerView on click of the update button.
Demo
Create A New Project
Before Implementing the following code first create a new project in Android Studio, go to File => then go to New => then select the New Project option. In Add an activity option select default activity(Blank Activity) and proceed.
Structure Of Code
The following image is showing the project structure. Total three java files I have created over here MainActivity, Model, and RvAdapter similarly total two XML files created one for MainActivity that is “activity_main.xml” and another for RecyclerView item that is “item_list.xml“.

Code
Code for build.gradle (Module: app )
Add the following two additional libraries in dependencies of your build.gradle (Module: app ):
- implementation ‘com.android.support:recyclerview-v7:28.0.0’
- implementation ‘com.google.code.gson:gson:2.8.2’
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.rvadddeleteupdate" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.google.code.gson:gson:2.8.2' }
Code for AndroidManifest.xml file.
The following code is for the android application Manifest File which is available in your project root directory. The manifest file defines the project structure this is a very important part of the application.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.rvadddeleteupdate"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Code for MainActivity.java file.
This is the main activity of the application where we implement the complete functionality like:
- Android RecyclerView implementation.
- Insert New Item in Recyclerview on click of Add button.
- Delete Item of RecyclerView on click of cancel button of the item.
- Enable the update option on click of RecyclerView item.
- Update the item of RecyclerView on click of Update button.
package com.example.rvadddeleteupdate; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.google.gson.Gson; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { ArrayList<Model> models = new ArrayList<Model>(); RecyclerView rvTechSolPoint; RvAdapter rvAdapter; TextView tvAdd, tvUpdate; EditText etEnterName; int position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rvTechSolPoint = findViewById(R.id.rv_list_item); tvAdd = findViewById(R.id.tv_add); etEnterName = findViewById(R.id.et_enter_name); tvUpdate = findViewById(R.id.tv_update); rvTechSolPoint.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false); rvTechSolPoint.setLayoutManager(layoutManager); rvAdapter = new RvAdapter(getApplicationContext(), models, new RvAdapter.Onclick() { @Override public void onEvent(Model model, int pos) { position = pos; tvUpdate.setVisibility(View.VISIBLE); etEnterName.setText(model.getName()); } }); rvTechSolPoint.setAdapter(rvAdapter); tvAdd.setOnClickListener(this); tvUpdate.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.tv_add: { insertMethod(String.valueOf(etEnterName.getText())); } break; case R.id.tv_update: { models.get(position).setName(etEnterName.getText().toString()); rvAdapter.notifyDataSetChanged(); tvUpdate.setVisibility(View.GONE); } break; } } private void insertMethod(String name) { Gson gson = new Gson(); try { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", name); Model model = gson.fromJson(String.valueOf(jsonObject), Model.class); models.add(model); rvAdapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } }
Code for the activity_main.xml file.
The following XML code is for the main UI of the application. In this layout add the RecyclerView widget and all the views to handle the functionality.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:background="#FFFFFF" android:orientation="vertical" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_list_item" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </android.support.v7.widget.RecyclerView> <LinearLayout android:layout_width="400dp" android:layout_height="300dp" android:layout_centerInParent="true" android:layout_marginEnd="50dp" android:background="@drawable/ss" android:gravity="center" android:orientation="vertical"> <EditText android:id="@+id/et_enter_name" android:layout_width="match_parent" android:layout_height="60dp" android:background="#ffffff" android:hint="Enter Name..." android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:paddingStart="10dp" /> <TextView android:id="@+id/tv_add" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="20dp" android:layout_marginStart="30dp" android:layout_marginEnd="30dp" android:background="@color/colorAccent" android:gravity="center" android:text="ADD" android:textColor="#ffffff" android:textSize="25sp" android:textStyle="bold" /> <TextView android:visibility="gone" android:id="@+id/tv_update" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="20dp" android:layout_marginStart="30dp" android:layout_marginEnd="30dp" android:background="@color/colorAccent" android:gravity="center" android:text="UPDATE" android:textColor="#ffffff" android:textSize="25sp" android:textStyle="bold" /> <ImageView android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="20dp" android:src="@drawable/logo"/> </LinearLayout> </LinearLayout>
Code for Model.java file.
Now we create a model class to hold our new inserted data and also add the getter/setter methods for all the variables which are defined in this class.
package com.example.rvadddeleteupdate; public class Model { public int id; public String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Code for RvAdapter.java file.
This source code for the adapter class which helps in render the data.
package com.example.rvadddeleteupdate; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; public class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvViewHolder> { Context context; ArrayList<Model> models; Onclick onclick; public interface Onclick { void onEvent(Model model,int pos); } public RvAdapter(Context context, ArrayList<Model> models, Onclick onclick) { this.context = context; this.models = models; this.onclick = onclick; } View view; @Override public RvAdapter.RvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); view = inflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false); RvViewHolder rvViewHolder = new RvViewHolder(view); return rvViewHolder; } @Override public void onBindViewHolder(RvAdapter.RvViewHolder holder, final int position) { final Model model = models.get(position); if (model.getName() != null) { holder.itemName.setText(model.getName()); } holder.removeImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { models.remove(position); notifyDataSetChanged(); } }); holder.llItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onclick.onEvent(model,position); } }); } @Override public int getItemCount() { return models.size(); } public class RvViewHolder extends RecyclerView.ViewHolder { TextView itemName; ImageView removeImg; LinearLayout llItem; public RvViewHolder(View itemView) { super(itemView); itemName = itemView.findViewById(R.id.tv_name); removeImg = itemView.findViewById(R.id.img_remove); llItem = itemView.findViewById(R.id.ll_item); } } }
Code for the item_list.xml file.
This is your one item layout of RecyclerView which helps to display a large number of the data list. In this item UI, I have defined a TextView with id “tv_name” to display a name and an ImageView with id “img_remove” to display a cancel icon on this icon perform an action to remove that particular item of RecyclerView.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll_item" android:background="#ffffff" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="match_parent" android:text="kunal" android:gravity="center_vertical" android:layout_marginStart="10dp" android:textSize="20sp" android:layout_weight="1" /> <ImageView android:id="@+id/img_remove" android:layout_width="50dp" android:layout_height="match_parent" android:layout_margin="5dp" android:rotation="45" android:src="@drawable/add_button" android:tint="@color/colorAccent" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorAccent"/> </LinearLayout>