RecyclerView layout_width Not Set When Using wrap_content

Nathan Sokalski 4,111 Reputation points
2021-11-16T17:47:44.497+00:00

I have an xml layout which contains a GridLayout which contains a TextView, RecyclerView, and Button. The RecyclerView is defined as follows:

<androidx.recyclerview.widget.RecyclerView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_column="0" android:layout_row="1" android:layout_rowWeight="1" android:layout_gravity="center_horizontal"/>

In activity_main.xml it is included using the following:

<include android:layout_width="match_parent" android:layout_height="0dp" android:layout_column="0" android:layout_row="1" android:layout_rowWeight="1" layout="@layout/editcoursesscreenlayout"/>

Everything appears as desired in the xml layout designer, but in activity_main.xml the height weight gives it a height larger than the screen (therefore pushing the Button offscreen, and when debugging the app I do not see the RecyclerView at all. If I set the layout_width of RecyclerView to a fixed width or match_parent, it is visible (although obviously not the width I want), but the height is still larger than it should be (it is not scrollable because it is not resized, and the Button is pushed offscreen). Why is wrap_content setting the width to 0, and why is the row weight not working correctly? (NOTE: The parent of the include tag is a GridLayout)

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,966 Reputation points
    2021-11-18T06:48:12.38+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    but the RecyclerView is cropped (rather than resized), therefore making it not scrollable. I tried setting the RecyclerView's layout_height to a fixed value, but it was still not scrollable

    I add more items to the recyclerView and reproduce the problem. This is because the recyclerView's height is larger than the include layout has. To fix this issue, try using PercentRelativeLayout instead. We need to install the Xamarin.AndroidX.PercentLayout package to use the layout.

    Here is the sample code, you could refer to it.

       <androidx.percentlayout.widget.PercentRelativeLayout   
           xmlns:android="http://schemas.android.com/apk/res/android"  
           xmlns:app="http://schemas.android.com/apk/res-auto"  
           android:layout_width="match_parent"  
           android:layout_height="match_parent">  
         
           <TextView  
               android:id="@+id/textView1"  
               android:text="the first column"  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"/>  
         
           <androidx.recyclerview.widget.RecyclerView  
               android:id="@+id/recyclerView1"  
               android:layout_below="@id/textView1"  
               android:layout_width="0dp"  
               android:layout_height="0dp"  
               app:layout_heightPercent="85%"  
               app:layout_widthPercent="100%"/>  
         
           <Button  
               android:id="@+id/btn"  
               android:layout_below="@id/recyclerView1"  
               android:text="button"  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"/>  
         
       </androidx.percentlayout.widget.PercentRelativeLayout>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.