0

How do I modify this code below so that when a user double taps on the screen the apps/puzzle restarts? any help is much appreciated.

 return true; } public boolean onTouchEvent( MotionEvent event ) { detector.onTouchEvent( event ); return true; } private class DoubleTapHandler extends GestureDetector.SimpleOnGestureListener { public boolean onDoubleTapEvent( MotionEvent event ) { int touchY = ( int ) event.getRawY( ); // y coordinate of the touch within puzzleView is // touchY - actionBarHeight - statusBarHeight int index = puzzleView.indexOfTextView( touchY - actionBarHeight - statusBarHeight ); if( puzzleView.getTextViewText( index ) .equals( puzzle.wordToChange( ) ) ) puzzleView.setTextViewText( index, puzzle.replacementWord( ) ); return true; } } } 

2 Answers 2

1

Not a perfect solution but works :)

private fun setDoubleClick() { var numOfClicks = 0 [View].setOnClickListener { numOfClicks++ if (numOfClicks == 2) { //onDoubleClick numOfClicks = 0 } else { Handler().postDelayed({ numOfClicks = 0 }, 2000/*Set this according to desired doubleclick speed*/ ) } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

I’ll try it and see But I’m using java language ... I’m also new to android programming.
0
final GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { //doubletap method @Override public boolean onDoubleTap(MotionEvent e) { //put what do you want when double tap restartApplication(); return true; } @Override public void onLongPress(MotionEvent e) { super.onLongPress(e); } @Override public boolean onDoubleTapEvent(MotionEvent e) { return true; } @Override public boolean onDown(MotionEvent e) { return true; } }); 

Now attach the detector your view touch listener

yourView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch (View v, MotionEvent event){ return gestureDetector.onTouchEvent(event); } }); 

Here is the restart function

void restartApplication(){ Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage( getBaseContext().getPackageName() ); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); } 

Reference https://stackoverflow.com/a/39816509/12676247

3 Comments

Okay but i need to be able to doubleTap and app restarts automatically. is this formula similar to that as well?
edited the answer for restart Application when double-tap
Thank you so much for help, it has worked like a charm here. again thank you so much sir...I don't know how to thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.