Search code examples
androidontouch

Get Position from OnTouchEvent


I used on touch event to find the touched position by the user.I used the following code

@Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
              float x=event.getX();
              float y=event.getY();

              Log.d("MULTIPLE", "X=" + x + " Y=" + y);
         }
        return true;
     }

And according to the position some methods are called.

When i tested in different devices the x and y values changed.Is there any way to get a unique value for x and y across all devices


Solution

  • I solved this in the following way

    The mid point of the device can be find out this way

    X=devicewidth * 0.5

    Y=deviceHeight * 0.5

    Call 0.5 as heightFactor and widhtFactor

    Any other points in the device have its own heightFactor and widhtFactor

    We get X and Y value from onTouch event (event.getX() and event.getY())

    So to find heightFactor and widhtFactor

    heightFactor =X/deviceHeight

    widhtFactor=Y/devicewidth

    thus we get heightFactor and widhtFactor for each point which will be same for all device