Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which stretchMode to use in GridView on android

Following is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridviewgallery"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="@dimen/gridview_column_width"
    android:numColumns="auto_fit"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:background="#444444"
/>

When I use android:stretchMode="columnWidth" , it creates a gap between the columns(though it fills the screen width) And if I use android:stretchMode="none" , it gets left aligned and there is a gap left on right side of screen(though now there is no gap between columns) Problem is I want both : To fill the screen width as well no gap between columns. How to achieve it?

like image 949
Pankaj Vatsa Avatar asked Jul 10 '13 09:07

Pankaj Vatsa


People also ask

Is GridView deprecated in Android?

Both GridView and GridLayout are safe for use in production code. They are obsolete, and I would not recommend that anyone use them, but they are safe.

Which attribute is used to define the number of columns to be added to the GridView?

android:numColumns: This attribute of GridView will be used to decide the number of columns that are to be displayed in Grid. android:horizontalSpacing: This attribute is used to define the spacing between two columns of GridView.

What is the difference between GridLayout and GridView in Android?

Let us not get confused with GridView and GridLayout to be the same. GridView simply gives us a two-dimensional view to display the items on the screen, under ViewGroup. On the other hand, GridLayout is a layout manager that arranges the views in a grid.


1 Answers

A combination of android:numColumns="auto_fit" android:stretchMode="columnWidth" and android:columnWidth="60dp" will give you a layout that adjusts for different screen sizes. Much better than fixing the number of columns. See example below...

<GridView
        android:id="@+id/tablegrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffff7965"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:columnWidth="60dp"
        />
like image 101
user3188040 Avatar answered Oct 06 '22 00:10

user3188040