Android image viewer adding long click press

The android image viewer adding long click press tutorial is the sixth part to the android image viewer tutorial series.

This is a continuation of the android image view series where we will now be adding more advanced features to the android image viewer.

The first feature we will be focusing on is pinch zoom. To make a start on that feature we will be adding a android View.OnLongClickListener to the android ImageView.

The reasoning behind this is that the full size image must be loaded to a ImageView prior to zooming in. And this full size image will be loaded via a long click (press) to the ImageView.

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/android_image_viewer.git Tag

image-viewer-longclicklistener

or you can run this command

git clone https://github.com/mobapptuts/android_image_viewer.git –branch

image-viewer-longclicklistener

Code Samples

Change the values xml file to increase the margin padding for the thumbnail image

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">100dp</dimen>
    <dimen name="activity_vertical_margin">100dp</dimen>
</resources>

Adding View.OnLongClickListener to ImageView

@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_image_view_main);

      mImageView = (ImageView) findViewById(R.id.imageView);
      mImageView.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
              Toast.makeText(getApplicationContext(), "ImageView long pressed!", Toast.LENGTH_SHORT).show();
              return true;
          }
      });

      Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
      intent.addCategory(Intent.CATEGORY_OPENABLE);
      intent.setType("image/*");
      startActivityForResult(intent, REQUEST_OPEN_RESULT_CODE);
  }

 

Android image viewer adding long click press Summary

In this tutorial we learned that is fast and straight forward to add an View.OnLongClickListener to a view type including the ImageView.

We also showed how to adjust the size of an ImageView by changing the size of its padding.

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>