0% found this document useful (0 votes)
59 views20 pages

PAPB 04A EventHandling

The document discusses event handling in Android programming. It explains that events are actions that occur when a user interacts with widgets. Common events include clicks, typing, scrolling. Event-driven programming dictates program execution based on user events. It describes how to add widgets to an activity, define listener methods to handle events, and attach listeners to widgets to respond to events. It provides an example of setting an onClick listener on a button to handle click events.

Uploaded by

Muhammad Rizaldi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views20 pages

PAPB 04A EventHandling

The document discusses event handling in Android programming. It explains that events are actions that occur when a user interacts with widgets. Common events include clicks, typing, scrolling. Event-driven programming dictates program execution based on user events. It describes how to add widgets to an activity, define listener methods to handle events, and attach listeners to widgets to respond to events. It provides an example of setting an onClick listener on a button to handle click events.

Uploaded by

Muhammad Rizaldi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Event Handling

Pemrograman Aplikasi Perangkat Bergerak

 Muhammad Aminul Akbar


Team Teaching PAPB
Contents
 Events
 Widgets and Event Handling
 Anonymous Event Listener
 Setting Activity as Event Listener
Events
Events
 Event: Action that occurs when user interacts with
widgets. An external stimulus your program can respond
to.
 e.g. clicks, typing, scrolling, etc.

 Common kinds of events include: Mouse motion /


tapping, Keys pressed, Timers expiring, Network data
available
 Event-driven programming: Overall execution of your
program is largely dictated by user events. - Commonly
used in graphical interface programs.
Click Event Scenario

event

public void doSomething() {


...
}
Responding Events
1. Write methods to handle each kind of event
("listener" methods).
2. Attach those listener methods to a particular
GUI widgets.

Note:
One listener is not limited to be attached to one widget. It can
be attached to any widgets for the same events.
Widgets and
Event Handling
Widgets: Views That Have
Events
 For a list of the widgets provided by Android, see the
android.widget package.
 Some Examples
 Button
 CheckBox
 DatePicker
 EditText
 ImageView
 Spinner (ComboBox)
 Or even ViewGroups may also have events
Adding Widget Objects to
Activity
 Before widgets and views are manipulatable
through Activity, widgets and views have to be
loaded into Activity using:

View v = findViewById(WIDGET_RESOURCE_ID);

 WIDGET_RESOURCE_ID is defined in layout XML


file, called after calling the setContentView()
method.
Adding Widget Objects to
Activity (2)
 In Layout XML file, give an unique ID to widgets:

<TextView android:id="@+id/textViewName" />

 In Activity onCreate() method, obtain the widgets using


findViewById() and cast the result to their appropriate type:

TextView nameView =
(TextView) findViewById(R.id.textViewName);
Event Handling
 Widgets (or Views) may have events associated on it
and developer may decide what events are going to be
handled and process.
 An event is handled by event listener object.
 Event listener object is ANY objects or even anonymous
objects that implements the event listener interface
 The listener object therefore can be registered to a
specific widgets to do something for a specified events

http://developer.android.com/guide/topics/ui/ui-events.html
Events listener Interface
 Several event listener Interface that handle specific
events are:
 View.OnClickListener (for handling "clicks" on a View),
 View.OnTouchListener (for handling screen touch events),
 View.OnKeyListener (for handling device key presses)

 A listener has an action method that being


executed when an event occurred to the attached
widget

http://developer.android.com/guide/topics/ui/ui-events.html
Event Handling
User Click
Event
 Event Handler is a listener object
which handle an event and do
some action based on what it
listen to. View Widget
 A listener is any objects that Button
implements the listener interface. onClick()
setOnClickListener()
 An Activity could act as an event
listener by implementing the interface
 A listener need to be set to a view Event Handler Object

or widget so that it would be able


to listen to an event occurred to
the view or widget it is assigned to View.OnClickListener
Setup a Button Click Event
Handling
 Step 1: Add View (Button) to Activity, cast it to their appropriate
type
 Button button = (Button) findViewById( buttonViewID )

 Step 2: Implement Event Handler…


 For button's click event means the event handler object should
implements the View.OnClickListener interface
 TWO OPTIONS – using a separate (specific) object to handle event(s)
that implements the interface, OR
 have the Activity containing the button do the event handling and
letting the Activity implements the interface
 Step 3: Register Event Handler to the button
 button.setOnClickListener( clickEventHandlerObject )
Event handling class
diagram

FIRST METHOD
SECOND METHOD (Preferred Way)
1: Handling Event in a Listener Object
 EVENT HANDLING CODE in separate object named myClickListener

// Create an anonymous implementation of OnClickListener STEP 2


private OnClickListener myClickListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};
//Now inside your Activity class onCreate event method
protected void onCreate(Bundle savedValues) {
    ...
    // STEP 1: Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);

    // STEP 3: Register the onClick listener


// with the implementation above
    button.setOnClickListener(myClickListener);

    ...
}
2: Let Activity Handle the
Event
 Here's the code to handle Button's click event
using the Activity itself
public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById(R.id.corky); // STEP 1
        button.setOnClickListener(this); //STEP 2 – registration
    }

    // Implement the OnClickListener callback method


// STEP 3 – event handler
    public void onClick(View v) {
      // do something when the button is clicked
    }
    ...
}
Questions?
Let's Code!
Hello
Mr. Head
 Buatlah aplikasi Android
Mr.Head, dimana
"komponen" Head dapat
ditampilkan/dihilangkan
melalui komponen Checkbox
 Clue:
 Use ImageViews in
ConstraintLayout
 Ubah property visibility dari
View.

You might also like