-1

I want to implement touch listener that detects

  1. Click/tap on chess piece in order to select ot
  2. Swipe/move of chess piece immediately.

How to do it?

cubesoft
  • 3,448
  • 7
  • 49
  • 91
  • Does this answer your question? [How to handle drag in an android chess app?](https://stackoverflow.com/questions/36888139/how-to-handle-drag-in-an-android-chess-app) – javdromero Oct 06 '22 at 14:17

2 Answers2

0

Unfortunately, I am not sure what you mean by a "chess piece", but let's assume that the board is a view in the background, and inside you have inner views which are chess pieces.

  1. To achieve the select/unselect given piece effect you should add a simple View.OnClickListener interface to your view. Each of your pieces should have a view class and some data connected to it (like a position, a type of piece, id, etc.). After clicking the piece update the state of some global variable, for example, selectedPiece with an ID assigned to a given piece. You should also change the UI of a given piece, for example change the border to show the selected piece to a user. Then after clicking a new field for moving the piece you should update the UI by moving your piece view to an appropriate position. Also, the data of a piece or a game should be updated - depending on the place of storing the pieces' positions.
  2. For this you should implement the drag&drop feature which is described here: https://www.tutorialspoint.com/android/android_drag_and_drop.html. You should determine the places where the user can move a piece and in case of dropping the piece in the wrong place, cancel the move and go with the piece to a start position.
Patryk Kubiak
  • 1,679
  • 2
  • 11
  • 17
  • I totally understand you but the problem is to distinguish between click and drag start. I want to start drag when the user moves the finger on the view but this interferes with the click handler. – cubesoft Oct 10 '22 at 13:56
  • https://stackoverflow.com/questions/14419865/android-distinguishing-between-click-and-drag-dispatchtouchevent This should help. If the answer with flag `isClick` doesn't work then try setting a threshold area between the click position to detect if the user want to move or click given element – Patryk Kubiak Oct 11 '22 at 12:05