Android Recyclerview item Textview won't show up in some item

Viewed 359

So in a Recyclerview I created, I set the item layout with CardView. Here's the item layout:

product_list_item_layout.xml

<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/CardView"
    android:id="@+id/sampleProductCard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_margin="12dp"
    app:cardCornerRadius="12dp"
    app:cardElevation="6dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="8dp"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher_background"
            />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="Apple Pie"
                android:textColor="#6f6f6f"
                android:textSize="14sp"
                android:id="@+id/productName"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:text="Rp. "
                    android:textColor="#6f6f6f"
                    android:textSize="12sp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:layout_marginLeft="2dp"
                    android:text="Price"
                    android:textColor="#6f6f6f"
                    android:textSize="12sp"
                    android:id="@+id/productPrice"/>

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:text="Stock :"
                    android:textColor="#000"
                    android:textSize="10sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:layout_marginLeft="6dp"
                    android:text="4"
                    android:textColor="#000"
                    android:textSize="10sp"
                    android:id="@+id/productStockQuantity"
                    />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

</com.google.android.material.card.MaterialCardView>

In Android Studio, the design preview showed up like this.

And this is the recyclerview layout:

fragment_product_list.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    tools:context=".mainFragments.ProductListFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        ...

        ...


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvProductList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp"
            />

        ...

    </LinearLayout>

</layout>

In the fragment, I set it up as GridLayout. But it showed up like this.

For the detail, the textview inside product_list_item.xml with android:id="@+id/productName" has many variation in length since I fetch the data from an API endpoint.

I don't think there's any problem with the fragment or the recyclerview adapter.

Did I miss something here ? If there's any detail I miss please mention in the Comment.

3 Answers

Please try to change the height of the ImageView to wrap_content

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher_background"
        />

I can't comment because of my reputation.

Here are some thoughts I had

  • is the layout consistently repeating the pattern? or maybe the data just isnt there and since the layout is set to wrap content, it hides the textview? maybe try putting some logs and see what's happening.
  • Your layout structure seems kinda complicated for recycler view items. maybe flatten it out with constraint layout? it is easier if you are trying to have imageview fill the entire layout with couple of text online.

Check if in other cells you set data. Sometimes recyclerView works not correct with empty cells. Check if you fill all include name, price, quantity etc.

Related