JavaEnthusias
JavaEnthusias

Reputation: 31

Android RecyclerView GridLayout size issue

I am trying to achieve the following result.

Expected Result Image

And I am getting the following result.

Actual Result Image

can anyone guide me to achieve the expected results.

I have shared my code below,

 mLayoutManager = new GridLayoutManager(MyGridActivity.this,3);
 recyclerView.setLayoutManager(mLayoutManager);
 adapter = new MyGridAdapterAdapter(MyGridActivity.this,arrayList);
 recyclerView.setAdapter(adapter);

Sharing my item_layout xml file for grid list.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView

    app:cardElevation="3dp"
    app:cardUseCompatPadding="true"
    app:cardCornerRadius="10dp"
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/cardView">

    <LinearLayout

        android:id="@+id/itemLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:scaleType="center"
            android:src="@mipmap/health_2" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardView"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:text="Health" />

    </LinearLayout>

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

Upvotes: 1

Views: 2291

Answers (2)

Rohan Shukla
Rohan Shukla

Reputation: 439

Try Using this code just copy and paste in your Activity while initializing

mLayoutManager = new GridLayoutManager(MyGridActivity.this,3);
recyclerView.setLayoutManager(mLayoutManager);

recyclerView.setHasFixedSize(true);//This line

adapter = new MyGridAdapterAdapter(MyGridActivity.this,arrayList);
recyclerView.setAdapter(adapter);

Upvotes: 0

Naveen Dew
Naveen Dew

Reputation: 1295

Your TextView length is disturbing GridItems

Either give your parent layout a fixed height or give your TextView a fixed height

try this ...

update your TextView from

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/cardView"
    android:layout_marginBottom="20dp"
    android:gravity="center"
    android:text="Health" />

to

    <TextView
        android:lines="2"
        android:maxLines="2"
        android:ellipsize="end"
        android:id="@+id/tv_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardView"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="Health" />

Result

Result

Upvotes: 1

Related Questions