Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle empty data in recycler view?

<android.support.v7.widget.RecyclerView
    android:id="@+id/education_recycle_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>

This is my current xml layout file for recycle view.

How to handle empty data in recycler view? is it possible when put text to display "no data" in same layout?

Thanks

like image 702
Azda Firmansyah Avatar asked Jan 05 '23 22:01

Azda Firmansyah


1 Answers

This is how I do it:

In XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/tv_no_data"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/empty_text" 
    android:textAppearance="?android:textAppearanceMedium"
    android:visibility="invisible" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/education_recycle_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    app:layoutManager="LinearLayoutManager" />
</FrameLayout>

and in the Activity:

if (!data.isEmpty()) {
                        //if data is available, don't show the empty text
                        emptyText.setVisibility(View.INVISIBLE);
                        RecyclerAdapter adapter = new RecyclerAdapter(data); // pass the data to your adapter here
                        recyclerView.setAdapter(adapter);

                    } else
                        emptyText.setVisibility(View.VISIBLE);

Let me know if you need further information.

like image 78
Ishita Sinha Avatar answered Feb 06 '23 21:02

Ishita Sinha