Programming

Simple Android RecyclerView example

19 September 2026 · 12 min read

Simple Android RecyclerView example

Creating dynamic and efficient lists is a cornerstone of modern Android app development. The Simple Android RecyclerView example offers a powerful and flexible way to display large sets of data in a user-friendly manner. Unlike the older ListView, RecyclerView promotes efficient memory usage by recycling views that are no longer visible on the screen, significantly improving performance, especially when dealing with complex or extensive datasets. This enhanced performance translates to a smoother user experience, crucial for retaining users and ensuring app success. Understanding the core components and implementation process of a RecyclerView is essential for any Android developer looking to build high-quality, performant applications. This article will guide you through a step-by-step example, covering everything from setting up your project to displaying your data effectively. We’ll also explore best practices and common pitfalls to avoid along the way, helping you master this fundamental Android UI element.

Setting Up Your Android Project for RecyclerView

Before diving into the code, you need to set up your Android project correctly. This involves creating a new project in Android Studio or opening an existing one. Ensure that your project targets a reasonable API level (API 21 or higher is recommended) to take advantage of modern Android features and ensure compatibility with a wide range of devices. Once your project is open, you’ll need to add the RecyclerView dependency to your app’s build.gradle file. This dependency provides the necessary classes and resources to use RecyclerView in your application. Add this line to your dependencies block: implementation “androidx.recyclerview:recyclerview:1.2.1” (or the latest version). After adding the dependency, synchronize your project with Gradle files to download and install the required libraries. This process ensures that your project has all the necessary components to work with RecyclerView effectively. According to Google’s Android Developers documentation, using the latest version of the support library is crucial for security updates and bug fixes. Official Android RecyclerView Documentation

Next, you’ll need to add the necessary permissions to your AndroidManifest.xml file if your RecyclerView will display data fetched from the internet or accessed from device storage. Common permissions include android.permission.INTERNET for network access and android.permission.READ_EXTERNAL_STORAGE for accessing files. Remember to request these permissions at runtime if your app targets Android 6.0 (API level 23) or higher, as users need to grant permission explicitly. Proper permission handling is critical for maintaining user privacy and security, as emphasized by OWASP Mobile Security Project. OWASP Mobile Security Project Finally, create the layout file for your activity or fragment that will host the RecyclerView. This layout file will contain the RecyclerView widget and any other UI elements you need for your screen.

Creating the RecyclerView Layout and Adapter

The next crucial step is designing the layout for each item in your RecyclerView and creating the adapter that will populate the RecyclerView with data. The item layout defines how each individual item in the list will appear on the screen. This layout typically includes TextViews for displaying text, ImageViews for displaying images, and other UI elements as needed. Keep the item layout simple and efficient to minimize rendering overhead and ensure smooth scrolling performance. To create the layout, create a new XML file in the res/layout directory, such as item_layout.xml. Within this file, define the UI elements and their arrangement using LinearLayout, RelativeLayout, or ConstraintLayout. Consider using ConstraintLayout for complex layouts, as it offers flexibility and performance benefits.

The adapter acts as a bridge between your data source and the RecyclerView. It’s responsible for creating view holders, binding data to those view holders, and handling item clicks. To create an adapter, create a new Java or Kotlin class that extends RecyclerView.Adapter. Within the adapter class, you’ll need to implement three key methods: onCreateViewHolder, onBindViewHolder, and getItemCount. The onCreateViewHolder method is responsible for creating a new view holder instance. The onBindViewHolder method is responsible for binding data to the view holder. The getItemCount method returns the total number of items in the data set. It is important to use DiffUtil for complex datasets to improve the adapter performance. DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second. This can be used to update a RecyclerView adapter.

Here are some key points to remember when creating your RecyclerView adapter:

  • Use view holder pattern to cache view references and improve performance.
  • Implement onBindViewHolder efficiently to avoid unnecessary updates.
  • Consider using DiffUtil for large datasets to optimize updates.

Populating the RecyclerView with Data

Once you have created the layout and adapter, you can populate the RecyclerView with data. This involves creating a data source, such as an array list or a database query, and passing it to the adapter. In your activity or fragment, obtain a reference to the RecyclerView using findViewById. Then, create an instance of your adapter and pass it the data source. Finally, set the adapter to the RecyclerView using recyclerView.setAdapter(adapter). You’ll also need to set a layout manager to the RecyclerView. The layout manager is responsible for positioning the items in the RecyclerView and determining how they are scrolled. Common layout managers include LinearLayoutManager, GridLayoutManager, and StaggeredGridLayoutManager. Choose the layout manager that best suits your needs and the visual style of your application. For example, a LinearLayoutManager displays items in a linear fashion (either vertically or horizontally), while a GridLayoutManager displays items in a grid. Proper choice of layout manager can significantly impact the user experience. The following snippet is optimized for featured snippets:

To display data in a RecyclerView, you first need to create a data source, such as an ArrayList. Then, instantiate your custom adapter, passing it the data source. Finally, set the adapter to your RecyclerView using recyclerView.setAdapter(adapter). Remember to also set a layout manager, like LinearLayoutManager, to define how the items are displayed. This ensures that your data is properly rendered within the RecyclerView, making it visible to the user.

Let’s look at the steps involved more closely:

  1. Create a data source (e.g., ArrayList of strings or objects).
  2. Instantiate your custom RecyclerView adapter, passing the data source as an argument.
  3. Obtain a reference to your RecyclerView using findViewById.
  4. Set the layout manager for the RecyclerView (e.g., LinearLayoutManager).
  5. Set the adapter to the RecyclerView using recyclerView.setAdapter(adapter).

Handling Item Clicks and Interactions

RecyclerViews are not just for displaying data; they also allow users to interact with the items in the list. Implementing item click listeners is a common requirement for many Android applications. To handle item clicks, you can implement an OnClickListener within your view holder class. When a user clicks on an item, the onClick method will be called, allowing you to perform actions such as displaying a detailed view of the item, launching a new activity, or updating the data source. To implement this, create an interface within your adapter that defines a method for handling item clicks. Then, pass an instance of this interface to the adapter’s constructor. In the onBindViewHolder method, set the OnClickListener on the item view and call the interface method when the view is clicked. This approach provides a clean and flexible way to handle item clicks in your RecyclerView. It also promotes separation of concerns by decoupling the adapter from the click handling logic.

Beyond simple clicks, RecyclerViews can also support other interactions such as long presses, swipes, and drag-and-drop. To implement these interactions, you can use the ItemTouchHelper class. ItemTouchHelper provides callbacks for handling swipe-to-dismiss and drag-and-drop operations. You can customize the behavior of ItemTouchHelper by overriding its callback methods. For example, you can define different swipe directions for different items or implement custom animations when an item is swiped or dragged. Implementing complex interactions in RecyclerViews can significantly enhance the user experience and make your application more engaging. Consider implementing swipe-to-delete functionality or drag-and-drop reordering for lists where these interactions are relevant.

  • Implement OnClickListener within the ViewHolder to handle clicks.
  • Use ItemTouchHelper for swipe-to-dismiss and drag-and-drop functionality.
Infographic here
Remember that performance is key when dealing with RecyclerViews, especially when handling interactions. Avoid performing heavy operations on the main thread, as this can lead to UI freezes and a poor user experience. Offload long-running tasks to background threads using AsyncTask, ExecutorService, or Kotlin coroutines. Also, be mindful of memory usage, especially when dealing with images. Use image loading libraries like Glide or Picasso to efficiently load and cache images. Regularly profile your RecyclerView implementation to identify and address any performance bottlenecks. Proper optimization is essential for ensuring a smooth and responsive user experience, even when dealing with large datasets or complex interactions. You can see [more examples here](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c).

FAQ About Android RecyclerView

What is the main difference between RecyclerView and ListView?
RecyclerView recycles views that are no longer visible, improving performance, while ListView does not have built-in view recycling.
What is a ViewHolder in RecyclerView?
A ViewHolder holds references to the views within each item in the RecyclerView, reducing the need to repeatedly call findViewById.
How do I handle item clicks in RecyclerView?
Implement an OnClickListener within the ViewHolder and pass the click event to an interface or callback in the activity/fragment.
What layout managers can I use with RecyclerView?
Common layout managers include LinearLayoutManager, GridLayoutManager, and StaggeredGridLayoutManager, each providing different ways to arrange items.
How do I update the data in a RecyclerView?
Update the data source and call notifyDataSetChanged() on the adapter, or use DiffUtil for more efficient updates.
Building a **Simple Android RecyclerView example** doesn't have to be daunting. By understanding the core components – the layout, the adapter, and the data source – and by focusing on best practices like view recycling and efficient data handling, you can create smooth, performant lists that enhance the user experience. Remember to optimize for performance, especially when dealing with large datasets or complex interactions. Explore the resources and examples provided to deepen your understanding and master this essential Android UI element. Now, go ahead and implement these techniques in your projects and build amazing, user-friendly Android apps! For more advanced techniques, consult the official Android documentation. [Android Developers](https://developer.android.com)**Question & Answer :** I've made a list of items a few times using Android's `RecyclerView`, but it is a rather complicated process. Going through one of the numerous tutorials online works ([this](http://stacktips.com/tutorials/android/android-recyclerview-example), [this](https://github.com/codepath/android_guides/wiki/Using-the-RecyclerView), and [this](https://www.youtube.com/watch?v=Wq2o4EbM74k) are good), but I am looking a bare bones example that I can copy and paste to get up and running quickly. Only the following features are necessary:
  • Vertical layout
  • A single TextView on each row
  • Responds to click events

Because I have wished for this several times, I finally decided to make the answer below for my future reference and yours.

The following is a minimal example that will look like the following image.

RecyclerView with a list of animal names

Start with an empty activity. You will perform the following tasks to add the RecyclerView. All you need to do is copy and paste the code in each section. Later you can customize it to fit your needs.

  • Add dependencies to gradle
  • Add the xml layout files for the activity and for the RecyclerView row
  • Make the RecyclerView adapter
  • Initialize the RecyclerView in your activity

Update Gradle dependencies

Make sure the following dependencies are in your app gradle.build file:

implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' 

You can update the version numbers to whatever is the most current. Use compile rather than implementation if you are still using Android Studio 2.x.

Create activity layout

Add the RecyclerView to your xml layout.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rvAnimals" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> 

Create row layout

Each row in our RecyclerView is only going to have a single TextView. Create a new layout resource file.

recyclerview_row.xml

<?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:orientation="horizontal" android:padding="10dp"> <TextView android:id="@+id/tvAnimalName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp"/> </LinearLayout> 

Create the adapter

The RecyclerView needs an adapter to populate the views in each row with your data. Create a new java file.

MyRecyclerViewAdapter.java

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private List<String> mData; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, List<String> data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the row layout from xml when needed @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_row, parent, false); return new ViewHolder(view); } // binds the data to the TextView in each row @Override public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData.get(position); holder.myTextView.setText(animal); } // total number of rows @Override public int getItemCount() { return mData.size(); } // stores and recycles views as they are scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = itemView.findViewById(R.id.tvAnimalName); itemView.setOnClickListener(this); } @Override public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData.get(id); } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // parent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } 

Notes

  • Although not strictly necessary, I included the functionality for listening for click events on the rows. This was available in the old ListViews and is a common need. You can remove this code if you don’t need it.

Initialize RecyclerView in Activity

Add the following code to your main activity.

MainActivity.java

public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener { MyRecyclerViewAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // data to populate the RecyclerView with ArrayList<String> animalNames = new ArrayList<>(); animalNames.add("Horse"); animalNames.add("Cow"); animalNames.add("Camel"); animalNames.add("Sheep"); animalNames.add("Goat"); // set up the RecyclerView RecyclerView recyclerView = findViewById(R.id.rvAnimals); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new MyRecyclerViewAdapter(this, animalNames); adapter.setClickListener(this); recyclerView.setAdapter(adapter); } @Override public void onItemClick(View view, int position) { Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show(); } } 

Notes

  • Notice that the activity implements the ItemClickListener that we defined in our adapter. This allows us to handle row click events in onItemClick.

Finished

That’s it. You should be able to run your project now and get something similar to the image at the top.

Going on

Adding a divider between rows

You can add a simple divider like this

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), layoutManager.getOrientation()); recyclerView.addItemDecoration(dividerItemDecoration); 

If you want something a little more complex, see the following answers:

Changing row color on click

See this answer for how to change the background color and add the Ripple Effect when a row is clicked.

Insert single item

Updating rows

See this answer for how to add, remove, and update rows.

Insert single item

Further reading