Android Event Handling
Android Event Handling
Basics
• Event Listeners
• Event Handlers
• Event Listener Registration
• 1. Event Listeners
An event listener is an interface in the View class
of Android. It has a single callback method. This
method will be called when the View that is
registered with the Listener is activated by the
user’s action.
2. Event Handlers
Event handles are the actual methods that have the action that is
to be taken. After an event has happened and event listeners are
registered, event listeners call Event Handler. These are useful to
define some callback methods.
3. Event Listener Registration
• Event Registration is the process in which Event
Listeners are registered with Event Handlers. This is
important as Handler will work only after the
Listener fires an Event.
Android Event Listener registration can be done in the
following three ways:
1. The first method to register event listeners is by
directly mentioning them in activity_main.xml.
2. We can also register event listeners by using Activity
class that implements a listener interface.
3. The last method is by using an Anonymous class.
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
TextView t1= (TextView)findViewById(R.id.t1);
t1.setText("My first Program");
}
}
);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/t1"
android:layout_width="150dp"
android:layout_height="58dp"
android:text="HI"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.235" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
tools:layout_editor_absoluteX="154dp"
tools:layout_editor_absoluteY="385dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity
AppCompatActivity makes it easier for you to develop apps that will
behave consistently across many versions of Android, compared to
inheriting from Activity or some other subclass of Act
setContentView(R.layout.activity_main);
This method parses the xml file mentioned as
parameter.It creates object for all controls .
findViewById(R.id.b1);
Finds a view that was identified by the android:id
XML attribute that was processed in above step.
• View is the superclass for all widgets and the
OnClickListener interface belongs to this class. All
widgets inherit this. View.OnClickListener is the same
as OnClickListener. You would have to override the
onClick(View view) method from this listener to
achieve the action that you want for your button.
• To tell Android to listen to click events for a widget,
you need to do:
• The 'View' parameter passed in the onClick() method
simply lets Android know that a view has been
clicked. It can be a Button or a TextView or
something else.
• It is up to you to set an OnClickListener for every
widget or to simply make the class containing all
these widgets implement the interface.
• In this case you will have a common onClick()
method for all the widgets and all you have to do is
to check the id of the view that is passed into the
method and then match that against the id for each
element that you want and take action for that
element.
Thanks !