2

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
  • 309
  • 1
  • 14
Ahmed Abdeen
  • 325
  • 5
  • 14

4 Answers4

0

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
  • 785
  • 4
  • 12
Deep Patel
  • 2,584
  • 2
  • 15
  • 28
  • It will show always that space even if there is no title that needs these 2 lines, I guess he wants to be dynamic, if only one item is two lines make all 2 lines otherwise make it 1 line and normal height – Skizo-ozᴉʞS ツ Jan 03 '22 at 12:53
-1

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
  • 306
  • 1
  • 9
-1

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
  • 602
  • 1
  • 6
  • 17
-2

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
  • 27
  • 1