1

I have a RecyclerView whose entries are CardViews. Each CardView contains a horizontal LinearLayout with 5 TextViews. No matter what I try, I cannot get the CardView to fill the width of the screen. The RecyclerView's width matches the parent container (the phone screen), and each CardView matches its parent's (the RecyclerView's) width. The design panel in Android Studio shows that the CardView should stretch to the edges of the screen, but it doesn't.

Here is the XML of the RecyclerView's layout: `

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="viewModel"
            type="com.example.weatherapp.home.HomeViewModel"/>
    </data>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/locations_list"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:scrollbars="vertical"
        app:listData="@{viewModel.locList}"/>


</layout>

`

And here is my XML for the layout of each item in the RecyclerView: `

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <import type="android.view.View" />

        <variable
            name="location"
            type="com.example.weatherapp.entity.Entry" />
    </data>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/cityName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{location.cityName}" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:text="@{location.stateName}"
                android:visibility="@{location.stateVisibility}" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:text="@{location.countryName}" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:text="@{location.fTemp}" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:text="@{location.weather}" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>
</layout>

`

EDIT: I colored the CardView and RecylerView to show the clear boundries. The CardView is purple and the RecyclerView its in is the green-turquoise color.

enter image description here

I have played around with the android:layout_weight settings of each TextView, setting them all to 1 and the android:layout_width of each to 0dp, as per Google's LinearLayout documentation. This did not stretch the CardView to the edges of the screen, and only served to shrink each TextView down. My goal is for the CardView to stretch to the edges and for the 5 TextViews to be spaced evenly within.

3 Answers3

0

In your recycerview layout, you should put <RecyclerView tag inside a different layout, such as FrameLayout, and set width of FrameLayout is match_parent.

And if you want "5 TextViews to be spaced evenly within.", you could repalce <LinearLayout tag in cardview to <ConstrainLayout tag. ConstraninLayout have "chain" attribute, it'll help in this case

ninhnau19
  • 695
  • 1
  • 3
  • 16
  • I gave this a shot. Placing the RecyclerView inside a FrameLayout still doesn't stretch the CardView out to the edges of the screen. – David Rudenya Nov 10 '22 at 03:09
0

for the 5 TextViews to be spaced evenly within.

You need to add weightSum attribute to you LinearLayout like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="5"
    android:orientation="horizontal">

with each TextView weight:

<TextView
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="..." />
...
...
...
...
Ankur
  • 1,268
  • 18
  • 22
  • I tried to implement this. It did make the TextViews inherit the height from the LinearLayout but they still won't stretch out for some reason. – David Rudenya Nov 10 '22 at 05:25
  • I see, so your problem #2 is solved. Now let's try problem #1. Can you pls add some background color to `RecyclerView` it self and confirm that it's not shrunk? We might be blaming `CardView` wrongly. – Ankur Nov 10 '22 at 05:49
  • Okay, I did. The image is up in the original post now. Looks like the RecyclerView is extending properly, but not the CardView. – David Rudenya Nov 10 '22 at 23:37
0

Okay, I figured it out using this post.

There are two solutions on that page: one edits the inflate function my layout inflater was using, the other wraps my CardView in a RelativeLayout. Both solutions work for me. I was using Kotlin's default inflate method in my list adapter class, but when I switched to the other version of the function, I was able to stop the CardView from attaching to the root of its parent at the wrong time. This appears to have been the problem. Thank you everyone who responded to help me!