GridView sample program in Android
By: Ashley
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>
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Solution to error: unable to open connection to server due to security error
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial
Intent in Android to call one activity from another activity.
Progress bar and downloading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
Reading a file sample program in Android
Date and Time sample program in Android
Gallery sample program in Android
GridView sample program in Android
Spinner sample program in Android
Archived Comments
1. Nice contents, you can get more android sample exa
View Tutorial By: hemant at 2011-10-03 05:23:59
2. to watch more information for android development
View Tutorial By: Rohit at 2012-09-10 07:39:15
3. Pavan
@(http://www.pavanhd.blogspo
View Tutorial By: pavan at 2013-03-01 10:41:47
4. Hi Ashley, thank you for this Android Gridview tut
View Tutorial By: Giancarlo Leonio at 2013-03-12 00:37:15
5. Hi... very nice tute... Thanks..
I am worki
View Tutorial By: anuj at 2013-03-15 06:12:46
6. how to display image on gridview from mysql databa
View Tutorial By: chirag at 2013-03-18 08:12:46
7. Hi, Very nice article sir...
i am a beginne
View Tutorial By: therohan at 2013-04-11 11:37:53
8. i have created a listview and i have a button in t
View Tutorial By: prateek at 2013-05-19 18:52:47
9. Thats very good and simple example indeed it help
View Tutorial By: pavan at 2013-07-26 04:17:32
10. i am in beginning level, i tried by this code. but
View Tutorial By: Shahana Akter at 2014-09-09 10:30:21
11. i want to do a program by using convertView with e
View Tutorial By: Shahana Akter at 2014-09-11 07:34:43