How to interact with Android Events. A Simple Guide for Google Android Programmers

Page content


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…)

Activity Events

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.

onTouchEvent(MotionEvent event)

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:

if(action =MotionEvent.ACTION_DOWN){

//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.

onTrackBallEvent(MotionEvent event)

This is very similar to the “onTouch” event, but is just launched when the trackball is moved.

Item Events

This Event is specific to a concrete item or view. What happens when we have too many buttons on the screen and they have different functionality? How do we deal with these events?

This is as simple as:

Button button = (Button)findViewByID(;

button.setOnClickListener(new Button.OnClickListener() {

public void onClick (View v){

//Do action



We are setting a Listener to this concrete Button. Remember that we have to create the Button in the XML file and give it an Identifier.

Follow up

If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev

This post is part of the series: How to- Develop Applications on Android: More Functionality

This time we will learn more advanced functionality to add to our Android Applications.

  1. Guide to Creating Android App Menus
  2. How to create Advanced Menus Via Our Google Android Applications
  3. Guide to Using Basic Content Providers in Android Development
  4. Dev Guide to Advanced Content Providers on Android
  5. How to interact with Android Events