Skip to content Skip to sidebar Skip to footer

Gridview Not Fit For All Screens In Android

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 fragme

Solution 1:

Try the follwoing GridLayout,

<android.support.v7.widget.GridLayoutandroid: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,

<LinearLayoutxmlns: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"><ImageViewandroid:id="@+id/mImageView"android:layout_width="35dp"android:layout_height="35dp"android:layout_gravity="center" /><TextViewandroid: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.

Solution 2:

Use GridLayout to acquire the desired result.Firstly addgridLayoutdependancy:

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

Then, add the following xml code:

<android.support.v7.widget.GridLayoutandroid:id="@+id/gridLayout"android:layout_width="match_parent"android:layout_height="match_parent"app:columnCount="3"><FrameLayoutapp:layout_columnWeight="1"app:layout_rowWeight="1"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@android:drawable/ic_lock_idle_alarm"android:layout_marginBottom="5dp"/><TextViewandroid: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

Post a Comment for "Gridview Not Fit For All Screens In Android"