3

I would like to create a GridView with squares items on two columns which would take the totality of the screen (match_parent).

Every item consists of an imageView and a textView.

Here is the result in image :

enter image description here

How do I make the items width to be equal to their variable height?

Thank you in advance!!

lopez.mikhael
  • 9,943
  • 19
  • 67
  • 110

3 Answers3

1

In your activity or Fragment layout you should have GridView like this:

<GridView
        android:id="@+id/grid_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
         />

And then you should have an Adapter that extends BaseAdapter for example and it's item should be as follow:

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawableTop="@drawable/yourDrawable"
        android:layout_margin="20dip"
        android:gravity="center" />

You need more explanation ?

elmorabea
  • 3,243
  • 1
  • 14
  • 20
0

Use these link, they will help you Option 1

Option 2

Community
  • 1
  • 1
BaDo
  • 540
  • 8
  • 19
  • Never put only links as your answer because those links usually die, and therefore your answer is useless. Either explain your solution or don't answer at all. – busuu Aug 02 '17 at 22:42
0

This is Gridview.

<GridView
    android:numColumns="2"
    android:id="@+id/related_contact_grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

This is item. You will inflate this to gridview in Adapter.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView"
        android:text="Tendance"
        android:layout_centerHorizontal="true"

</RelativeLayout>

If you want, I can support your work on this Gridview.

CleanCoder
  • 332
  • 3
  • 14