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

  • Generative AI in corporate accounting: Integration, use cases, challenges, ROI evaluation, and future outlook

    Overview Corporate accounting is fundamental to ensuring an organization’s financial stability and strategic growth. As the cornerstone of financial reporting and decision-making, it upholds transparency and accountability in business operations. However, technological advancements, particularly the emergence of generative AI, are redefining the field. By automating repetitive tasks and amplifying data-driven insights, generative AI in corporate […]

  • Generative AI in HR Operations: Overview, Use Cases, Challenges, and Future Trends

    Overview Imagine a workplace where HR tasks aren’t bogged down by endless paperwork or repetitive chores, but instead powered by intelligent systems that think, create, and adapt—welcome to the world of GenAI. Generative AI in HR operations offers a perfect blend of efficiency, personalization, and strategic insight that transforms how organizations interact with their talent. […]

  • Generative AI in Sales: Implementation Approaches, Use Cases, Challenges, Best Practices, and Future Trends

    The world of sales is evolving at lightning speed. Today’s sales teams are not just tasked with meeting ambitious quotas but must also navigate a maze of complex buyer journeys and ever-rising customer expectations. Despite relying on advanced CRM systems and various sales tools, many teams remain bogged down by repetitive administrative tasks, a lack […]

  • Generative AI in Due Diligence: Integration Approaches, Use Cases, Challenges, and Future Outlook

    Generative AI is revolutionizing the due diligence landscape, setting unprecedented benchmarks in data analysis, risk management, and operational efficiency. By combining advanced data processing capabilities with human-like contextual understanding, this cutting-edge technology is reshaping traditional due diligence processes, making them more efficient, accurate, and insightful. This comprehensive guide explores the integration strategies, practical applications, challenges, […]

  • Exploring the Role of AI in Sustainable Development Goals (SDGs)

    Artificial Intelligence (AI) is revolutionizing how we address some of the world’s most pressing challenges. As we strive to meet the United Nations’ Sustainable Development Goals (SDGs) by 2030, AI emerges as a powerful tool to accelerate progress across various domains. AI’s potential to contribute to sustainable development is vast from eradicating poverty to combating […]

  • Future Trends in AI Chatbots: What to Expect in the Next Decade

    Artificial Intelligence (AI) chatbots have become indispensable across industries. The absolute conversational capabilities of AI chatbots are enhancing customer engagement, streamlining operations, and transforming how businesses interact with users. As technology evolves, the future of AI chatbots holds revolutionary advancements that will redefine their capabilities. So, let’s start with exploring the AI chatbot trends: Future […]

Click to Copy