-1

I am working in an Android app. In this I have an issue that my GridView and images are not get fit with all screen sizes .

Here I have the gridview inside the fragment

The fragment_reminders.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout_Gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <GridView
        android:id="@+id/mGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="10dp" />
</LinearLayout>

And gridview.xml is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="5dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView
            android:id="@+id/mImageView"
            android:layout_width="35dp"
            android:layout_height="35dp" />

        <TextView
            android:id="@+id/mTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textAlignment="center"
            android:textSize="10dp" />
</LinearLayout>

Can anyone help me to fix this issue?

Nikson
  • 900
  • 2
  • 21
  • 51

2 Answers2

0

Use GridLayout to acquire the desired result.Firstly add gridLayout dependancy:

compile 'com.android.support:gridlayout-v7:27.0.1'

Then, add the following xml code:

 <android.support.v7.widget.GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="3">

    <FrameLayout
        app:layout_columnWeight="1"
        app:layout_rowWeight="1">

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_lock_idle_alarm"
                android:layout_marginBottom="5dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Alarms"/>
        </LinearLayout>

     </FrameLayout>

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

In the above code, I've just added 1 element. Just copy & paste this FrameLayout and change texts & images accordingly. After adding 12 elements, the output should look as below:

enter image description here

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
0

Try the follwoing GridLayout,

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        app:alignmentMode="alignBounds"
        app:columnCount="3"
        app:rowCount="5">

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

And change your GridLayout item to the following,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="1.5dp"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="5dp"
        app:layout_columnWeight="1"
        app:layout_gravity="fill"
        app:layout_rowWeight="1">

        <ImageView
            android:id="@+id/mImageView"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/mTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textAlignment="center"
            android:textSize="10dp" />

    </LinearLayout>

To use the support.v7.widget.GridLayout add the dependency,

implementation 'com.android.support:cardview-v7:27.0.1'

Haven't tested yet, but it should work.

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50