How to determine a long touch on android?

Viewed 24926

I am looking for a way for when a user long touches a mapview (lets say for 1000ms) that i can some how do a certain action.

How would i go about judging how long a user long touches a mapsview(or any view).

It would be similar to android google maps app, when you long touch, it brings up a balloon overlay item.

Edit added

mapView.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {

            Toast.makeText(mapView.getContext(), "Hello 123", 2000);
            return false;
        }
    });

the above does not work... any ideas why?

Edit added

This is what i am trying at the moment, but it does not seem to work, even if i only press on the phone, it says the event is an action_move,

i am using an inner class in my MapActivity

    private long startTime=0;
private long endTime=0;

class MapOverlay extends Overlay {



    @Override
    public boolean onTouchEvent(MotionEvent ev, MapView mapView) {

        if(ev.getAction() == MotionEvent.ACTION_DOWN){
             //record the start time
             startTime = ev.getEventTime();

             Log.d("LC", "IN DOWN");
          }else if(ev.getAction() == MotionEvent.ACTION_UP){
             //record the end time
             endTime = ev.getEventTime();
             Log.d("LC", "IN UP");
          }else if(ev.getAction() == MotionEvent.ACTION_MOVE){
              Log.d("LC", "IN move");
              endTime=0;
          }

          //verify
          if(endTime - startTime > 1000){
             //we have a 1000ms duration touch
             //propagate your own event
              Log.d("LC", "time touched greater than 1000ms");
              Toast.makeText(getBaseContext(), "Hello 123", Toast.LENGTH_SHORT).show();
              startTime=0; 
              endTime=0;
             return true; //notify that you handled this event (do not propagate)
          }

        return false;//propogate to enable drag

    }

}

and here is my error log that does not make any sense to me

06-29 14:29:55.509: DEBUG/LC(7693): IN move
06-29 14:29:56.149: DEBUG/LC(7693): IN UP
06-29 14:29:56.149: DEBUG/LC(7693): 6346707 6349261
06-29 14:29:56.149: DEBUG/LC(7693): time touched greater than 1000ms

the end time should be set to zero...but it is not...any idea why?

6 Answers

This is how I use long and short touches

View.setOnTouchListener(new View.OnTouchListener() {
        double firsttouch=0;
        boolean isup=false;
        int millistotouch=1000;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction()==MotionEvent.ACTION_DOWN) {
                firsttouch = (double) System.currentTimeMillis();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!isup) {
                             //
                             // Do your long click work
                             //
                            }
                            else
                            {
                                firsttouch=0;
                                isup=false;
                            }
                        }
                    }
                }, millistotouch);
                return true;
            }
            if (motionEvent.getAction()==MotionEvent.ACTION_UP) {
                if (((double) System.currentTimeMillis()-firsttouch)<millistotouch)
                {
                    isup=true;
                    //
                    // Do your short click work
                    //
                }

            }
            return false;
        }
    });
Related