Custom RecyclerView with EmptyView support

Hey folks, as you might know that RecyclerView does not support EmptyView but we can implement it using Custom RecylerView and in this post i am going to show you exactly that.

EmptyView is  any view which is shown when there is no data in the list. It is a great way to improve user experience as when the list is empty, the user will get to know about what’s happening.

Boilerplates:-

Add dependency:-

implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'

Create RecyclerView Adapter:-

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private Context context;
    private ArrayList<String> list;

    public MyAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        AppCompatTextView appCompatTextView = new AppCompatTextView(context);
        appCompatTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        appCompatTextView.setPadding(5,5,5,5);
        appCompatTextView.setTextSize(25);
        appCompatTextView.setGravity(Gravity.CENTER);
        return new ViewHolder(appCompatTextView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTitle.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final AppCompatTextView mTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            mTitle = (AppCompatTextView) itemView;

        }
    }
}

Create a EmptyView which will be shown when the list is empty

<!--emptyview-->
<android.support.v7.widget.LinearLayoutCompat
    android:id="@+id/emptyview_llc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:visibility="gone"
    android:orientation="vertical">
    <!--image for emptyview-->
    <android.support.v7.widget.AppCompatImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:src="@drawable/sadface" />
    <!--text for emptyview-->
    <android.support.v7.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="NO DATA FOUND !"
        android:textColor="@android:color/black"
        android:textSize="20sp" />
</android.support.v7.widget.LinearLayoutCompat>

In our main activity add two FloatingActionButton, one for adding items in the list and another for clearing all list items and in case you are new to android and dont know about FloatingActionButton -> click here

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:layout_margin="10dp"
    android:id="@+id/clearall_fab"
    android:src="@drawable/ic_clear_all_black_24dp"
    android:tint="@android:color/white"/>
<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/additem_fab"
    android:layout_gravity="left|bottom"
    android:layout_margin="10dp"
    android:tint="@android:color/white"
    android:src="@drawable/ic_add_black_24dp"/>

RecyclerViewWithEmptyView  :-All you need to do is create a custom recyclerview which extends RecyclerView and add observer to adapter. Observer contains the methods which are called whenever the adapters data is changed.

public class RecyclerViewWithEmptyView extends RecyclerView {
    private View emptyView;

    public RecyclerViewWithEmptyView(Context context) {
        super(context);
    }

    public RecyclerViewWithEmptyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RecyclerViewWithEmptyView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }

    @Override
    public void setAdapter(Adapter adapter) {
        Adapter previousadapter = getAdapter();
        if (previousadapter != null) {
            previousadapter.unregisterAdapterDataObserver(emptyObserver);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(emptyObserver);
        }
        emptyObserver.onChanged();
    }

    private AdapterDataObserver emptyObserver = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            shouldSetEmptyView();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            shouldSetEmptyView();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            super.onItemRangeRemoved(positionStart, itemCount);
            shouldSetEmptyView();
        }
    };

    public void shouldSetEmptyView() {
        Adapter<?> adapter = getAdapter();
        if (adapter != null && emptyView != null) {
            if (adapter.getItemCount() == 0) {
                emptyView.setVisibility(View.VISIBLE);
                RecyclerViewWithEmptyView.this.setVisibility(View.GONE);
            } else {
                emptyView.setVisibility(View.GONE);
                RecyclerViewWithEmptyView.this.setVisibility(View.VISIBLE);
            }
        }
    }

}

Now add this RecyclerViewWithEmptyView in our mainactivity, fInal mainactivity.xml:-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@android:color/white"
    tools:context="com.example.shivam.emptyviewsupportedrecyclerview.MainActivity">

    <com.example.shivam.emptyviewsupportedrecyclerview.RecyclerViewWithEmptyView
        android:id="@+id/myrecyclerview_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!--emptyview-->
    <android.support.v7.widget.LinearLayoutCompat
        android:id="@+id/emptyview_llc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        android:orientation="vertical">
        <!--image for emptyview-->
        <android.support.v7.widget.AppCompatImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:src="@drawable/sadface" />
        <!--text for emptyview-->
        <android.support.v7.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="NO DATA FOUND !"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    </android.support.v7.widget.LinearLayoutCompat>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_margin="10dp"
        android:id="@+id/clearall_fab"
        android:src="@drawable/ic_clear_all_black_24dp"
        android:tint="@android:color/white"/>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/additem_fab"
        android:layout_gravity="left|bottom"
        android:layout_margin="10dp"
        android:tint="@android:color/white"
        android:src="@drawable/ic_add_black_24dp"/>
</FrameLayout>

Our final MainActivity.java:-

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayoutCompat emptyview_llc;
    private RecyclerViewWithEmptyView myrecyclerview_rv;
    private FloatingActionButton clearall_fab;
    private FloatingActionButton addItem_fab;
    private ArrayList<String> list;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initialize
        myrecyclerview_rv=(RecyclerViewWithEmptyView)findViewById(R.id.myrecyclerview_rv);
        emptyview_llc=(LinearLayoutCompat) findViewById(R.id.emptyview_llc);
        clearall_fab=(FloatingActionButton)findViewById(R.id.clearall_fab);
        addItem_fab=(FloatingActionButton)findViewById(R.id.additem_fab);
        clearall_fab.setOnClickListener(this);
        addItem_fab.setOnClickListener(this);

        //set emptyview for our custom recyclerview
        myrecyclerview_rv.setEmptyView(emptyview_llc);

        //set custom RecyclerView Adapter
        list=new ArrayList<String>();
        myrecyclerview_rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        myAdapter=new MyAdapter(MainActivity.this,list);
        myrecyclerview_rv.setAdapter(myAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.clearall_fab:
                list.clear();
                myAdapter.notifyDataSetChanged();
                break;
            case R.id.additem_fab:
                list.add("Item"+list.size());
                myAdapter.notifyItemInserted(list.size());
                break;
        }
    }
}

and thats all.

THANK YOU.


Posted

in

by

Recent Post

  • How to Implement In-Order, Pre-Order, and Post-Order Tree Traversal in Python?

    Tree traversal is an essential operation in many tree-based data structures. In binary trees, the most common traversal methods are in-order traversal, pre-order traversal, and post-order traversal. Understanding these tree traversal techniques is crucial for tasks such as tree searching, tree printing, and more complex operations like tree serialization. In this detailed guide, we will […]

  • Mastering Merge Sort: A Comprehensive Guide to Efficient Sorting

    Are you eager to enhance your coding skills by mastering one of the most efficient sorting algorithms? If so, delve into the world of merge sort in Python. Known for its powerful divide-and-conquer strategy, merge sort is indispensable for efficiently handling large datasets with precision. In this detailed guide, we’ll walk you through the complete […]

  • Optimizing Chatbot Performance: KPIs to Track Chatbot Accuracy

    In today’s digital age, chatbots have become integral to customer service, sales, and user engagement strategies. They offer quick responses, round-the-clock availability, and the ability to handle multiple users simultaneously. However, the effectiveness of a chatbot hinges on its accuracy and conversational abilities. Therefore, it is necessary to ensure your chatbot performs optimally, tracking and […]

  • Reinforcement Learning: From Q-Learning to Deep Q-Networks

    In the ever-evolving field of artificial intelligence (AI), Reinforcement Learning (RL) stands as a pioneering technique enabling agents (entities or software algorithms) to learn from interactions with an environment. Unlike traditional machine learning methods reliant on labeled datasets, RL focuses on an agent’s ability to make decisions through trial and error, aiming to optimize its […]

  • Understanding AI Predictions with LIME and SHAP- Explainable AI Techniques

    As artificial intelligence (AI) systems become increasingly complex and pervasive in decision-making processes, the need for explainability and interpretability in AI models has grown significantly. This blog provides a comprehensive review of two prominent techniques for explainable AI: Local Interpretable Model-agnostic Explanations (LIME) and Shapley Additive Explanations (SHAP). These techniques enhance transparency and accountability by […]

  • Building and Deploying a Custom Machine Learning Model: A Comprehensive Guide

    Machine Learning models are algorithms or computational models that act as powerful tools. Simply put, a Machine Learning model is used to automate repetitive tasks, identify patterns, and derive actionable insights from large datasets. Due to these hyper-advanced capabilities of Machine Learning models, it has been widely adopted by industries such as finance and healthcare.  […]

Click to Copy