0

I am trying to create a grid layout of cards similar to what is used in google play like this

enter image description here

The layout i am using and the layout obtained are these:

For GridView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#e5e5e5"
     >

    <GridView  
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="60dp"
    android:numColumns="2"
    android:layout_marginLeft="2dp"

    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

</RelativeLayout>

for GridItem:

<?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"
android:background="@drawable/bg_card"

>

<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
     />

  <TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:textSize="17sp"
    android:layout_below="@id/imgIcon"
    android:layout_marginTop="35dp"
    android:layout_centerInParent="true"/>
</RelativeLayout>

The output that I get is this:

enter image description here

The image size that i am using is 356x500. (I do get a somewhat well sized grid on using 300x300) The issues that i am facing are:

  1. 1.As this image shows, the cards are too big with lots of white spaces around the image and the image does not fit like in the google play store. How do i make the card be propotional to the image. The amount of white space changes with change in image size.
  2. The card is "stuck" to the left edge of the screen . How do i keep spacing between the edge of the screen and the card.

Please help me in rectifying this issue. Thanks

Shivam Bhalla
  • 1,879
  • 5
  • 35
  • 63

3 Answers3

0

Space are bacause you used

android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"

Also edit your

<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
     />

in gridview use android:stretchMode="none" instead of android:stretchMode="columnWidth"

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

Put android:scaleType="fitStart" inside your

zainoz.zaini
  • 918
  • 6
  • 20
0

Use imgIcon.setAdjustViewBounds(true); You can also do the same in the xml