Pages Navigation Menu

Coding is much easier than you think

How To Create Grid View in an Android Application

How To Create Grid View in an Android Application

In this Tutorial, you will Learn How to view the activity using Grid view

Here you go!!!!



Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

Gridview: GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.
** UPDATE: Android Complete tutorial now available here.
List Adapter: Extended Adapter that is the bridge between a ListView and the data that backs the list. Frequently that data comes from a Cursor, but that is not required. The ListView can display any data provided that it is wrapped in a ListAdapter.

The tutorial is about how to display contents in activity in the grid view /matrix.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to android application project, to create a new project in Eclipse refer Creation of Android Project.


Now let’€™s go to the coding section. This project requires following files.

Source Code:


Activity Files:

  1. activity_gridview.xml -€“ gridview activity (Main activity)

res – Values:

  1. strings.xml -€“ strings available in grid view activity

Manifest file:

  1. AndroidManifest.xml

Here is the coding for the above files.

package com.simplecodestuffs.gridview; // Package gridview
import; // Required to create an activity
import android.os.Bundle; // A mapping from String values to various Parcelable types
import android.widget.AdapterView; // bridge between a ListView and the data
import android.widget.ArrayAdapter; // expects provided resource id references a single TextView
import android.widget.GridView; // displays items in a two-dimensional
import android.widget.TextView; // displays text in the layout
import android.widget.Toast; //displays pop notification message
import android.view.View; // Required for various views and view groups
import android.widget.AdapterView.OnItemClickListener;
public class Gridview extends Activity { // that displays items in a two-dimensional
	GridView gridView;// object for Gridview
	static final String[] numbers = new String[] {  // list of texts to be displayed in Grid
			"A", "B", "C", "D", "E",
			"F", "G", "H", "I", "J",
			"K", "L", "M", "N", "O",
			"P", "Q", "R", "S", "T",
			"U", "V", "W", "X", "Y", "Z"};
	public void onCreate(Bundle savedInstanceState) { // Create a new activity
		setContentView(R.layout.activity_gridview);//Displays activity/screen when app starts
// acquiring grid properties from layout

		gridView = (GridView) findViewById(;
// create an adapter using the populated list
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, numbers);
 		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
				int position, long id) {
// display pop message
				((TextView) v).getText(), Toast.LENGTH_SHORT).show(); 


<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android=""
    android:layout_height="fill_parent" >

Menu : activity_gridview.xml

<menu xmlns:android="" >




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="8" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



<?xml version="1.0" encoding="utf-8"?>

    <string name="app_name">GridView</string>
    <string name="menu_settings">Settings</string>


Styles. xml:


        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    <style name="AppBaseTheme" parent="android:Theme.Light">
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->


Run the android application:

Android applications can be run on the android devices. You can either connect hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project -> Build Project.

Build project

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched which displays the AVD on your screen.

You can see your app with the image set during the android project creation in AVD. Upon running the application, the emulator will be launched with the selected/ available AVD created on your screen.

Start Emulator

Emulator loading home page

To test your application, unlock the screen and double click on your app.

AVD Home screen


Grid view

Clicking button A:

Grid view_clicking A

Clicking button P:

Grid view_clicking P

Thus the android application project is executed successfully.


Click Here To Share This..!!

%d bloggers like this: