View Ravichandran J.V.'s profile on LinkedIn RJV 's Blog: Get image clicked in a grid - Android Tweet @ravichandranjv


Wednesday, 6 February 2013

Get image clicked in a grid - Android

I have titled these Android related posts to match Google searches as the many results I found on the web are pretty complicated.

Here is the simple way using the AdapterView. Extend your class with BaseAdapter that gives you a getView() method to implement your view in a Grid.

// The main activity class

import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
 ImageView v,v1;
 protected void onCreate(Bundle savedInstanceState) {
  GridView gv=(GridView) findViewById(;
  gv.setAdapter(new ImageCellAdapter(this));
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(, menu);
  return true;

 public void onClick(View v) {
  String msg=" " + v.getId();
  Toast.makeText(this, "Clicked "+msg,Toast.LENGTH_SHORT).show();
// The adapter class

import android.content.Context;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
public class ImageCellAdapter extends BaseAdapter
public static final int DEFAULT_NUM_IMAGES = 8;
public ViewGroup mParentView = null;
private Context mContext;
public ImageCellAdapter(Context c)
    mContext = c;
public int getCount()
    return mThumbIds.length;
public Object getItem(int position)
    return mThumbIds[position];
public long getItemId(int position) {
    return position;
public View getView (int position, View convertView, ViewGroup parent)
    mParentView = parent;

    ImageCell v = null;
    if (convertView == null) {
        v = new ImageCell (mContext);       
    } else {
        v = (ImageCell) convertView;
    v.setImageResource (mThumbIds[position]);
    v.cellNumber = position;
    v.setOnClickListener ((View.OnClickListener) mContext);
    return v;
public Integer[] mThumbIds = {
        R.drawable.r12, R.drawable.t12,
        R.drawable.b12, R.drawable.k12,
        R.drawable.f12, R.drawable.l12,
        R.drawable.o12, R.drawable.m12,
        R.drawable.p12, R.drawable.s12,
        R.drawable.c12, R.drawable.a12,
        R.drawable.h12, R.drawable.e12,
        R.drawable.g12, R.drawable.d12,
        R.drawable.i12, R.drawable.w12,
        R.drawable.n12, R.drawable.y12,
        R.drawable.z12, R.drawable.u12,
        R.drawable.q12, R.drawable.v12,
        R.drawable.j12, R.drawable.x12,
// The image cell class

public class ImageCell extends ImageView
//implements OnClickListener
//    public boolean cellEmpty = true;
    public int cellNumber = -1;
  //  public GridView gv;
public ImageCell (Context context) {
 super (context);
public ImageCell (Context context, AttributeSet attrs) {
 super (context, attrs);
public ImageCell (Context context, AttributeSet attrs, int style) {
 super (context, attrs, style);
// The activity XML


LinearLayout xmlns:android=





<GridView xmlns:android=""