Recipe Reacting to Touch Events

Any interaction with the screen, be it a touch or a navigated selection using the trackball, is an interaction with the corresponding view at that location. Because the screen layout is a hierarchy of views, as described in Chapter 4, the system starts at the top of this hierarchy and passes the event down the tree until it is handled by a view. Some events, if not consumed, can continue to pass down the tree after being handled.

Listing 5.9 shows a button called ex_button that handles both a click and a long click (press and hold) by setting two event listeners. When the event occurs, the corresponding callback method is called and displays a Toast to the screen to show the method was triggered.

Listing 5.9 src/com/cookbook/touch_examples/ package com.cookbook.touch_examples;


import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnLongClickListener;

import android.widget.Button;

import android.widget.Toast;

public class TouchExamples extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Button ex = (Button) findViewById(;

ex.setOnClickListener(new OnClickListener() { public void onClick(View v) {

Toast.makeText(TouchExamples.this, "Click", Toast.LENGTH_SHORT).show();

ex.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) {

Toast.makeText(TouchExamples.this, "LONG Click",

Toast.LENGTH_SHORT).show(); return true;

The layout providing the button is given in Listing 5.10.

Listing 5.10 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=""

android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/ex_button" android:text="Press Me" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

For compactness, this callback method is defined in place in Listing 5.9, but it can also be defined explicitly for readability and reusability:

View.OnClickListener myTouchMethod = new View.OnClickListener() { public void onClick(View v) {

//insert relevant action here


Another way is to have the activity implement the OnClickListener interface.Then, the method is at the activity level and avoids an extra class load:

public class TouchExamples extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Button ex = (Button) findViewById(; ex.setOnClickListener(this);

public void onClick(View v) {

if(v.getId() == { // insert relevant action here

This implementation of the onClick() method at the activity level helps to show how a parent view can handle touch events for multiple children.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment