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

4

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

The press 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?

    
asked by anonymous 16.01.2018 / 00:13

1 answer

4

The OnLongClickListener does not work. It is for all the steps that form a long click event (the one that is used to bring up the context menu).

You need to move to a lower level - use the OnTouchListener , in set with 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 handle the ACTION_CANCEL case, which is when the user (somehow) has cleared the touch without touching your View. In this case, treat as if no option has been chosen.

    
16.01.2018 / 00:18