March 6, 2021
					    							
												How to show empty View in Activity Recyclerview?
package com.zakasoft.cashreceipt;
import android.content.Context;
        import androidx.recyclerview.widget.RecyclerView;
        import android.util.AttributeSet;
        import android.view.View;
import androidx.recyclerview.widget.RecyclerView;
public class EmptyRecyclerView extends RecyclerView {
    private View emptyView;
    final private RecyclerView.AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkIfEmpty();
        }
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkIfEmpty();
        }
        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkIfEmpty();
        }
    };
    public EmptyRecyclerView(Context context) {
        super(context);
    }
    public EmptyRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public EmptyRecyclerView(Context context, AttributeSet attrs,
                             int defStyle) {
        super(context, attrs, defStyle);
    }
    void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            final boolean emptyViewVisible =
                    getAdapter().getItemCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }
    @Override
    public void setAdapter(Adapter adapter) {
        final Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            oldAdapter.unregisterAdapterDataObserver(observer);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(observer);
        }
        checkIfEmpty();
    }
    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
        checkIfEmpty();
    }
}
EmptyRecyclerView recyclerView; EmptyRecyclerView.Adapter adapter;
recyclerView = (EmptyRecyclerView)findViewById(R.id.todo_list_recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); // Fetch the empty view from the layout and set it on // the new recycler view View emptyView = findViewById(R.id.todo_list_empty_view); recyclerView.setEmptyView(emptyView);
<!-- Replaced android.support.v7.widget.RecyclerView with the
new EmptyRecyclerView -->
   <com.zakasoft.cashreceipt.EmptyRecyclerView
       android:id="@+id/todo_list_recycler_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />
   <!-- Added an empty view which will be shown when the EmptyRecyclerView
        is empty -->
   <LinearLayout
       android:id="@+id/todo_list_empty_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:textAlignment="center">
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:padding="16dp"
           android:text="@string/no_data_available"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:padding="16dp"
           android:text="ADD NEW"/>
   </LinearLayout>
																						
								
								
													

 
																							