GridView sample program in Android

By: Ashley Emailed: 1643 times Printed: 2113 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This sample android program shows you how to use GridView in Android. In this program a list is shown as a grid. When you click on the list, the selected item is shown on the text view. You can use this ArrayAdapter widget and the GridView object together with the onListItemClick() method to determine the selected index and process accordingly.

The GridViewDemo.java file is as follows:

package com.javasamples;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;


public class GridViewDemo extends Activity implements
		AdapterView.OnItemClickListener {

	TextView selection;
	String[] items = { "this", "is", "a", "really", "really2", "really3",
			"really4", "really5", "silly", "list" };

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		selection = (TextView) findViewById(R.id.selection);

		GridView gv = (GridView) findViewById(R.id.grid);

		ArrayAdapter<String> aa = new ArrayAdapter<String>(
				this,
				android.R.layout.simple_list_item_1, 
				items );

		gv.setAdapter(aa);
		gv.setOnItemClickListener(this);
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		selection.setText(items[position]);
	}
	
}// class


The output of this program will be as shown in the android emulator below.

The main.xml file in your res/layout folder is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff0033cc"
	  android:textSize="14pt"
 	  android:textStyle="bold"
        
        />
    <GridView
        android:id="@+id/grid"
        android:background="#ff0000ff"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:verticalSpacing="35px"
        android:horizontalSpacing="5px"
        android:numColumns="auto_fit"
        android:columnWidth="100px"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />
</LinearLayout>

Android Home | All Android Tutorials | Latest Android Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(6)


1. View Comment

Hi Ashley, thank you for this Android Gridview tutorial. I compiled a list of some top resources around this topic. I included your post. Check it out/ feel free to share. http://www.verious.com/board/Giancarlo-Leonio/creating-an-android-gridview-layout/ Hope other developers find this useful as well. :)

View Tutorial          By: Giancarlo Leonio at 2013-03-12 00:37:15
2. View Comment

Hi... very nice tute... Thanks..
I am working on a Sales App... & I want a interface where user can add items in a grid..
e.g. In a Grid-
1st column will be Combobox to select Item - on selecting Item all related details should be filled.

Basically is that possible in android? And Can you please give me some idea?
I have worked on such interface in .NET but how to do it in android? Please help..
Thanks!


View Tutorial          By: anuj at 2013-03-15 06:12:46
3. View Comment

how to display image on gridview from mysql database in android

View Tutorial          By: chirag at 2013-03-18 08:12:46
4. View Comment

i have created a listview and i have a button in the same activity, on clicking the button my listview should convert into gridview means another activity should open with same content but in gridview.
How can i do this please tell me.


View Tutorial          By: prateek at 2013-05-19 18:52:47
5. View Comment

i am in beginning level, i tried by this code. but i could not create the class code.
How can i create the class for this aa object. pealse help me.


View Tutorial          By: Shahana Akter at 2014-09-09 10:30:21
6. View Comment

i want to do a program by using convertView with explanation in android.

View Tutorial          By: Shahana Akter at 2014-09-11 07:34:43

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ashley
Is autodiscover CNAME a must in Office 365
Directory Synchronization tool in Office 365
Configure Outlook Manually for Office 365
Review: Ruggear - The world's most Rugged Phone in Singapore
Solution to error: unable to open connection to server due to security error
Making your Alias email as 'Send As' in Office 365
Steps to Create Shared Mailbox in office 365
xxxx cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
The user's Outlook Live mailbox is full.
Outlook Live couldn't connect to the IMAP server.
The user's mailbox has already been migrated.
An Outlook Live mailbox wasn't found for the user.
Outlook Live couldn't sign in to the user account on the IMAP messaging system.
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial

More Tutorials in Android
Getting Started with Android
Solution to error: unable to open connection to server due to security error
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Android Emulator
Intent in Android to call one activity from another activity.
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Reading a file sample program in Android

More Latest News
Most Viewed Articles (in Android )
Reading and Writing a file to SD card sample program in Android
Progress bar and downloading a file sample program in Android
ArrayAdapter sample program in Android
Android Send SMS and Make a phone call in Android Application
Date and Time sample program in Android
Intent in Android to call one activity from another activity.
Spinner sample program in Android
Reading a file sample program in Android
Animation sample program in Android
GridView sample program in Android
Solution to error: unable to open connection to server due to security error
Gallery sample program in Android
Getting Started with Android
Error generating R.java from manifest
Android Emulator
Most Emailed Articles (in Android)
Intent in Android to call one activity from another activity.
Getting Started with Android
Error generating R.java from manifest
Progress bar and downloading a file sample program in Android
Android Emulator
Android Send SMS and Make a phone call in Android Application
AlertDialog sample program in Android
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Reading a file sample program in Android