Azda Firmansyah Azda Firmansyah - 5 months ago 25
Android Question

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

Answer

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.

Comments