Android GridView Example

By | September 25, 2012
In Android , grid view that lets you display components in a two dimensional scrolling grid Gridview is mainly useful when we want show data in grid layout like displaying images or icons
In this example you will see the months names stored in string variable are displayed in gridview

Lets See an Example

Project Detail

Project Name GridView
Package com.pavan.gridview
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

file :- grid_layout.xml

< GridView
  xmlns:android=”http://schemas.android.com/apk/res/android”
  android:id=”@+id/grid_id”
  android:layout_width=”fill_parent”
  android:layout_height=”fill_parent” 
  android:columnWidth=”50dp”
  android:gravity=”center”
  android:numColumns=”auto_fit”
  android:padding=”15dp”
   android:stretchMode=”columnWidth” />

2. Activity

 file:- Grid_activity.java

package com.pavan.gridview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class Grid_activity extends Activity {

String[] data = new String[] { “jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”,
“Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec” };

GridView gv;

@Override
public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     setContentView(R.layout.grid_layout);

 ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_list_item_1, data);

     gv = (GridView) findViewById(R.id.grid_id);
     gv.setAdapter(aa);
}
}

3. RUN

Android GridView