1

This is my app where is the problem exists

I want my items to fit like this app

I'm using a RecyclerView with GridLayoutManager. My problem as you see in the image that the grid items aren't the same height when one of the items has a longer text. I'm loading the data from firebase and the notify the Adapter when the list has the data. I tried everything but can't find a solution to auto fit the items like the second image attached. Any help, please?

Here is my recyclerview item layout

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    >



    <android.support.v7.widget.CardView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:cardCornerRadius="10dp"
        android:layout_margin="5dp"
        app:cardBackgroundColor="#fff"
        app:cardElevation="5dp"
        >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/cartoonImg"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:scaleType="fitXY"
                app:imgUrl="@{cartoon.thumb}"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                tool:text="Cartoon Name"
                android:gravity="center"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginRight="2dp"
                android:layout_marginLeft="2dp"
                android:textColor="@color/colorPrimary"
                android:text="@{cartoon.title}" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

</FrameLayout>
Mangesh Pawar
  • 161
  • 13
Ahmed Abdeen
  • 93
  • 1
  • 10

4 Answers4

1

This will result into exactly what you need

Try replacing your TextView with this:

           <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                tool:text="Cartoon Name"
                android:gravity="center"
                android:minLines="2"
                android:maxLines="2"
                android:ellipsize="end"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginRight="2dp"
                android:layout_marginLeft="2dp"
                android:textColor="@color/colorPrimary"
                android:text="@{cartoon.title}" />
Nikunj Peerbits
  • 735
  • 3
  • 12
Deep Patel
  • 2,258
  • 2
  • 12
  • 26
0

you can give a static height to your textview, this way it will be the same for all. But you may lose your text in this.

Ritviz Sharma
  • 322
  • 1
  • 9
0

You can add to your TextView:

android:ellipsize="start" android:maxLines="1

From API 26, you can use Autosizing TextView - check the documentation here: https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

sale0303
  • 31
  • 1
0

You are using the line

android:layout_height="wrap_content"

on your CardView, and since the descendants have match_parent, the size changes for your TextView.

To solve this, you can use the following property android:maxLines="1" inside your TextView. This will stop the TextView from jumping to the next line, but will crop the text.

Alternatively, you can use a static height instead of match_parent or wrap_content, that is, using a value. I strongly recommend using 48dp since that is what Material Design Guidelines recommend. Source:

S. Czop
  • 414
  • 5
  • 16