android camera2 api recyclerview onclick

android camera2 api recyclerview o'clock

Implementing an interface to get the onClick position from the ImageAdapter to the activity

This tutorial explains how to setup the android camera2 api recyclerview onclick position so we can find the location of the image file.

Get Code

The code has been uploaded to github here TAG camera2-recyclerview-onclick or

else you can run this command

git clone –branch camera2-recyclerview-onclick¬†

Code Samples

Create the interface file



 * Created by nigelhenshaw on 28/09/2015.
public interface RecyclerViewClickPositionInterface {
    public void getRecyclerViewAdapterPosition(int position);


Implement the callback

public class CamaraIntentActivity extends Activity implements RecyclerViewClickPositionInterface {
  public void getRecyclerViewAdapterPosition(int position) {

      Toast.makeText(this, Integer.toString(position), Toast.LENGTH_SHORT).show();


Add the interface to the ImageAdapter call

   protected void onCreate(Bundle savedInstanceState) {


       mRecyclerView = (RecyclerView) findViewById(;
       GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
       RecyclerView.Adapter imageAdapter = new ImageAdapter(sortFilesToLatest(mGalleryFolder), this);

       final int maxMemorySize = (int) Runtime.getRuntime().maxMemory() / 1024;
       final int cacheSize = maxMemorySize / 10;

       mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {

           protected int sizeOf(String key, Bitmap value) {
               return value.getByteCount() / 1024;

       mTextureView = (TextureView) findViewById(;
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
     if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
         RecyclerView.Adapter newImageAdapter = new ImageAdapter(sortFilesToLatest(mGalleryFolder), this);
         mRecyclerView.swapAdapter(newImageAdapter, false);

private void swapImageAdapter() {
    RecyclerView.Adapter newImageAdapter = new ImageAdapter(sortFilesToLatest(mGalleryFolder), this);
    mRecyclerView.swapAdapter(newImageAdapter, false);


Create the interface member in the ImageAdapter & initialise the member inside the constructor

  private static RecyclerViewClickPositionInterface mPositionInterface;

public ImageAdapter(File[] folderFiles, RecyclerViewClickPositionInterface positionInterface) {
      mPositionInterface = positionInterface;
      mImageFiles= folderFiles;

Add the onClickListener to the ViewHolder & call the interface

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
       private ImageView imageView;

       public ViewHolder(View view) {

           imageView = (ImageView) view.findViewById(;

       public ImageView getImageView() {
           return imageView;

       public void onClick(View v) {
About The Author

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>