Detect Tap Count in Android

private int count = 0;
    private long startMillis=0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int eventaction = event.getAction();
        if (eventaction == MotionEvent.ACTION_UP) {

            //get system current milliseconds
            long time= System.currentTimeMillis();


            //if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
            if (startMillis==0 || (time-startMillis> 3000) ) {
                startMillis=time;
                count=1;
            }
            //it is not the first, and it has been  less than 3 seconds since the first
            else{ //  time-startMillis< 3000
                count++;
            }

            if (count==5) {
                 BrId = myPrefs.getInt("BrId", 0); // return 0 if someValue doesn't exist
                if(BrId != 0){
                    txtBrId.setText(String.valueOf(BrId));
                }
                txtBrId.setVisibility(View.VISIBLE);
                btnSaveBrId.setVisibility(View.VISIBLE);
                btnStartTracker.setVisibility(View.INVISIBLE);
            }
            return true;
        }
        return false;
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s