Translate

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.app.Activity;
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;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity implements OnClickListener {
 ImageView v,v1;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  GridView gv=(GridView) findViewById(R.id.image_grid_view);
  gv.setAdapter(new ImageCellAdapter(this));
       }
  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
 @Override
 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.setId(position);
   //v.setLayoutParams(LayoutParams.WRAP_CONTENT));
    v.setOnClickListener ((View.OnClickListener) mContext);
    return v;
}
public Integer[] mThumbIds = {
  R.drawable.ic_launcher, R.drawable.re,
        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=http://schemas.android.com/apk/res/android

android:layout_width="fill_parent"

android:layout_height="fill_parent" 

>

 

<GridView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/image_grid_view"

android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:numColumns="9"

/>

 

</LinearLayout>