How do I run an algorithm when the user stops touching the screen?

Asked

Viewed 1,410 times

4

I would like, when the user presses the screen, to open a horizontal menu with 4 options where the option to which he drops his finger on top will be selected.

The pressing event I already know. would be this:

btn_operacao.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            //algoritmo . . .
            return true;
        }
    });

What would be the event to catch the moment when the user removes his finger from the screen?

1 answer

4


That’s not what Onlongclicklistener’s for. It is for all steps that form a long click event (the one that is usually used to bring up the context menu).

You need to go to a lower level-use the Ontouchlistener, together with the actions ACTION_DOWN and ACTION_UP

minhaView.setOnTouchListener(new OnTouchListener () {
  public boolean onTouch(View view, MotionEvent event) {
    if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
      // mostre o menu que aparece quando o usuário toca na tela
    } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
      // o usuário soltou o toque. Execute a lógica de decidir qual opção foi selecionada
    }
    return true;
  }
});

It may also be interesting to treat the case of ACTION_CANCEL, which is when the user (somehow) canceled the tap without dropping his finger in your View. In that case, treat it as if no option had been chosen.

  • Thanks, it worked! Close there at the end of the comic with a ); Hug!

  • 1

    True. Fixed. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.