RecyclerView
Android Development
UI Design
Empty View
Android Studio

How to show an empty view with a RecyclerView?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A RecyclerView that renders nothing without explanation usually feels broken to the user. Because RecyclerView has no built-in empty-view API like old ListView patterns did, you have to manage the empty state yourself by showing a separate view whenever the adapter has no items.

Put the List and Empty View in the Same Layout

The simplest approach is to place both views in one parent container and toggle their visibility.

xml
1<?xml version="1.0" encoding="utf-8"?>
2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent">
5
6    <androidx.recyclerview.widget.RecyclerView
7        android:id="@+id/recyclerView"
8        android:layout_width="match_parent"
9        android:layout_height="match_parent" />
10
11    <LinearLayout
12        android:id="@+id/emptyView"
13        android:layout_width="match_parent"
14        android:layout_height="match_parent"
15        android:gravity="center"
16        android:orientation="vertical"
17        android:visibility="gone">
18
19        <TextView
20            android:layout_width="wrap_content"
21            android:layout_height="wrap_content"
22            android:text="No items available" />
23    </LinearLayout>
24
25</FrameLayout>

This is enough for many screens. The empty state can later be expanded with an icon, a retry button, or an explanation for why the list is empty.

Toggle the Empty State When the Adapter Changes

Once the views exist, the next job is deciding when to show one or the other. A practical pattern is to observe the adapter and recalculate visibility whenever the data changes.

kotlin
1class ItemFragment : Fragment(R.layout.fragment_items) {
2
3    private lateinit var recyclerView: RecyclerView
4    private lateinit var emptyView: View
5    private lateinit var adapter: ItemAdapter
6
7    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
8        super.onViewCreated(view, savedInstanceState)
9
10        recyclerView = view.findViewById(R.id.recyclerView)
11        emptyView = view.findViewById(R.id.emptyView)
12        adapter = ItemAdapter()
13
14        recyclerView.layoutManager = LinearLayoutManager(requireContext())
15        recyclerView.adapter = adapter
16
17        adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
18            override fun onChanged() = updateEmptyState()
19            override fun onItemRangeInserted(positionStart: Int, itemCount: Int) = updateEmptyState()
20            override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) = updateEmptyState()
21        })
22
23        loadItems()
24    }
25
26    private fun updateEmptyState() {
27        val isEmpty = adapter.itemCount == 0
28        recyclerView.visibility = if (isEmpty) View.GONE else View.VISIBLE
29        emptyView.visibility = if (isEmpty) View.VISIBLE else View.GONE
30    }
31
32    private fun loadItems() {
33        adapter.submitList(emptyList())
34        updateEmptyState()
35    }
36}

This works well for simple screens where the UI decision really is just "show the list" or "show the empty state."

Distinguish Empty, Loading, and Error States

A more complex screen usually needs more than one non-list state. An empty result after a successful load is not the same thing as a loading spinner or a network failure.

That is why many larger screens use a UI state model such as:

kotlin
1data class UiState(
2    val items: List<String> = emptyList(),
3    val loading: Boolean = false,
4    val errorMessage: String? = null
5)

Then the fragment renders based on the full state instead of guessing from adapter.itemCount alone. That avoids awkward cases where an empty message flashes briefly while data is still loading.

Make the Empty View Useful

A good empty state should explain the situation and, if possible, suggest a next step.

Examples:

  • "No saved items yet"
  • "No results match the current filters"
  • "Nothing downloaded yet"
  • "Tap retry to load again"

That is much better than a vague blank page or a generic "no data" label.

Why This Logic Should Usually Live Near the Screen

Some teams try to hide empty-state logic inside a custom adapter. That can work, but it also mixes UI-state decisions into a class that is really meant to bind rows. In many apps, it is clearer for the fragment or activity to decide whether the list itself should be visible.

The adapter should usually answer "how do I render items," while the screen state answers "should the list be shown at all."

Common Pitfalls

A common mistake is checking emptiness once during initialization and never updating it after the data changes. The result is an empty view that stays visible forever or never appears again.

Another issue is treating loading and empty as the same state. That creates confusing flicker where the user sees "No items" for a moment while the network request is still in flight.

Developers also often make the empty state too passive. If the user can recover with retry, clear filters, or create-first-item actions, the empty view should expose that directly.

Summary

  • 'RecyclerView has no built-in empty view, so you must manage one yourself.'
  • Place the list and empty view in the same layout and toggle their visibility.
  • An AdapterDataObserver is a simple way to react to list changes.
  • Distinguish empty, loading, and error states instead of merging them.
  • A useful empty view should explain the situation and ideally suggest the next action.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.