Search code examples
javaandroid-studioeventsscreentouch-event

OnTouchEvent reacts multiple while touching the screen


I've got the problem, that the onTouchEvent reacts to staying with the finger on screen and move the finger. That isn`t my aim. I want, that the OnTouchEvent only reacts if i tap on the screen, after this the OnTouchEvent should reactivate itself and the user have to click another time.

Anyone can solve my problem? I mean it's a little bit tricky with my timer, because i never leave the method OnTouchEvent.

public boolean onTouchEvent(MotionEvent me){
    if(!started){
        started = true;
        taptostart.setVisibility(View.GONE);
        expla.setVisibility(View.GONE);
        //start the timer
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        changePos();
                    }
                });
            }
        }, 0, 20);
    }else{
        activateHo();
    }
    return false;
}

Solution

  • public boolean onTouchEvent(MotionEvent me){
        if(me.getAction() == MotionEvent.ACTION_DOWN){
           //Do what you want here, when the view is touched
        }
    return false;
    }