0

I am creating an app in which I am implementing Ontouchlistener, but I am not getting the correct code. How can I write the correct code for this?

Here is the code I have written for my activity using various tutorials.

public class ActivitySwipeDetector
  implements OnTouchListener
{
  private static final int MIN_DISTANCE = 100;
  private static final String logTag = "SwipeDetector";
  private float downX;
  private float downY;
  public Action mSwipeDetected = Action.None;
  private float upX;
  private float upY;

  public Action getAction()
  {
    return this.mSwipeDetected;
  }

  public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
  {
    switch (paramMotionEvent.getAction())
    {
    default:
    case 0:
    case 1:
    }
    float f2;
    do {
        do {
            float f1;
            do {
                //return false;
                this.downX = paramMotionEvent.getX();
                this.downY = paramMotionEvent.getY();
                this.mSwipeDetected = Action.None;
                // return false;
                this.upX = paramMotionEvent.getX();
                this.upY = paramMotionEvent.getY();
                f1 = this.downX - this.upX;
                f2 = this.downY - this.upY;
                if (Math.abs(f1) <= 100.0F)
                    break;
                if (f1 < 0.0F) {
                    Log.i("SwipeDetector", "Swipe Left to Right");
                    this.mSwipeDetected = Action.LR;
                    return false;
                }

            }
            while (f1 <= 0.0F);
            Log.i("SwipeDetector", "Swipe Right to Left");
            this.mSwipeDetected = Action.RL;
            return false;
        }
        while (Math.abs(f2) <= 100.0F);

        if (f2 < 0.0F) {
            Log.i("SwipeDetector", "Swipe Top to Bottom");
            this.mSwipeDetected = Action.TB;
            return false;
        }
    }
    while (f2 <= 0.0F);
    Log.i("SwipeDetector", "Swipe Bottom to Top");
    this.mSwipeDetected = Action.BT;
    return false;
  }

  public boolean swipeDetected()
  {
    return this.mSwipeDetected != Action.None;
  }

  public static enum Action
  {
      LR, RL, TB, BT, None;

      static
    {

      Action[] arrayOfAction = new Action[5];
      arrayOfAction[0] = LR;
      arrayOfAction[1] = RL;
      arrayOfAction[2] = TB;
      arrayOfAction[3] = BT;
      arrayOfAction[4] = None;
    }


  }
}

The problem I am currently encountering is in this code:

if (f2 < 0.0F) {
    Log.i("SwipeDetector", "Swipe Top to Bottom");
    this.mSwipeDetected = Action.TB;
    return false;
}

The compiler is saying "unreachable statement".

halfer
  • 19,824
  • 17
  • 99
  • 186
Param
  • 268
  • 1
  • 3
  • 12
  • and which error You get? – Opiatefuchs Feb 05 '15 at 12:51
  • @Opiatefuchs- showing error in this code `if (f2 < 0.0F) { Log.i("SwipeDetector", "Swipe Top to Bottom"); this.mSwipeDetected = Action.TB; return false; }` it saying unreachable statement – Param Feb 05 '15 at 12:59
  • Your code looks very confusing to me, I really con´t follow it. But what I think is, Your if statement is never reached because it is inside the do/while loops. Shouldn´t it be while statement? – Opiatefuchs Feb 05 '15 at 14:05
  • Unreachable statements breaks your code as the java compiler does not allow them. See [This question](http://stackoverflow.com/questions/3795585), and my answer below - you're doing too much work. – Felix Feb 05 '15 at 15:42

1 Answers1

0
  1. You should use existing constants to improve readability. See the documentation here.
  2. It seems you're doing too much work. Go over this tutorial (from Android's documentation) to see how you can achieve more with less code.
halfer
  • 19,824
  • 17
  • 99
  • 186
Felix
  • 1,034
  • 1
  • 9
  • 29
  • @Felix- Actually i want to know if this code is correct? – Param Feb 05 '15 at 12:48
  • @Param, your code is confusing and hard to read because of magic numbers such as "0" and "1" (over [MotionEvent.ACTION_DOWN](http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN) and ACTION_UP). Besides (and I hate myself for saying that), why would you want to implement a OnTouchListener yourself and not use a GestureDetector [as described in the official documentation](http://developer.android.com/training/gestures/detector.html)? I think you could do a lot more with less code if you use the right tools. Check out GestureDetector.OnGestureListener, it might help. – Felix Feb 05 '15 at 15:40