2

Is there a way where I can add a listener to a layout or view and as a user touches the screen it will add the count. Something like

    tvTouchCount.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int count = 0;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    count = count + 1;
                    if (count > maxCount)
                        maxCount = count;
                    tvTouchCount.setText("Max Count=" + maxCount);
                    return false;
                case MotionEvent.ACTION_UP:
                    count = count - 1;
                    return false;
                default:
                    return false;
            }
        }
    });

I've tried getting the details from the package manager but that only tells me if it supports 2 , 2-5, and 5+ independent touchs and not the exact number

callMeRoka
  • 46
  • 5
  • Yes OnTouchListener can be applied to any view – asok Buzz Feb 22 '17 at 19:57
  • Possible duplicate of [How to use View.OnTouchListener instead of onClick](http://stackoverflow.com/questions/11690504/how-to-use-view-ontouchlistener-instead-of-onclick) – Drew Szurko Feb 22 '17 at 19:58
  • My question isn't how to use the ontouch listener but getting the exact number of maximum supported independent touch on a device. – callMeRoka Mar 01 '17 at 03:49

1 Answers1

0

You can get the pointers count from the motion event.

new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int pointersCount = event.getPointerCount()
    }
}

As concerns the maximum number of available touches, you can check it via PackageManager.

PackageManager packageManager = context.getPackageManager()
if (packageManager.hasSystemFeature(FEATURE_TOUCHSCREEN_MULTITOUCH)) {
    //The device's supports basic two-finger gesture detection.
}

if (packageManager.hasSystemFeature(FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) {
    //The device's capable of tracking two or more fingers fully independently.
}

if (packageManager.hasSystemFeature(FEATURE_TOUCHSCREEN_MULTITOUCH_JAZZHAND)) {
    //The device's is capable of tracking a full hand of fingers fully independently.
    //that is, 5 or more simultaneous independent pointers.
}
Alex Kamenkov
  • 891
  • 1
  • 6
  • 16