Beruflich Dokumente
Kultur Dokumente
http://www.mkyong.com/android/android-gridview-example/
Java Forum
Advertise
Contact Us
RSS
Java Core
Web Frameworks
Spring
Hibernate
Web Service
Others...
Search
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.
Ramesh
Mohamed
Abhishek
Sukhmeet
Santhosh
Ishtiyaq
Mohammad
Amila
Wee
Sneha
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
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
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
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"?>
2 de 7
27/02/12 17:04
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
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
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
http://www.mkyong.com/android/android-gridview-example/
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
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
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)
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
http://www.mkyong.com/android/android-gridview-example/
Vote
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
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
7 de 7
27/02/12 17:04