Android imageview long press and regular press

I have an image where I need to have both long clicks (for the context menu) and regular clicks. I can get someone to work, but not both. What am I missing? The code below only works for regular presses. As soon as I touch the screen, it will start executing the onTouch code.

 ImageView image = (ImageView)findViewById(R.id.visible_image);
 image.setLongClickable(true);
 image.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent ev) {
        switch (ev.getAction()) {
         case MotionEvent.ACTION_DOWN:
            // disable the screen taps for 500ms
            DecodeActionDownEvent(v, ev, bm2);
         case MotionEvent.ACTION_MOVE:
         case MotionEvent.ACTION_UP:
     }
        return true;
    }
 });
 registerForContextMenu(image);

...

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
     ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Edit");
    ArrayList<String> menuItems = new ArrayList<String>();
    menuItems.add("Edit page settings");
    menuItems.add("Edit page objects");
    menuItems.add("Edit this object");
    for (int i = 0; i<menuItems.size(); i++) {
        menu.add(Menu.NONE, i, i, menuItems.get(i));
    }
 }
+3
source share
3 answers

You can try to do it like this:

ImageView imageView = (ImageView) findViewById(R.id.ImageView);
imageView.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
                    //your stuff
        return true;
    }
});
imageView.setOnClickListener(new OnClickListener() {
    @Override
    public boolean onClick(View v) {
        //your stuff
        return true;
    }
});
+17
source

To get the x and y coordinates, set the touch listener

      ImageView.setOnTouchListener(new View.OnTouchListener()
      {
        public boolean onTouch(View v, MotionEvent event)
        {
           //And u can get x and y values like:

           x = event.getX(); 
           y = event.getY() ;
        }
      }
+2
source

, :

//Define these variables at the beginning of your Activity or Fragment:
private long then;
private int longClickDuration = 5000; //for long click to trigger after 5 seconds

...

ImageView imageView = (ImageView) findViewById(R.id.visible_image);
imageView.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          then = (long) System.currentTimeMillis();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
          if ((System.currentTimeMillis() - then) > longClickDuration) {
            /* Implement long click behavior here */
            System.out.println("Long Click has happened!");
            return false;
          } else {
            /* Implement short click behavior here or do nothing */
            System.out.println("Short Click has happened...");
            return true;
          }
        }
        return true;
      }
    });
+2

All Articles