Ram Ram - 3 months ago 50
Android Question

Dynamically change column number in android gridview?

My application display the image icon in gridview in landscape orientation.For that i use the xml as

<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="4"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"/>


For portrait orientation i want to display only two image icon in gridview column.How to do it?.

Answer

By using adaptive resources: make sure in resources folder /res you have the following folders: values-land and values-port. In both of those folders add a resource file, let's call it integers.xml.

In /values-land/integers.xml you will have at least:

<resources>
    <item name="grid_rows" type="integer">4</item>
</resources>

while for values-port/integers.xml:

<resources>
    <item name="grid_rows" type="integer">3</item>
</resources>

The layout changes to:

<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="100px"
    android:gravity="center"
    android:numColumns="@integer/grid_rows"
    android:stretchMode="columnWidth" />

Note the presence of @integer/grid_rows