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.
Coding:
Now let’s go to the coding section. This project requires following files.
Source Code:
- Gridview.java
Activity Files:
- activity_gridview.xml - gridview activity (Main activity)
res – Values:
- strings.xml - strings available in grid view activity
Manifest file:
- AndroidManifest.xml
Here is the coding for the above files.
Gridview.java:
package com.simplecodestuffs.gridview; // Package gridview import android.app.Activity; // 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"}; @Override public void onCreate(Bundle savedInstanceState) { // Create a new activity super.onCreate(savedInstanceState); setContentView(R.layout.activity_gridview);//Displays activity/screen when app starts // acquiring grid properties from layout gridView = (GridView) findViewById(R.id.gridView); // create an adapter using the populated list 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) { // display pop message Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); } }); } }
activity_gridview.xml:
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView" android:numColumns="auto_fit" android:gravity="center" android:columnWidth="50dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > </GridView>
Menu : activity_gridview.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_settings" android:orderInCategory="100" android:title="@string/menu_settings"/> </menu>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.simplecodestuffs.gridview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.simplecodestuffs.gridview.Gridview" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">GridView</string> <string name="menu_settings">Settings</string> </resources>
Styles. xml:
<resources> <!-- 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. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> </resources>
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.
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.
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.
To test your application, unlock the screen and double click on your app.
Clicking button A:
Clicking button P:
Thus the android application project is executed successfully.
Click Here To Share This..!!
Share