Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

BSE2209

MOBILE PROGRAMMING PROJECT


In Last Lecture

Implementing a Bottom Navigation


 Activity screen
 BottomNavigationView
 Menu
 Menu items
 Fragments container
 Fragments

 Material Design

2
Last Week’s Tasks
 Complete developing the following screens

3
In this Lecture

 Event Handling
 Listener objects
 Anonymous inner objects
 Use of lambdas in event handlers

4
Events: What are they?

 A user interacts with your app through: touch, click, swipe, or


typing something.
 Android framework captures, stores, processes, and sends
these actions to your app as event objects.
 You write functions to respond to these events.
 These functions are written inside listener objects.

5
Events: What are they?

Simplified event handling model


6
Common Lister Objects

Interface Function Description


View.OnClickListener onClick() Called when the user either touches and
holds the control (when in touch mode), or
focuses upon the item with the navigation keys
then presses the ENTER key
View.OnLongClickListe onLongClick() Almost the same as a click, but only longer
ner
View.OnFocusChangeL onFocusChange() When the user navigates onto or away from the
istener control
View.OnTouchListener onTouch() Almost the same as click action but this handler
lets you find out if the user swiped up or down.
You can use this to respond to gestures
View.OnCreateContext onCreateContextM Android calls this when a ContextMenu is being
MenuListener enu() built, as a result of a sustained long click

7
Setting up a listener

 Two ways to setup a listener:


 Registering the listener object: tell Android Framework which
function to call when the user interacts with a view object
 View object sets it using its onclick attribute
 Registering a listener object
my_button.setOnClickListener(object: View.OnClickListener {
override fun onClick(v: View?) {
//do something
}
})

8
Setting up a listener
 The setOnClickListener is a member my_button.setOnClickListener(object:
function of the android.view.View class View.OnClickListener {
override fun onClick(v: View?) {
 This function expects an OnClickListener // do something
object as an argument—this object }
becomes the listener for the button control })

 We can create a listener object by creating


an object expression that inherits from
View.OnClickListener.
 Same object expression can be written
in less verbose way using lambda
function
my_button.setOnClickListener { // do something }
9
Setting up a listener

 To handle an event using onclick attribute of the object:


 Create a function in the associated kotlin file which accepts a view
 Set the value of the onclick attribute of the object to the function
created

fun do_something(v:View?){
// do something here
}

<Button
android:id="@+id/button"
android:onClick="do_something"
android:text="Button"/>
10
Tasks for this week
Implement the following sequence of interfaces (Tapping on components circled in blue should take you to next screen)

11
Tasks for this week
Implement the following sequence of interfaces (Tapping on components circled in blue should take you to next screen)

12
Tasks for this week
Implement the following sequence of interfaces (Tapping on components circled in blue should take you to next screen)

Notice the created routine on routines fragment

13
Tasks for this week
Implement the following sequence of interfaces (Tapping on components circled in blue should take you to next screen)

14

You might also like