How to Show an Empty View with a RecyclerView in Android

In this android programming source code example, we are going to explore how to show an empty view with RecyclerView in android.

In case the data source attached to a RecyclerView does not return any value, it is better to display a simple message to users notifying them about lack of data.

You can hide a RecyclerView without a data or you can use the ViewHolder Type to decide what to display when there is data and no data.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"/>

    <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data is empty"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:visibility="gone"/>

</RelativeLayout>

MainActivity.java

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView list;
    private adapter recyclerAdapter;
    private TextView emptyData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view17);
        list = (RecyclerView) findViewById(R.id.list);
        emptyData = (TextView) findViewById(R.id.empty);

        //Data
        ArrayList<String> country = new ArrayList<>();

        if(country.isEmpty()){
            list.setVisibility(View.GONE);
            emptyData.setVisibility(View.VISIBLE);
        }else{
            emptyData.setVisibility(View.GONE);
            list.setVisibility(View.VISIBLE);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        list.setLayoutManager(layoutManager);
        recyclerAdapter = new adapter(RecyclerViewActivity17.this, country);
        list.addItemDecoration(new DividerItemDecoration(list.getContext(), layoutManager.getOrientation()));
        list.setAdapter(recyclerAdapter);
    }

    private class adapter extends RecyclerView.Adapter<adapter.myViewHolder> {
        Context context;
        List<String> mData;

        public adapter(Context context, List<String> data) {
            this.context = context;
            this.mData = data;
        }

        @Override
        public adapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter1, parent, false);
            return new myViewHolder(view);
        }

        @Override
        public void onBindViewHolder(adapter.myViewHolder holder, int position) {
            holder.country.setText(mData.get(position));
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public class myViewHolder extends RecyclerView.ViewHolder {
            TextView country;

            public myViewHolder(View itemView) {
                super(itemView);
                country = (TextView) itemView.findViewById(R.id.country);
            }
        }
    }
}

 

android empty recyclerview

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment