0

Here is the problem...

What I Try to do :

I have a button in my Fragment, the user presses it.

And I would like to know when user's finger moves out of the button.

What I have managed to do :

private Button btn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...

    btn = (Button) view.findViewById(R.id.btn);
    btn.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // TODO do something
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                checkIfFingerStillOnButton(event);
            }
            return true;
        }
    });
}

public void checkIfFingerStillOnButton(MotionEvent event) {
    boolean result = false;

    // Button limits
    float coordLeft = btn.getLeft();
    float coordRight = btn.getRight();
    float coordTop = btn.getTop();
    float coordBot = btn.getBottom();

    // Finger coordinates
    float X = event.getX();
    float Y = event.getY();

    if (X>coordLeft && X<coordRight && Y<coordTop && Y>coordBot) { result = true; }
}

Result so far :

The coordinates returned by those functions aren't what I expected.

Here is what I get when clicking in the middle of the button :

Left=312.0, Right=672.0, Top=670.0, Bottom=1030.0, 
X=189.0, Y=194.17029 | result=false

Thank you for your help !

Anoop Kanyan
  • 618
  • 7
  • 19
Fundhor
  • 3,369
  • 1
  • 25
  • 47

2 Answers2

1

First, You're wrong in your last test. You have to do this comparison :

X>coordLeft && X<coordRight && Y>coordTop && Y<coordBot

Second, I think the coordinates are relative to the button and no the Fragment. Try to use getRawX() and getRawY() for values relative to your device.

Benoît Verhaeghe
  • 612
  • 1
  • 6
  • 21
  • RawX and RawY seems to actually give me the good position relative to device. Still the result is not as expected "Left=312.0, Right=672.0, Top=670.0, Bottom=1030.0, X=542.0, Y=1140.9623 | result=false". It seems like Left/Right/Top/Bottom have another referential. (My test was indeed wrong, ty) – Fundhor Jan 10 '16 at 17:00
  • May you can use `getLocationOnScreen (int[] location)` to get the position of your button relative of your screen and use `getWidth()` and `getHeight()` to get the right and the bottom limit/boder of the button – Benoît Verhaeghe Jan 10 '16 at 17:10
  • It's actually the way I solved it yes, thanks for your help. – Fundhor Jan 10 '16 at 17:22
0

Here is the way I solved it :

boolean result = false;
int[] location  = new int[2];
btnStartGame.getLocationOnScreen(location);

// Finger coordinates
float X = event.getRawX();
float Y = event.getRawY();

// Button limits
float coordLeft = location[0];
float coordRight = location[0] + btnStartGame.getWidth();
float coordTop = location[1];
float coordBot = location[1] + btnStartGame.getHeight();

if (X>coordLeft && X<coordRight && Y>coordTop && Y<coordBot) {
    result = true;
    btnStartGame.setText("true");
} else {
    btnStartGame.setText("false");
}

I hope it will help someone else.

Community
  • 1
  • 1
Fundhor
  • 3,369
  • 1
  • 25
  • 47