1

I want to max height to recycler view but doesn't work for me. I found some answers but they didn't work for me.
I tried many ways to fix that. but I couldn't found the answer. this is XML code. Anyone can help me to solve that problem

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewItems"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:maxHeight="100dp"
                app:layout_constrainedHeight="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintHeight_max="100dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Nimantha
  • 165
  • 3
  • 11
  • Possible duplicate of [How to set RecyclerView Max Height](https://stackoverflow.com/questions/42694355/how-to-set-recyclerview-max-height) – user1506104 Mar 08 '19 at 07:25
  • @user1506104 i know this question already asked in here. But theirs solutions didn't work for me. Thats why again posted this question. Sorry for that – Nimantha Mar 10 '19 at 13:42

2 Answers2

0

Paste this code on your xml activity file. Hope this will work.

 <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewItems"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:maxHeight="100dp"
                app:layout_constrainedHeight="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintHeight_max="100dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
Aovin Mahmud
  • 204
  • 3
  • 18
0

Your question is not so clear to me but if you want the recyclerView to take all of the screen space you should use this :

  <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewItems"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

If you want to limit your recyclerview height I would not recommend using fixed sized in dp - use guideline/barrier to make your recyclerView take only some percent of the screen, for example if you want your view only take 30% of your screen use guidelines with 30% different and constrain your view to them like this:

   <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewItems"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

</androidx.constraintlayout.widget.ConstraintLayout>
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • i want to add recycler view inside dialog. i used recycler view height as wrap content . when lot of items have dialog height so big. when recycler view height goes more than 400dp (as a example) it should be stopped (i mean height) . then other item to view scroll down. But i used max width in recycler view it didn't work. Also i created a constraint layout and add recycler view inside it and set height to 0dp . it didn't worked – Nimantha Mar 10 '19 at 13:24
  • This is not a good practice(the fixed sizes), what is wrong with my answer? why not use constraint layout with guidelines like the example I gave? – Tamir Abutbul Mar 10 '19 at 13:26
  • didn't work bro. ( I used recycler view in dialog) it could be the reason this method didn't work.... – Nimantha Mar 10 '19 at 13:54
  • Ok in that case creat custom dialog with custom layout and in your layout try to do as i sujested in my answer with guidlines.This should work – Tamir Abutbul Mar 10 '19 at 14:01