How to setEmptyView to RecyclerView

Right now I would really like to begin new collection about ideas for RecyclerView. Virtually all purposes display info in the lists. We’ve two primary elements for displaying record of knowledge in Android. It’s ListView and RecyclerView. The RecyclerView is extra environment friendly element from an Android documentation.

After first run some purposes has an empty record of knowledge, as instance Notes or ToDo record. In fact, it’s actually dangerous consumer experience to present empty listing. Ideally, we should always show some message which explains present state of affairs and/or how to add a knowledge.

Once we come again to ListView, this element has a easy technique which permits display a view when listing is empty. It is referred to as setEmptyView technique and I would really like to show you ways we will add this easy technique to the RecyclerView.

First, I’ll present how we will create a easy software with the RecyclerView and after it we’ll implement setEmptyView technique for our RecyclerView. In this article, I’ll present you the simplified code.

Easy RecyclerView

Let us start with dependencies. We should always add required dependencies to construct.gradle file.

apply plugin: ‘’

compileSdkVersion 25
buildToolsVersion “25.0.2”


compile ‘’

Subsequent step is creating format for an activity for an Exercise or a Fragment.

And format for RecyclerView merchandise

We also should implement a easy mannequin class for Notes

public class Notice
personal String mTitle;

public Word(String title)
this.mTitle = title;

public String getTitle()
return mTitle;

public boolean equals(Object o)
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Notice observe = (Word) o;

return mTitle != null ? mTitle.equals(word.mTitle) : notice.mTitle == null;

public int hashCode()
return mTitle != null ? mTitle.hashCode() : 0;

public String toString()
return “Note” +
“title=’” + mTitle + ”’ +

And we should always implement an adapter.

public class NotesAdapter extends RecyclerView.Adapter
personal Listing mNotes;

public void setNotes(Record notes)
this.mNotes = notes;

public NoteViewHolder onCreateViewHolder(ViewGroup mum or dad, int viewType)
View itemView = LayoutInflater.from(mum or dad.getContext())
.inflate(R.format.item_note, mum or dad, false);

return new NoteViewHolder(itemView);

public void onBindViewHolder(NoteViewHolder holder, int position)

public int getItemCount()
return mNotes == null ? 0 : mNotes.measurement();

Our Adapter additionally required ViewHolder.

class NoteViewHolder extends RecyclerView.ViewHolder

protected TextView mTitleTextView;

NoteViewHolder(View itemView)
mTitleTextView = (TextView) itemView.findViewById(;

And eventually, we should always add an activity.

public class MainActivity extends AppCompatActivity
personal remaining static int DELAY = 3000;

personal Handler mHandler;
personal NotesAdapter mAdapter;

personal Runnable updateAdapterRunnable = new Runnable()
public void run()


protected void onCreate(Bundle savedInstanceState)

mAdapter = new NotesAdapter();

RecyclerView recyclerView = (RecyclerView) findViewById(;
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());

mHandler = new Handler();

protected void onResume()
mHandler.postDelayed(updateAdapterRunnable, DELAY);

protected void onPause()

personal static Listing getTmpNotes()
Listing listing = new ArrayList<>();
record.add(new Notice(“1st note”));
record.add(new Word(“2nd note”));
record.add(new Notice(“3rd note”));
record.add(new Observe(“4th note”));
record.add(new Notice(“5th note”));
listing.add(new Notice(“6th note”));
record.add(new Notice(“7th note”));
record.add(new Observe(“8th note”));
record.add(new Observe(“9th note”));
listing.add(new Notice(“10th note”));
record.add(new Observe(“11th note”));
listing.add(new Word(“12th note”));
listing.add(new Notice(“13th note”));
record.add(new Notice(“15th note”));
return record;

So, we have now

RecyclerView with a setEmptyView technique

The primary concept right here is to display a view when listing is empty. To start with, we should always create a custom RecyclerView. I would really like to inform you few phrases about AdapterDataObserver.

The AdapterDataObserver class include few strategies, like onChanged, onItemRangeChanged, onItemRangeChanged, onItemRangeInserted, onItemRangeMoved, onItemRangeRemoved.

onItemRangeChanged(int positionStart, int itemCount, Object payload)
onItemRangeChanged(int positionStart, int itemCount)
onItemRangeInserted(int positionStart, int itemCount)
onItemRangeMoved(int fromPosition, int toPosition, int itemCount)
onItemRangeRemoved(int positionStart, int itemCount)

These methods can notify your observer about any modifications.

After it might create a customized RecyclerView.

public class EmptyRecyclerView extends RecyclerView
personal View mEmptyView;

public EmptyRecyclerView(Context context)

public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs)
tremendous(context, attrs);

public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);

personal void initEmptyView()
if (mEmptyView != null)
getAdapter() == null || getAdapter().getItemCount() == zero ? VISIBLE : GONE);
getAdapter() == null || getAdapter().getItemCount() == 0 ? GONE : VISIBLE);

last AdapterDataObserver observer = new AdapterDataObserver()
public void onChanged()

public void onItemRangeInserted(int positionStart, int itemCount)
super.onItemRangeInserted(positionStart, itemCount);

public void onItemRangeRemoved(int positionStart, int itemCount)
super.onItemRangeRemoved(positionStart, itemCount);


public void setAdapter(Adapter adapter)
Adapter oldAdapter = getAdapter();

if (oldAdapter != null)

if (adapter != null)

public void setEmptyView(View view)
this.mEmptyView = view;

We also needs to replace the format for an activity or a fragment.

Right here we now have new view (empty_view) and we changed to our new EmptyRecyclerView (com.alexzh.recyclerviewsetemptyview.EmptyRecyclerView).

We additionally ought to update the activity.

protected void onCreate(Bundle savedInstanceState)

EmptyRecyclerView recyclerView = (EmptyRecyclerView) findViewById(;


We will additionally update Adapter and change public class NotesAdapter extends RecyclerView.Adapter to public class NotesAdapter extends EmptyRecyclerView.Adapter.

Right now, we’ve got better consumer experience.

Finally, I would really like to present a gist with simplified used code in this tutorial.

If this article was helpful to you, please share it. You can even subscribe to me on Twitter and get extra useful info related to Android improvement.