written by: Jbeerdev•edited by: Simon Hill•updated: 7/1/2010
One of the best hardware characteristics of Android is the big touchscreen offered to users. But how do we interact with it? How do we tell Android that we have touched the screen? When programming in the Google Android environment we use "Events." Keep reading to learn more.
slide 1 of 4
In older mobile devices the only way to interact with the software was using buttons on the device. Some of them had “voice recognition", but it was not very efficient. Newer devices on the other hand have wide and touchable screens, that's the case with Android, specifically on the G1 we find a touchable screen and a QWERTY keyboard, with the G2 (HTC Magic) there is no QWERTY keyboard or dial keys ALA the iPhone design concept. Having a look at it, we can only see 5 buttons (Turn on, Turn off-Power, Home, Back and Menu buttons) and a Trackball . Where are the dial-buttons? How can we write an SMS? The answer is: using the touchscreen.
The Android G2 has a “software keyboard" implemented on it: when we want to write a SMS, to create a contact or just to surf the Internet, a little keyboard appears on the screen, if you have thin fingers, you could write anything you want without any problem... if your fingers are more thick...you might have more problems.
Let's see on a small scale, how this works.
We'll divide the handling of the events in two ways: How to handle events that happen in the whole activity (let's describe them in this way) and handle “local" events, specific for concrete views or widgets (buttons, lists, images...)
slide 2 of 4
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: