Android add onClick to dynamically created gallery item

I am trying to create a gallery that will show a picture fullscreen when you tap on the thumbnail.

I followed sort of a tutorial which nicely created the gallery, but I don't know how to give each image thumbnail an onClick event.

In my gallery page, inside the onCreate, I have this:

public class GalleryPage extends Activity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_page);
        ...
        this.imageGrid = (GridView) findViewById(R.id.gallery_container);
        this.bitmapList = new ArrayList<Bitmap>();

        File f = new File(latest_directory);
        File file[] = f.listFiles();
        for (int i=0; i < file.length; i++) {
            this.bitmapList.add(loadImage(latest_directory + "/" + file[i].getName()));
        }
        this.imageGrid.setAdapter(new ImageAdapter(this, this.bitmapList));
    }
}

Then, the ImageAdapter looks like this:

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Bitmap> bitmapList;

    public ImageAdapter(Context context, ArrayList<Bitmap> bitmapList) {
        this.context = context;
        this.bitmapList = bitmapList;
    }

    public int getCount() {
        return this.bitmapList.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(this.context);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageBitmap(this.bitmapList.get(position));
        return imageView;
    }
}

I was thinking I could just do:

imageView.setOnClickListener("onImageClick");

and have a method in my gallery page, but this throws an error.

How can I make it so when I click on any of the dynamically created gallery images (imageView), I can get the image location to pull the image and place it in a full size overlay.

Answers


Set an OnItemClickListener to the GridView.

imageGrid.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {

        //Selected bitmap
        Bitmap bitMap = bitmapList.get(position);

        //Do something with it
    }
});

Use setOnItemClickListener. If you want use your custom interface then follow this :

Create Interface

public interface ItemClickListener {
    void onItemClick(Bitmap t, int position);
}

In your adapter set Listener:

public class ImageAdapter extends BaseAdapter {
/// your code
private ItemClickListener listener;

public void setListener(ItemClickListener listener) {
        this.listener = listener;
    }
// your code
}

In getView method

 public View getView(int position, View convertView, ViewGroup parent) {
        // your code
        Bitmap bitmap =  this.bitmapList.get(position);
        imageView.setImageBitmap(bitmap );
        imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null)
                            listener.onItemClick(bitmap,position);
                    }
                });
        return imageView;
    }

That's it. Now implement this in your Activity.


Need Your Help

jquery radiobutton to trigger checkbox

javascript jquery html

So I have some radiobutton and when I click some of radio button it also giving check into some checkbox, how I can do it in web programming?