Академический Документы
Профессиональный Документы
Культура Документы
CSC- 341
Mobile Application Development
Lecturer
M Talha Alam
https://sites.google.com/view/mtalhaalam
Bahria University Karachi Campus
LECTURE 5
LISTENERS
OUTLINE
Event Listener
Event Listener
• Events are a useful way to collect data about a user's
interaction with interactive components of your app,
like button presses or screen touch etc.
Event Listener
• In the figure there is an activity and a button on it.
• When user clicked/ press the “Click Me” button an event generated and we
toast a text.
• So it is useful to handle user interaction as well as navigation according to user
interaction
Disadvantage
• If there are more than one control, you have to cut and paste the
code for the handler and if the code for the handler is long, it makes
the code harder to maintain
Bahria University Karachi Campus
Example
• Add a button in xml file
<Button
android:id="@+id/button1"
android:layout_height="wrap_content
"
android:layout_width="match_parent"
android:text="Click Me"/>
Example
• Register click event with button
clickMe.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
}
}
);
Bahria University Karachi Campus
Example
• Implements onClickListener callback
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text=“First Button"/>
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text=“Second Button"/>
Example
• Implements onClickListener() CallBack which is onCLick()
Rather you will use the layout file (activity_main.xml) to specify the handler method via the
android:onClick attribute for click event.
Can be control click events differently for different control by passing different event
handler methods.
The event handler method must have a void return type and take a View as an argument.
The method name is arbitrary, and the main class need not implement any particular
interface.
This approach does not allow to pass arguments to Listener and for the Android developers
it will be difficult to know which method is the handler for which control until they look
into activity_main.xml file.
Bahria University Karachi Campus
Example
• Used android:onClick attribute to specify a callback function
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text=“First Button“
android:onClick=“method1” />
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text=“Second Button“
android:onClick=“method2” />
Bahria University Karachi Campus
Example
• Implements onClickListener() CallBack which is onCLick()
public void method1(View v)
{
// write your handler code here
return;
}
EXAMPLE CODE
The example below shows how to register an on-click listener for a Button.
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new
OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
Button btnCTime;
EditText txtCTime;
import android.widget.Button;
import android.widget.EditText;
btnCTime=(Button)findViewById(R.id.btnGenCurTime);
txtCTime=(EditText)findViewById(R.id.txtShowCurTime);
Bahria University Karachi Campus
txtCTime.setInputType(InputType.TYPE_NULL);
</li>
<li>Set the OnClickListener for the Button
btnCTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View vw) {
}
});
import android.view.View.OnClickListener;
Bahria University Karachi Campus
txtCTime.setText(new Date().toString());
import java.util.Date;
• Finally, the default activity file should look like as java file in next
slide.
Bahria University Karachi Campus
package com.suvendu.tutorials.button;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;