ANDROID EVENT
HANDLING
L. Grewe
Widget : Views that have
events
For a list of the widgets provided by Android,
see the [Link] package.
Some Examples
Button
CheckBox
DatePicker
EditText
ImageView
SearchView
Spinner
Event Handling
Decide what Widgets whos events to process
Define an event listener and register it
with the View.
[Link] (for handling "clicks" on a
View), [Link] (for handling touch
screen events in a View), and
[Link] (for handling device key
presses within a View)
[Link]
cs/ui/[Link] details more
Using Eclipse --lets recall our example
from our starting lecture
Hello World Project
Lets add a button
Lets add a Button to a
program-based interface
Step 1: Add button
Step 2: Register Event Handler
TWO
OPTIONS separate class to handle
event(s), OR have the Activity containing the
button do the event handling
Step 3: Implement Event Handlerfor a
Button means implementing the
[Link] interface
Event handling done by
Activity itself
Here code to handle is inside Activity itself
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById([Link]); //STEP 1
[Link](this); //STEP 2 - registration
}
// Implement the OnClickListener callback
//STEP 3 event handler
public void onClick(View v) {
// do something when the button is clicked
}
...
Event Handling - here have
a SEPARATE class
EVENT HANDLING CODE in separate object mCorkyListner
// Create an anonymous implementation of OnClickListener STEP 3
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
//Now inside your Activity class
protected void onCreate(Bundle savedValues) {
...
// STEP 1: Capture our button from layout
Button button = (Button)findViewById([Link]);
// STEP 2: Register the onClick listener with the implementation above
[Link](mCorkyListener);
...
}
Lets do it step by step
Depends on IDElets look at Eclipse
STEP1: Create a Button
Create button
using XML GUI
interface in Eclipse
Set property so
label on button is
Hit Me
Also, added text
view with
instructions telling
user to hit button
STEP2: Register the main
Application Activity as
EventHandler
for button
Grab button associated with its id, store in
local variable b.
Call setonClickListener(this)
public class AndroidExample_EventHadlingActivity extends Activity implements OnClickListener {
int hits =0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
Button b = (Button)findViewById([Link].button1);
[Link](this);
}
\
STEP2: Register the main
Application Activity as
EventHandler
for button
Have the main class AndroidExample_eventHandlingActivity
implement OnClickListener for button
In method onClick increment hits and use in display string.
public class AndroidExample_EventHadlingActivity extends Activity implements OnClickListener {
int hits =0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//see previous slide..
}
// Implement the OnClickListener callback
//STEP 3 event handler
public void onClick(View v) {
// do something when the button is clicked
[Link]++;
TextView text = (TextView)findViewById([Link].textView1);
String s = "You hit me " + [Link];
[Link](s);
}
}
Run your code
Lets run the previous code.
After running itbefore hit
button
After hitting button a few
times
What can you do with Event
Handling
.your imagination is the limit..
Remember .Widget :
Views that have events
For a list of the widgets provided by Android,
see the [Link] package.
Some Examples
Button
CheckBox
DatePicker
EditText
ImageView
SearchView
Spinner
Dont [Link] and
Intent Recievers
less coupled ways to have things happen.
.but, that is another lecture