How to show an empty view with a RecyclerView?
On the same layout where is defined the RecyclerView
, add the TextView
:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="@string/no_data_available" />
At the onCreate
or the appropriate callback you check if the dataset that feeds your RecyclerView
is empty.
If the dataset is empty, the RecyclerView
is empty too. In that case, the message appears on the screen.
If not, change its visibility:
private RecyclerView recyclerView;
private TextView emptyView;
// ...
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
emptyView = (TextView) rootView.findViewById(R.id.empty_view);
// ...
if (dataset.isEmpty()) {
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
How to create an empty view in a RecyclerView?
How to solve this problem?
I created an EmptyRecyclerView that extends the RecyclerView behaviours and added an updateEmptyView() method that verifies how many items I have in my RecyclerView and updates the view accordingly. My solution was based on this answer by maff91. And another source that helped me solve the problem is this Google Application that creates an EmptyView message like the one I needed.
My thanks to maff91 and the Google Application that has the same behaviour.
How to display textview if recyclerview empty android
Try this
for (int i=0; i<array.length(); i++ ) {
JSONObject ob = array.getJSONObject(i);
List_Data listData = new List_Data(ob.getString("comment")
, ob.getString("name"));
list_data.add(listData);
Log.i(TAG, "test2"+array.toString());
}
adapter.notifyDataSetChanged();
rvcy.setVisibility(list_data.isEmpty() ? View.GONE : View.VISIBLE)
emptyView.setVisibility(list_data.isEmpty() ? View.VISIBLE : View.GONE)
Empty View on a RecyclerView
I updated your code:
<?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:name="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment"
android:layout_width="match_parent"
tools:context="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/grocery_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/grocery_item" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="NO DATA AVAILABLE" />
</FrameLayout>
Your Fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_grocerylist, container, false);
rv = view.findViewById(R.id.grocery_list);
TextView emptyView = (TextView)view.findViewById(R.id.empty_view);
dh = new DatabaseHandler(view.getContext());
dataset = dh.getGroceries();
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) rv;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
adapter = new GroceryListRecyclerViewAdapter(dataset,mListener,this);
recyclerView.setAdapter(adapter);
ItemTouchHelper.Callback callback = new SimpleTouchHelperCallback(adapter);
ith = new ItemTouchHelper(callback);
ith.attachToRecyclerView(recyclerView);
DividerItemDecoration div = new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(div);
if (dataset.isEmpty()){
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
return view;
}
How to display a TextView when Recycler View is empty?
The conditions are inverted. When list is empty you are showing recyclerView and vice versa. Simply use a not check.
Consider below:
private fun displayList() {
val list = listOf<Shoe>()
if (list.isNotEmpty()) {
binding.recyclerViewList.visibility = View.VISIBLE
binding.tvNoRecords.visibility = View.GONE
} else {
binding.recyclerViewList.visibility = View.GONE
binding.tvNoRecords.visibility = View.VISIBLE
}
}
How to deal with recyclerview showing empty row
Filter your items before passing it to recycler view in your activity or your fragment like below
moviesList.filter{ movie ->
movie != null &&
!movie.movieName.isNullOrEmpty() &&
!movie.movieVoteCount.isNullOrEmpty() &&
!movie.movieVoteCount.isNullOrEmpty() &&
!movie.moviePoster.isNullOrEmpty()
}
How to handle empty data in recycler view?
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.
Related Topics
How to Animate Addition or Removal of Android Listview Rows
How to Display a List of Images in a Listview in Android
How to Add APKs in an Aosp Build
Android Speech Recognition Continuous Service
Run Code When Android App Is Closed/Sent to Background
Why Mediaplayer Throws Not Present Error When Creating Instance of It
Further Understanding Setretaininstance(True)
Default Interface Methods Are Only Supported Starting with Android 7.0 (Nougat)
Android Device Chooser - My Device Seems Offline
Why Do Most Fields (Class Members) in Android Tutorial Start with 'M'
How to Hide a Button Programmatically
Android: Automatically Choose Debug/Release Maps API Key
Why Do I Get Com.Google.Android.Gms.Common.Api.Apiexception: 10:
Calculate Text Size According to Width of Text Area
Android:Maximum Allowed Width & Height of Bitmap
Answer Incoming Call Using Android.Telecom and Incallservice