How to Make Wrap_Content Work on a Recyclerview

How do I make WRAP_CONTENT work on a RecyclerView

From Android Support Library 23.2.1 update, all WRAP_CONTENT should work correctly.

Please update version of a library in gradle file OR to further :

compile 'com.android.support:recyclerview-v7:23.2.1'

solved some issue like Fixed bugs related to various measure-spec methods

Check http://developer.android.com/tools/support-library/features.html#v7-recyclerview

you can check Support Library revision history

set recyclerView item height wrap_content

Set adjustViewBounds to true in Image view that you are using in row.xml like this:

<ImageView
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/img"/>

By adding adjustViewBounds and leaving the layout_height as wrap_content we are keeping the aspect ratio of the image while keeping the width to match parent but letting the height change accordingly, so we are still able to control the size of the image (kind of) and maintaining the aspect ratio at the same time.

You can also set staggered grid layout manager in recyler view to have different item size.

recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, 1)); //First param to set span count and second for orientation

Android wrap_content not working on recyclerview

The distance of the recyclerview used in ConstraintLayout as a view is disabled because it does not fit the logic of ConstraintLayout. So edit the height value of recyclerview as follows:

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp" />

height = 0dp

make RecyclerView's height to wrap_content in Constraint layout

i had the same issue and i find this solution:
you should add this attribute to your recyclerview and it makes your recyclerview wrap_content in the constraint_layout:

app:layout_constraintHeight_default="wrap"

let me know if this solution fixed your problem.

EDIT : recycler's height should be 0dp.

EDIT 2 : in the newer versions of support library, use this code:

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"

RecyclerView max_height with wrap_content

I found the solution. This is the XML now:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="wrap_content"
android:layout_height="wrap_content"

tools:context=".histDialog">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/colorPrimary"
android:clipToPadding="true"
android:padding="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="500dp"
app:layout_constraintHeight_min="50dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The important part is:

        android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="500dp"
app:layout_constraintHeight_min="50dp"

for the RecyclerView. Took me long enough to realise it was this simple. Hope this can help others too.

RecyclerView is not taking wrap_content height, it becomes scrollable

I managed to resolve this issue by putting my recyclerview in a nestedscrollview and kept 'android:nestedScrollingEnabled' disabled/false.

RecyclerView wrap_content

It is related on how the LayoutManager calculates the sizes. Here you have the related bug with some workarounds people have used.

NOTE: With the release 23.2 of the support library now the wrap content is supported, so looks like they fixed it. You can checkout the changelog here



Related Topics



Leave a reply



Submit