Search code examples
androidxmlpositiontouchpixel

How to get position of pixel touched in android studio xml?


I'm trying to get the actual touch pixel of a background image of an android studio xml. I'm trying to start a new activity when touch position is in bounds of desired coordinates. Thank you!


Solution

  • you can set onTouchListener() on your layout with background image.

    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
    mainLayout.setOnTouchListener(this); // in this case activity should implements  View.OnTouchListener
    

    Then use something like that

    public boolean onTouch(View v, MotionEvent event) {
        Point touchPlace = new Point();
        touchPlace.set(event.getX(), event.getY());
    }