[Android] Get the tapped position (coordinates) on the screen

Overview

You may want to get the coordinates of the position tapped by the user on the screen of the Android device, such as when you have the stamp using static electricity pressed on the application and check the consistency of the stamp by collating the touched coordinates. .. In that case, you can get multiple coordinates of the tapped position by overriding the onTouchEvent or dispatchTouchEvent method of the Activity class.

example

Activity class


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        this.onTouchEvent(event);

        return super.dispatchTouchEvent(event);
    }

    private void onTouchEvent(MotionEvent motionEvent) {
        //Get the tapped position (excluding when you move your finger without releasing it)
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN || 
            event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
            StringBuilder builder = new StringBuilder();
            this.coordinates = new ArrayList<>();
            int count = event.getPointerCount();

            //Corresponds to when multiple places are tapped
            for (int i = 0; i < count; i++) {
                int x = (int) event.getX(i);
                int y = (int) event.getY(i);
                Logger.d("# X: " + x + ", y: " + y + ", PointerID: " + event.getPointerId(i));
                builder.append("(" + x + "," + y + "),");

                Coordinate coordinate = new Coordinate(x, y);
                this.coordinates.add(coordinate);
            }

            Logger.d("#Coordinate: " + builder.toString());
        }
    }

Coordinate class


public class Coordinate {
    private int x;
    private int y;

    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

Recommended Posts

[Android] Get the tapped position (coordinates) on the screen
[Android] Get the date on Monday
[Android] List all setting items on the setting screen
Get JUnit code coverage on Android.
Get the acceleration and bearing of the world coordinate system on Android
Try using the service on Android Oreo
Display an image on the base64 screen
How the website is displayed on the screen
Get your version number in the Android app
[Java] 4 steps to implement splash screen on Android
I want to place RadioButtons in the same RadioGroup at any position on the screen.
[Android] Solution when the camera cannot be started on Android 9
Import the instance and use it on another screen
[Android] Display images and characters on the ViewPager tab
[Android Studio] [Java] How to fix the screen vertically
Run the Android emulator on Docker using Android Emulator Container Scripts
Display the list in setDetails on the screen with spring-security
[Android] How to get the setting language of the terminal
[Introduction] Display Android Studio Hello World on the emulator
I can't get out of the Rails dbconsole screen
I want to simplify the log output on Android
Ruby/Racc: Get the position (row, column) where parsing failed
Until the Google Assistant sample runs on Android Things
I found no way to get the error code when I received an exception on Android
Access Web API on Android with Get and process Json (Java for the time being)