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

  • AI Chatbots for Sales Team Automation: The Critical Role of AI Sales Assistants in Automating Your Sales Team

    Sales teams are the heart of any successful business, but managing them effectively can often feel like trying to juggle flaming swords. The constant pressure to generate leads, maintain relationships, and close deals leaves your team overwhelmed, spending more time on administrative tasks than actual selling. Here’s where AI-powered sales assistants step in to completely […]

  • Transforming HR with AI Assistants: The Comprehensive Guide

    The role of Human Resources (HR) is critical for the smooth functioning of any organization, from handling administrative tasks to shaping workplace culture and driving strategic decisions. However, traditional methods often fall short of meeting the demands of a modern, dynamic workforce. This is where our Human Resource AI assistants enter —a game-changing tool that […]

  • How Conversational AI Chatbots Improve Conversion Rates in E-Commerce?

    The digital shopping experience has evolved, with Conversational AI Chatbots revolutionizing customer interactions in e-commerce. These AI-powered systems offer personalized, real-time communication with customers, streamlining the buying process and increasing conversion rates. But how do Conversational AI Chatbots improve e-commerce conversion rates, and what are the real benefits for customers? In this blog, we’ll break […]

  • 12 Essential SaaS Metrics to Track Business Growth

    In the dynamic landscape of Software as a Service (SaaS), the ability to leverage data effectively is paramount for long-term success. As SaaS businesses grow, tracking the right SaaS metrics becomes essential for understanding performance, optimizing strategies, and fostering sustainable growth. This comprehensive guide explores 12 essential SaaS metrics that every SaaS business should track […]

  • Bagging vs Boosting: Understanding the Key Differences in Ensemble Learning

    In modern machine learning, achieving accurate predictions is critical for various applications. Two powerful ensemble learning techniques that help enhance model performance are Bagging and Boosting. These methods aim to combine multiple weak learners to build a stronger, more accurate model. However, they differ significantly in their approaches. In this comprehensive guide, we will dive […]

  • What Is Synthetic Data? Benefits, Techniques & Applications in AI & ML

    In today’s data-driven era, information is the cornerstone of technological advancement and business innovation. However, real-world data often presents challenges—such as scarcity, sensitivity, and high costs—especially when it comes to specific or restricted datasets. Synthetic data offers a transformative solution, providing businesses and researchers with a way to generate realistic and usable data without the […]

Click to Copy