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

  • 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 […]

  • Federated vs Centralized Learning: The Battle for Privacy, Efficiency, and Scalability in AI

    The ever-expanding field of Artificial Intelligence (AI) and Machine Learning (ML) relies heavily on data to train models. Traditionally, this data is centralized, aggregated, and processed in one location. However, with the emergence of privacy concerns, the need for decentralized systems has grown significantly. This is where Federated Learning (FL) steps in as a compelling […]

  • Federated Learning’s Growing Role in Natural Language Processing (NLP)

    Federated learning is gaining traction in one of the most exciting areas: Natural Language Processing (NLP). Predictive text models on your phone and virtual assistants like Google Assistant and Siri constantly learn from how you interact with them. Traditionally, your interactions (i.e., your text messages or voice commands) would need to be sent back to […]

  • What is Knowledge Distillation? Simplifying Complex Models for Faster Inference

    As AI models grow increasingly complex, deploying them in real-time applications becomes challenging due to their computational demands. Knowledge Distillation (KD) offers a solution by transferring knowledge from a large, complex model (the “teacher”) to a smaller, more efficient model (the “student”). This technique allows for significant reductions in model size and computational load without […]

Click to Copy