Home > Software engineering >  How to make recycler view always visible even empty - Android
How to make recycler view always visible even empty - Android

Time:07-23

I have a recyclerView. Its width match_parent / height wrap_content. I can not make it both match_parent since it takes only portion of the screen. So I need the area of that recycler been always visible even there are no items in it. Right now I have a background in it. And its visible only when at least 1 item in recycler. If recycler is empty the whole view is gone.

So is there a way to leave recycler with 0 items in it but at the same time the space that it holds be visible on the screen?

CodePudding user response:

Have you tried setting constant height to your RV? It should solve you

  <android.support.v7.widget.RecyclerView
        android:id="@ id/rv"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>

If you need it to be scalable based on the screen size you can use ConstraintLayout to set a percentage of screen for its height:

 <android.support.v7.widget.RecyclerView
        android:id="@ id/rv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.5"
/> 

see here for more examples: https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#match_constraint-dimensions-added-in-1.1

CodePudding user response:

So the solution here is to set minimumHeight to some value. In this case the width will always be match_parent and you will have some invisible area that you can manipulate as you want

  • Related