Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting swipes, click, hold on one View Android

I have an ImageView which fills my whole Activity. I need to be able to detect 4 touch events :

  • Hold (longer than 400 ms)
  • Click
  • Swipe Left
  • Swipe Right

At the moment I am able to detect the first two using the following code :

imageView.setOnTouchListener(new View.OnTouchListener() {

    private Timer timerr = new Timer();
    private long LONG_PRESS_TIMEOUT = 400; // TODO: your timeout here
    private boolean wasLong = false;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
      Log.d(getClass().getName(), "touch event: " + event.toString());

      if (event.getAction() == MotionEvent.ACTION_DOWN) {
       // HOLD DETECTED
       timerr.schedule(new TimerTask() {
          @Override
          public void run() {
            SlideShow.this.runOnUiThread(new Runnable() {
              @Override
              public void run() {

              }, LONG_PRESS_TIMEOUT);
             return true;
            }

            if (event.getAction() == MotionEvent.ACTION_UP) {
             if (!isPaused && !wasLong) {
              // CLICK DETECTED
              timerr.cancel();
              timerr = new Timer();
              if (!wasLong) {

               return false;
              }
             });

Now I am trying to use the code from this question to implement the swipe right and swipe left actions. Android Swipe on List The problem with this is that this works on listview, where you can implement an onTouchListener for the whole listview, and a seperate onItemClickListener for the list item. I do not know how to adapt this to this situation though where only one Listener is available.

like image 720
Alk Avatar asked Mar 04 '16 03:03

Alk


1 Answers

Gesture Detector is the way to go.

Alternatively,

 image.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    x1 = event.getX();
                    y1 = event.getY();
                    t1 = System.currentTimeMillis();
                    return true;
                case MotionEvent.ACTION_UP:
                    x2 = event.getX();
                    y2 = event.getY();
                    t2 = System.currentTimeMillis();

                    if (x1 == x2 && y1 == y2 && (t2 - t1) < CLICK_DURATION) {
                        Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
                    } else if ((t2 - t1) >= CLICK_DURATION) {
                        Toast.makeText(getActivity(), "Long click", Toast.LENGTH_SHORT).show();
                    } else if (x1 > x2) {
                        Toast.makeText(getActivity(), "Left swipe", Toast.LENGTH_SHORT).show();
                    } else if (x2 > x1) {
                        Toast.makeText(getActivity(), "Right swipe", Toast.LENGTH_SHORT).show();
                    } 


                    return true;
            }

            return false;
        }
like image 164
Neo Avatar answered Oct 21 '22 08:10

Neo