Display images inside a GridView in Android using VS 2010

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
  • First, create a new android application give the name to project like WelcomeGridView
  • Save the image files into the project's Resources/Drawable/ directory
  • Now Edit your Resources/Layout/Main.axml file
    1. <?xml version="1.0" encoding="utf-8"?>  
    2.    
    3. <GridView xmlns:android="http://schemas.android.com/apk/res/android"   
    4.       android:id="@+id/PhotoGridView"   
    5.       android:layout_width="fill_parent"   
    6.       android:layout_height="fill_parent"   
    7.       android:columnWidth="100dp"   
    8.       android:numColumns="auto_fit"   
    9.       android:verticalSpacing="15dp"   
    10.       android:horizontalSpacing="15dp"   
    11.       android:stretchMode="columnWidth"   
    12.       android:gravity="center"  
    13. />  
  • Now in OnCreate() method call the GridView from the layout using findViewById(int) and create a source for all items through which items to be displayed in the grid. One more interesting thing we do here, using setOnItemClickListener() method which is passed a new AdapterView.OnItemClickListener. This anonymous instance defines the onItemClick() callback method to show a Toast that displays the index position of the selected item.
    1. protected override void OnCreate(Bundle bundle) {  
    2.  base.OnCreate(bundle);  
    3.  SetContentView(Resource.Layout.Main);  
    4.  var PhotoGridView = FindViewById < GridView > (Resource.Id.PhotoGridView);  
    5.  PhotoGridView.Adapter = new ImageAdapter(this);  
    6.  PhotoGridView.ItemClick += delegate(object sender, ItemEventArgs args) {  
    7.   Toast.MakeText(this, (args.Position + 1).ToString(), ToastLength.Short).Show();  
    8.  };  

  • Create a new ImageView for each item referenced by the Adapter
    1. public override View GetView(int position, View convertView, ViewGroup parent)   
    2. {  
    3.  ImageView imageView;  
    4.  if (convertView == null)  
    5.  {  
    6.   imageView = new ImageView(contextcreate);  
    7.   imageView.LayoutParameters = new GridView.LayoutParams(9090);  
    8.   imageView.SetScaleType(ImageView.ScaleType.CenterCrop);  
    9.   imageView.SetPadding(10101010);  
    10.  }   
    11.  else   
    12.  {  
    13.   imageView = (ImageView) convertView;  
    14.  }  
    15.  imageView.SetImageResource(imageIds[position]);  
    16.  return imageView;  

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(9090);  
  52.     imageView.SetScaleType(ImageView.ScaleType.CenterCrop);  
  53.     imageView.SetPadding(10101010);  
  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. }  

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.......


Similar Articles