The GridView view mode displays a list of data items by binding data fields to columns and by displaying a column header to identify the field. The column cells and the column header of a GridViewColumn have the same width.
Introduction
The GridView view mode displays a list of data items by binding data fields to columns and by displaying a column header to identify the field. The column cells and the column header of a GridViewColumn have the same width. By default, each column sizes its width to fit its content. Optionally, you can set a column to a fixed width. Related data content displays in horizontal rows.
GridView in Android
GridView is a ViewGroup in ehich you can displays items in a two-dimensional and also scrollable grid. By using ListAdapter you can add items in the grid the items are automatically inserted to the layout. GridView is basically used to create more interactive app widgets on the users Home screen. You can use the GridView view together with ImageView views to display a series of images.
The table below shows the XML Attributes which you have to use while working with GridView XML file:
I am trying to explain you how to use GridView in Android by given example below:
Steps to create GridView in Android
Create a new ImageView for each item referenced by the Adapter public override View GetView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(contextcreate); imageView.LayoutParameters = new GridView.LayoutParams(90, 90); imageView.SetScaleType(ImageView.ScaleType.CenterCrop); imageView.SetPadding(10, 10, 10, 10); } else { imageView = (ImageView)convertView; } imageView.SetImageResource(imageIds[position]); return imageView; }
This are the basic methods which you learn above, now here is the complete code:
using System; using Android.App;using Android.Content;using Android.Runtime;using Android.Views;using Android.Widget;using Android.OS; namespace WelcomeGridView{ [Activity(Label = "WelcomeGridView", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : Activity { protected override void OnCreate(Bundle bundle) base.OnCreate(bundle); SetContentView(Resource.Layout.Main); var PhotoGridView = FindViewById<GridView>(Resource.Id.PhotoGridView); PhotoGridView.Adapter = new ImageAdapter(this); PhotoGridView.ItemClick += delegate(object sender, ItemEventArgs args) { Toast.MakeText(this, (args.Position+1).ToString(), ToastLength.Short).Show(); }; } public class ImageAdapter : BaseAdapter { Context contextcreate; public ImageAdapter(Context a) { contextcreate = a; } public override int Count { get { return imageIds.Length; } } public override Java.Lang.Object GetItem(int position) { return null; } public override long GetItemId(int position) { return 0; } public override View GetView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(contextcreate); imageView.LayoutParameters = new GridView.LayoutParams(90, 90); imageView.SetScaleType(ImageView.ScaleType.CenterCrop); imageView.SetPadding(10, 10, 10, 10); } else { imageView = (ImageView)convertView; } imageView.SetImageResource(imageIds[position]); return imageView; } int[] imageIds = { Resource.Drawable.mahesh, Resource.Drawable.dbeniwal321, Resource.Drawable.kartik, Resource.Drawable.mgold, Resource.Drawable.dhananjaycoder, Resource.Drawable.nandi, Resource.Drawable.praveen, Resource.Drawable.suthish, Resource.Drawable.nipuntomar, Resource.Drawable.dpatra, Resource.Drawable.jaganathan, Resource.Drawable.abhikumarvatsa, }; } }}
public ImageAdapter(Context a) { contextcreate = a; } public override int Count { get { return imageIds.Length; } } public override Java.Lang.Object GetItem(int position) { return null; } public override long GetItemId(int position) { return 0; } public override View GetView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null)
{ imageView = new ImageView(contextcreate); imageView.LayoutParameters = new GridView.LayoutParams(90, 90); imageView.SetScaleType(ImageView.ScaleType.CenterCrop); imageView.SetPadding(10, 10, 10, 10); }
else { imageView = (ImageView)convertView; }
imageView.SetImageResource(imageIds[position]); return imageView;
}
int[] imageIds = {
Resource.Drawable.mahesh, Resource.Drawable.dbeniwal321,
Resource.Drawable.kartik, Resource.Drawable.mgold, Resource.Drawable.dhananjaycoder, Resource.Drawable.nandi,
Resource.Drawable.praveen, Resource.Drawable.suthish,
Resource.Drawable.nipuntomar, Resource.Drawable.dpatra,
Resource.Drawable.jaganathan, Resource.Drawable.abhikumarvatsa, }; } }}
When you click on any image the position of the image will show on the screen:
Happy Learning.......
Pro WPF: Windows Presentation Foundation in .NET 3.0