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 Tag


or you can run this command

git clone –branch


Code Samples

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

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

Adding View.OnLongClickListener to ImageView

  protected void onCreate(Bundle savedInstanceState) {

      mImageView = (ImageView) findViewById(;
      mImageView.setOnLongClickListener(new View.OnLongClickListener() {
          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);
      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>