Android TouchEvent Location Y Value Greater than Screen Height When Bottom Of Screen Clicked

70 views Asked by At

I am working on an application that is a kiosk and I am trying to create a behavior that when the user clicks all 4 corners of the screen, I run code to exit kiosk mode. I got most of the code setup and working but am running into an issue where two of the conditions are not being called. After debugging, I noticed that the MouseEvent is returning a y value that is greater than the screen heigh and I cannot determine why. A click on the top right and left corners of the screen register good but the bottom is where the issue is arising. Is there something I am missing.

I tried to use this example to find the bounds but ama little confused as maxY seems to be on the bottom of the screen. It seems that the maxY validation is failing because even though the screen maxY is 736, my y values from the touch are showing higher than that. Can someone tell me what I am missing or doing wrong?

My layouts bounds are set to match width and match height.

@NotNull
private View.OnTouchListener setupMotionListener() {
    return new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            int count = arg1.getPointerCount();
            if(count == 4) {
                for (int i = 0; i < count; i++) {
                    x[i] = arg1.getX(i);
                    y[i] = arg1.getY(i);
                }
                Display mdisp = getWindowManager().getDefaultDisplay();
                Point mdispSize = new Point();
                mdisp.getSize(mdispSize);
                int maxX = mdispSize.x;
                int maxY = mdispSize.y;

                for (int i = 0; i < count; i++) {
                    if (x[i] >= 0 && x[i] <= 100 && y[i] >= 0 && y[i] <= 100) {
                        closeClicked.set(0, true);
                    } else if (x[i] >= 0 && x[i] <= 100 && y[i] <= maxY && y[i] >= (maxY - 100)) {
                        closeClicked.set(1, true);
                    } else if (x[i] <= maxX && x[i] >= (maxX - 100) && y[i] >= 0 && y[i] <= 100) {
                        closeClicked.set(2, true);
                    } else if (x[i] <= maxX && x[i] >= (maxX - 100) && y[i] <= maxY && y[i] >= (maxY - 100)) {
                        closeClicked.set(3, true);
                    }

                }

                if (!closeClicked.contains(false)) {
                    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("isKioskEnabled", false);
                    editor.commit();
                    Intent kiosk = new Intent(KioskActivity.this, MainActivity.class);
                    kiosk.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplicationContext().startActivity(kiosk);
                } else {
                    closeClicked = Arrays.asList(false, false, false, false);
                    x = new float[4];
                    y = new float[4];
                }
            } else {
                x = new float[4];
                y = new float[4];
            }
            return true;
        }
    };
}

my maxX or screenWidth is:1280 my maxY or screenHeight is:736

my y values from touching the 4 corners are (notice touch event is greater than screen height): y values for 4 corners

similarly, my x values on all 4 corners are: x values for 4 corners

1

There are 1 answers

0
Cloverleaf On

Please note that mdispSize.y does not return the screen height, but the usable screen height, i.e. screen height minus height of the navigation bar. So if you click on the two lowest corners of the screen (i.e. on the navigation bar), then indeed arg1.getY(...) > mdispSize.y.

Use

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
maxY = displayMetrics.heightPixels;

instead to get the full screen height.