Zelig
Zelig

Reputation: 1808

Correctly sizing RecyclerView's children

I have a RecyclerView declared this way :

<?xml version="1.0" encoding="utf-8"?>
    <mypackage.myRecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyRecyclerViewId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp">
</mypackage.myRecyclerView>

Children are built this way :

<TextView
    android:id="@+id/Date"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:paddingStart="5dp"
    android:paddingEnd="5dp"
    android:gravity="center_vertical"
    android:background="@drawable/myBorder"/>

<TextView
    android:id="@+id/Comment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="5dp"
    android:paddingEnd="5dp"
    android:background="@drawable/myBorder"/>

I'm expecting the second field to fill all the remaining space left in my RecyclerView, thanks to it's match_parent android:layout_width property, but it's not the case!

If I add a background color to my RecyclerView, I see it correctly reach my screen's right hand end, but my second TextView curiously ends approximately in it's middle.

Any idea to make my second TextView use all the available space?

The LayoutManager I use is a 2 columned GridLayoutManager.

Here's the screenshot of why I get :

enter image description here1

The grey area at the right shows how large the RecyclerView is. As you can see, the second TextView stops too soon, leaving a lot of empty space unused. What I want to do is to have this TextView take benefit from all this empty space.

Upvotes: 0

Views: 41

Answers (1)

Thogaruchesti Hemanth
Thogaruchesti Hemanth

Reputation: 96

Use the layout_weight property so it takes all the empty space like this

<TextView
    android:id="@+id/Comment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="5dp"
    android:layout_weight="1"
    android:paddingEnd="5dp"
    android:background="@drawable/myBorder"/>

Upvotes: 1

Related Questions