Imagine you have created an Activity in an app. Let's see a useful example to make it more clear: we are creating a file explorer to our Android Device, it should look like the “Windows Explorer" in Windows (or “Nautilus" in Ubuntu), we have a space in the screen filled with files and folders. And we are going to add the following functionality:
-If we touch the screen, a pop-up will appear, giving us information about the folder we are in. (Useless but, this is just to show all event types in the same activity).
-If we press a key, (in the hardware keyboard) the focus will be moved to the folder/file with the starting letter we have pressed (this is useless, but just an example to learn about).
-If we move the Trackball, move the focus of the files/folders.
With this method, we will tell Android that we have touched the screen. We have to place this method in the Activity we are working on, in our case, in the file-browser Activity. Inside it, we can get the action we are doing on the touchscreen. Are we just touching it? Are we moving the finger?
Int action = event.getAction().
This action can be:
ACTION_MOVE -> We are moving the finger/pen in the screen.
ACTION_DOWN -> We have pushed the screen with a finger/pen.
ACTION_UP -> We have taken the finger/pen from the screen.
Using this we can write:
//Show the pop up
The other thing we can do here, is getting the coordinates of the screen, depending where we are touching. This is as easy as:
onKeyUp(int keyCode, KeyEvent event) / onKeyDown(int keyCode, KeyEvent event)
These methods are called when we press a key or we release it. The “int" parameter give us the code of the key pressed.
This is very similar to the “onTouch" event, but is just launched when the trackball is moved.