Sie sind auf Seite 1von 7

Android GridView example

http://www.mkyong.com/android/android-gridview-example/

Java Forum

Advertise

Contact Us

RSS

Java Core

Web Frameworks

Spring

Hibernate

Web Service

Others...

Search

Android GridView example


Published: February 21, 2012 , Updated: February 21, 2012 , Author: mkyong

In Android, GridView let you arranges components in a two-dimensional scrolling grid. For detail attribute exaplanation, see GridView reference. In this tutorial, we will show you 2 common GridView examples : 1. Normal way, just display text in GridView layout. 2. Create a custom adapter to display image and text in GridView layout.
java Formacin Online y gratuita para obtener tu certificado en java ifi.com.es/es/IT_Certificaciones Master Java-J2EE OnLine. 12 meses. Desde 450 . Si pagas ms, es porque quieres. www.syncrom.com Android app marketing AppBrain offers pay-per-download bidding to promote your free app. www.appbrain.com

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3. Mkyong en Facebook
Me gusta A 2,328 personas les gusta Mkyong.

1. Normal GridView example


Display characters from A to Z in GridView layout. Quite simple, it should be sef-explanatory.

Ramesh

Mohamed

Abhishek

Sukhmeet

Santhosh

1.1 Android Layout file res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView1" android:numColumns="auto_fit" android:gravity="center" android:columnWidth="50dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > </GridView>

Ishtiyaq

Mohammad

Amila

Wee

Sneha

Plug-in social de Facebook

1.2 Activity
package com.mkyong.android; import import import import import import import import import android.app.Activity; android.os.Bundle; android.widget.AdapterView; android.widget.ArrayAdapter; android.widget.GridView; android.widget.TextView; android.widget.Toast; android.view.View; android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity { GridView gridView; static final String[] numbers = new "A", "B", "C", "D", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q", "R", "S", "U", "V", "W", "X", String[] { "E", "J", "O", "T", "Y", "Z"};

1 de 7

27/02/12 17:04

Android GridView example

http://www.mkyong.com/android/android-gridview-example/

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.gridView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); } }); } }

1.3 Demo

Subscribe Mkyong via email :

Subscribe

Latest Posts
Android WebView example Android TabLayout example How to import class automatically in Eclipse How to remove unused imports in Eclipse Android GridView example

2. Custom Adapter example


In this example, extend a BaseAdapter to display a group of image and text in GridView layout. 2.1 Two Android Layout files File res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView1" android:numColumns="auto_fit" android:gravity="center" android:columnWidth="100dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > </GridView>

How to display line numbers in Eclipse How to check if Date is within a certain range in Java? How to check if date is valid in Java Struts 2 dynamic image example try-with-resources example in JDK 7

Advertisement

As a market leader in the reseller hosting industry, micfo.com offers you a great opportunity to start your own web hosting company. Join them today.

Latest Comments
File res/layout/mobile.xml
<?xml version="1.0" encoding="utf-8"?>

Navin on JSON example with Jersey + Jackson

2 de 7

27/02/12 17:04

Android GridView example


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <ImageView android:id="@+id/grid_item_image" android:layout_width="50px" android:layout_height="50px" android:layout_marginRight="10px" android:src="@drawable/android_logo" > </ImageView> <TextView android:id="@+id/grid_item_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:layout_marginTop="5px" android:textSize="15px" > </TextView> </LinearLayout>

http://www.mkyong.com/android/android-gridview-example/

How to write a service consuming a JSON string as a parameter. I mean, method should consume directl... Meethas on Maven + (Spring + Hibernate) Annotation + MySql Example Hi , I need a simple hibernate project without maven or ant and i need to run it in apache tomcat se... HibLearner on Hibernate fetching strategies examples Suppose I have an Order table with 50 columns and I have an Order class with 50 properties and mappe... mkyong on Connect to PostgreSQL with JDBC driver Make sure postgresql-9.1-901.jdbc3.jar is configured correctly and able to locate in your class path... mkyong on Struts 2 Hello World Example

2.2 Custom Adapter


package com.mkyong.android.adapter; import import import import import import import android.content.Context; android.view.LayoutInflater; android.view.View; android.view.ViewGroup; android.widget.BaseAdapter; android.widget.ImageView; android.widget.TextView;

Not really related with above article, please post your question on JavaNullPointer.com? And elabora...

import com.mkyong.android.R; public class ImageAdapter extends BaseAdapter { private Context context; private final String[] mobileValues; public ImageAdapter(Context context, String[] mobileValues) { this.context = context; this.mobileValues = mobileValues; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View gridView; if (convertView == null) { gridView = new View(context); // get layout from mobile.xml gridView = inflater.inflate(R.layout.mobile, null); // set value into textview TextView textView = (TextView) gridView .findViewById(R.id.grid_item_label); textView.setText(mobileValues[position]); // set image based on selected text ImageView imageView = (ImageView) gridView .findViewById(R.id.grid_item_image); String mobile = mobileValues[position]; if (mobile.equals("Windows")) { imageView.setImageResource(R.drawable.windows_logo); } else if (mobile.equals("iOS")) { imageView.setImageResource(R.drawable.ios_logo); } else if (mobile.equals("Blackberry")) { imageView.setImageResource(R.drawable.blackberry_logo); } else { imageView.setImageResource(R.drawable.android_logo); } } else { gridView = (View) convertView; } return gridView; } @Override

3 de 7

27/02/12 17:04

Android GridView example


public int getCount() { return mobileValues.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } }

http://www.mkyong.com/android/android-gridview-example/

2.3 Activity
package com.mkyong.android; import import import import import import import import import com.mkyong.android.adapter.ImageAdapter; android.app.Activity; android.os.Bundle; android.widget.AdapterView; android.widget.GridView; android.widget.TextView; android.widget.Toast; android.view.View; android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity { GridView gridView; static final String[] MOBILE_OS = new String[] { "Android", "iOS","Windows", "Blackberry" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.gridView1); gridView.setAdapter(new ImageAdapter(this, MOBILE_OS)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText( getApplicationContext(), ((TextView) v.findViewById(R.id.grid_item_label)) .getText(), Toast.LENGTH_SHORT).show(); } }); } }

2.4 Demo

4 de 7

27/02/12 17:04

Android GridView example

http://www.mkyong.com/android/android-gridview-example/

Download Source Code

Download it Android-GridView-Example.zip (21 KB)

References
1. GridView Javadoc 2. Android GridView example 3. Android ListView example

To Whom It May Concern, If you have any Java questions or problems, please post at this new JavaNullPointer.com forum. Best Regards, mkyong

Related Posts
Android TabLayout example Android TableLayout example

Popular Posts
Top 5 free Java eBooks Top 8 Java people you should know

5 de 7

27/02/12 17:04

Android GridView example


Android WebView example Android ListView example How to set default activity for Android application

http://www.mkyong.com/android/android-gridview-example/
Top 20 Java websites you must visit Top 10 Java regular expression examples Top 10 open source forums in collection Top 5 open source Q&A systems

Java / J2EE Tutorials

1 Comment
Neel says:
February 22, 2012 at 4:40 pm

How to get the name of selected item into string rather then using Toast.makeText(getApplicationContext(), ((TextView) v.findViewById(R.id.grid_item_label)) .getText(), Toast.LENGTH_SHORT).show(); i want to store the result in 1 string .. Thank You
Reply

Leave a Reply
Name (required)

Mail (will not be published) (required)

Website

[Note] - To post source code in comment, wrap your source code like this :

1. Java - <pre lang="java"> Java codes here </pre> 2. XML - <pre lang="xml"> XML here </pre> 3. HTML - <pre lang="html4strict"> HTML here </pre>

Submit Comment

6 de 7

27/02/12 17:04

Android GridView example

http://www.mkyong.com/android/android-gridview-example/

Notify me of followup comments via e-mail

Poll : What is your current Java web application framework?


Apache Struts Apache Struts2 Apache Tapestry Apache Wicket Custom in-house Framework Google Web Toolkit (GWT) JavaServer Faces (JSF 1.x) JavaServer Faces (JSF 2.x) JBoss Seam No framework, code in JSP Oracle Application Framework Others Spring MVC Spring WebFlow Stripes View Results

Vote

All Available Tutorials


Java Core Technologies : Java I/O, Java RegEx, Java XML, Java JSON, JDBC, Java Misc J2EE Frameworks : Hibernate, JSF 2.0, Spring Core, Spring MVC, Spring Security, Apache Wicket, Struts 1.x, Struts 2.x Web Service : JAX-WS (SOAP), JAX-RS (REST) Build Tools : Maven, Archiva Unit Test Frameworks : jUnit, TestNG Others... : jQuery, Java MongoDB

Favorites Links
DZone - Fresh Links Official Java EE 5 Tutorial Spring 2.5.x documentation Hibernate core documentation Java SE 6.0 API documentation Java EE 6.0 API documentation Java Secure Socket Extension (JSSE) Reference Guide JSP home page JSF home page Eclipse IDE for Java developer Maven home page Ant home page Struts 1.3 documentation Struts 2.2 documentation Maven central repository Java.Net Maven repository Martin Fowler

Friends & Links


Java Training JavaScript Training Java Web Hosting Java & Co. Tutorials PHP Tutorials TenthOfMarch Find Free Icons Web Security Blog

About Mkyong.com
Mkyong.com is about a person who is facing the big tree (Java web development), always wonder why the tree (Java) is so big!

All tutorials and examples are unique, and from my personal experience, if you find mistake in my tutorial, please correct me :) after all, we learned through the process. For Java question that is not related to the tutorial, you can post here - Java Q&A forum. 1. Twitter - Follow Me 2. Facebook - Fan Page 3. RSS - Subscribe It Advertise With Us

Copyright 2008-2011 Mkyong Enterprise, all rights reserved. Privacy Policy

7 de 7

27/02/12 17:04

Das könnte Ihnen auch gefallen