Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android (x,y) continuous touch (drag) coordinates

I'm new to android, and I've been trying for a while to find out how can I retrieve the coordinates of a continuous touch on the screen. for example have 2 vars (x,y) that update in real time as finger moves around. I got it how to find it when the touch is made, but I really don;t get it how to make it return the result after that , when the finger is moving.

I've been trying switch statements, while/for loops in different combination with ACTION_MOVE./ UP/ DOWN .. .still nothing.

I've found like the same question on the website, but the answers only fit for the first step(showing the coordination from the touch only) I'd really appreciate a solution to this! Thanks!

like image 377
user1422535 Avatar asked May 28 '12 23:05

user1422535


2 Answers

Without seeing your code I'm just guessing, but essentially if you don't return true to the first call to onTouchEvent, you won't see any of the subsequent events in the gesture (MOVE, UP, etc).

Maybe that is your problem? Otherwise please put code sample up.

like image 185
Tim Avatar answered Oct 04 '22 22:10

Tim


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TextView xCoord = (TextView) findViewById(R.id.textView1);
    final TextView yCoord = (TextView) findViewById(R.id.textView2);

    final View touchView = findViewById(R.id.textView3);
    touchView.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction();
            switch (action & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN: {
                    xCoord.setText(String.valueOf((int) event.getX()));
                    yCoord.setText(String.valueOf((int) event.getY()));
                    break;
                }

                case MotionEvent.ACTION_MOVE:{
                    xCoord.setText(String.valueOf((int) event.getX()));
                    yCoord.setText(String.valueOf((int) event.getY()));
                    break;
                }
            }
            return true;

        }

    });
}
like image 38
user1422535 Avatar answered Oct 04 '22 20:10

user1422535