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

  • Building Intelligent AI Models For Enterprise Success: Insider Strategies 

    Just picture a world where machines think and learn like us. It might sound like a scene straight out of a sci-fi movie, right? Well, guess what? We are already living in that world now. Today, data, clever algorithms, and AI models are changing the way businesses operate. AI models are serving as a brilliant […]

  • Introducing Google Vids in Workspace: Your Ultimate AI-Powered Video Creation Tool

    Hey there, fellow content creators and marketing gurus! Are you tired of drowning in a sea of emails, images, and marketing copy, struggling to turn them into eye-catching video presentations? Fear not, because Google has just unveiled its latest innovation at the Cloud Next conference in Las Vegas: Google Vids- Google’s AI Video Creation tool! […]

  • Achieve High ROI With Expert Enterprise Application Development

    Nowadays modern-day enterprises encounter no. of challenges such as communication breakdown, inefficient business processes, data fragmentation, data security risks, legacy system integration with modern applications, supply chain management issues, lack of data analytics and business intelligence, inefficient customer relationship management, and many more. Ignoring such problems within an organization can adversely impact various aspects of […]

  • State Management with Currying in React.js

    Dive into React.js state management made easy with currying. Say goodbye to repetitive code and hello to streamlined development. Explore the simplicity and efficiency of currying for your React components today!

  • How Much Does It Cost to Develop an App in 2024?

    The price of bringing your app to life typically ranges from $20,000 to $200,000. This cost varies based on factors like which platform you choose, the complexity of features you want, and the size of your intended audience. However, costs can climb even higher for more complex projects, reaching up to $350,000.

  • Mastering Software Testing Strategies: Your Guide

    Implementing best software testing strategies is a crucial part of software development, ensuring that digital products meet industry standards. Defined by the International Software Testing Qualification Board, it encompasses a range of activities, both static and dynamic, throughout the software’s lifecycle. As an essential component of the Software Development Life Cycle (SDLC), the Software Testing […]