14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

❑ onKeyUp Called when a user releases a pressed key<br />

❑ onTrackballEvent Called when the device’s trackball is moved<br />

Chapter 4: Creating User Interfaces<br />

❑ onTouchEvent Called when the touch screen is pressed or released, or it detects movement<br />

The following code snippet shows a skeleton class that overrides each of the user interaction handlers<br />

in a View:<br />

@Override<br />

public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {<br />

// Return true if the event was handled.<br />

return true;<br />

}<br />

@Override<br />

public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {<br />

// Return true if the event was handled.<br />

return true;<br />

}<br />

@Override<br />

public boolean onTrackballEvent(MotionEvent event ) {<br />

// Get the type of action this event represents<br />

int actionPerformed = event.getAction();<br />

// Return true if the event was handled.<br />

return true;<br />

}<br />

@Override<br />

public boolean onTouchEvent(MotionEvent event) {<br />

// Get the type of action this event represents<br />

int actionPerformed = event.getAction();<br />

// Return true if the event was handled.<br />

return true;<br />

}<br />

Further details on using each of these event handlers, including greater detail on the parameters<br />

received by each method, are available in Chapter 11.<br />

Creating a Compass View Example<br />

In the following example, you’ll create a new Compass View by extending the View class. It uses a traditional<br />

compass rose to indicate a heading/orientation. When complete, it should appear as in Figure 4-3.<br />

A compass is an example of a User Interface control that requires a radically different visual display<br />

from the textboxes and buttons available in the SDK toolbox, making it an excellent candidate for<br />

building from scratch.<br />

In Chapter 10, you’ll use this Compass View and the device’s built-in accelerometer to display the<br />

user’s current bearing. Then in Chapter 11, you will learn some advanced techniques for Canvas drawing<br />

that will let you dramatically improve its appearance.<br />

93

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!