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>
Archived Comments
1. 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
2. i am in beginning level, i tried by this code. but i could not create the class code.
How can
View Tutorial By: Shahana Akter at 2014-09-09 10:30:21
3. Thats very good and simple example indeed it helps in understanding the concept of gridview UI layi
View Tutorial By: pavan at 2013-07-26 04:17:32
4. i have created a listview and i have a button in the same activity, on clicking the button my listvi
View Tutorial By: prateek at 2013-05-19 18:52:47
5. Hi, Very nice article sir...
i am a beginner in development field.. i have been looking fr m
View Tutorial By: therohan at 2013-04-11 11:37:53
6. how to display image on gridview from mysql database in android
View Tutorial By: chirag at 2013-03-18 08:12:46
7. Hi... very nice tute... Thanks..
I am working on a Sales App... & I want a interface wher
View Tutorial By: anuj at 2013-03-15 06:12:46
8. Hi Ashley, thank you for this Android Gridview tutorial. I compiled a list of some top resources aro
View Tutorial By: Giancarlo Leonio at 2013-03-12 00:37:15
9. Pavan
@(http://www.pavanhd.blogspot.in/)
You Can also find here for m
View Tutorial By: pavan at 2013-03-01 10:41:47
10. to watch more information for android development , go to BlueRayPlus.com
View Tutorial By: Rohit at 2012-09-10 07:39:15
11. Nice contents, you can get more android sample example, solution, tutorial and basic examples of the
View Tutorial By: hemant at 2011-10-03 05:23:59
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
Related Tutorials
Get Location of an android phone programmatically
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