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 which 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:
Attribute Name Related Method Description
android:columnWidth setColumnWidth(int) Used for width for each column.
android:gravity setGravity(int) Used for gravity within each cell.
android:horizontalSpacing setHorizontalSpacing(int) Used for default horizontal spacing between columns.
android:numColumns setNumColumns(int) Used for how many columns want to show.
android:stretchMode setStretchMode(int) Used for fill the available empty space.
android:verticalSpacing setVerticalSpacing(int) Used for default vertical spacing between rows.
I am trying to explain to you how to use GridView in Android by the given example below:
Steps to create GridView in Android
This is the basic methods which you learn above, nowhere is the complete code:
  1. using System;
  2. using Android.App;
  3. using Android.Content;
  4. using Android.Runtime;
  5. using Android.Views;
  6. using Android.Widget;
  7. using Android.OS;
  8. namespace WelcomeGridView
  9. {
  10. [Activity(Label = "WelcomeGridView", MainLauncher = true, Icon = "@drawable/icon")]
  11. public class Activity1: Activity
  12. {
  13. protected override void OnCreate(Bundle bundle)
  14. base.OnCreate(bundle);
  15. SetContentView(Resource.Layout.Main);
  16. var PhotoGridView = FindViewById < GridView > (Resource.Id.PhotoGridView);
  17. PhotoGridView.Adapter = new ImageAdapter(this);
  18. PhotoGridView.ItemClick += delegate(object sender, ItemEventArgs args)
  19. {
  20. Toast.MakeText(this, (args.Position + 1).ToString(), ToastLength.Short).Show();
  21. };
  22. }
  23. public class ImageAdapter: BaseAdapter
  24. {
  25. Context contextcreate;
  26. public ImageAdapter(Context a)
  27. {
  28. contextcreate = a;
  29. }
  30. public override int Count
  31. {
  32. get
  33. {
  34. return imageIds.Length;
  35. }
  36. }
  37. public override Java.Lang.Object GetItem(int position)
  38. {
  39. return null;
  40. }
  41. public override long GetItemId(int position)
  42. {
  43. return 0;
  44. }
  45. public override View GetView(int position, View convertView, ViewGroup parent)
  46. {
  47. ImageView imageView;
  48. if (convertView == null)
  49. {
  50. imageView = new ImageView(contextcreate);
  51. imageView.LayoutParameters = new GridView.LayoutParams(90, 90);
  52. imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
  53. imageView.SetPadding(10, 10, 10, 10);
  54. }
  55. else
  56. {
  57. imageView = (ImageView) convertView;
  58. }
  59. imageView.SetImageResource(imageIds[position]);
  60. return imageView;
  61. }
  62. int[] imageIds =
  63. {
  64. Resource.Drawable.mahesh,
  65. Resource.Drawable.dbeniwal321,
  66. Resource.Drawable.kartik,
  67. Resource.Drawable.mgold,
  68. Resource.Drawable.dhananjaycoder,
  69. Resource.Drawable.nandi,
  70. Resource.Drawable.praveen,
  71. Resource.Drawable.suthish,
  72. Resource.Drawable.nipuntomar,
  73. Resource.Drawable.dpatra,
  74. Resource.Drawable.jaganathan,
  75. Resource.Drawable.abhikumarvatsa,
  76. };
  77. }
  78. }
  79. }
Run the application your grid layout should look something like this:
Images in GridView
When you click on any image the position of the the image will show on the screen:
Display Image Index on click
Happy Learning.......