15 Android Event Handling
15 Android Event Handling
Events are a useful way to collect data about a user's interaction with
interactive components of Applications. Like button presses or screen
touch etc. The Android framework maintains an event queue as first-in, first-
out (FIFO) basis. You can capture these events in your program and take
appropriate action as per requirements.
Event Listeners − An event listener is an interface in the View class that contains
a single callback method. These methods will be called by the Android framework
when the View to which the listener has been registered is triggered by user
interaction with the item in the UI.
OnClickListener()
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
This is called when the user either clicks or touches or focuses
onClick()
upon any widgetAccept
like button, text, image etc. You will use
onClick() event handler to handle such event.
Learn more
onLongClick() OnLongClickListener()
https://www.tutorialspoint.com/android/android_event_handling.htm 1/9
5/14/2019 Android Event Handling
OnFocusChangeListener()
This is called when the widget looses its focus ie. user goes
onFocusChange()
away from the view item. You will use onFocusChange() event
handler to handle such event.
OnFocusChangeListener()
This is called when the user is focused on the item and presses
onKey()
or releases a hardware key on the device. You will use onKey()
event handler to handle such event.
OnTouchListener()
This is called when the user presses the key, releases the key,
onTouch()
or any movement gesture on the screen. You will use onTouch()
event handler to handle such event.
OnMenuItemClickListener()
onMenuItemClick() This is called when the user selects a menu item. You will use
onMenuItemClick() event handler to handle such event.
onCreateContextMenuItemListener()
onCreateContextMenu() This is called when the context menu is being built(as the result
of a sustained "long click)
There are many more event listeners available as a part of View class like
OnHoverListener, OnDragListener etc which may be needed for your application. So I
recommend to refer official documentation for Android application development in case you
are going to develop a sophisticated apps.
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Event Listeners Registration
Accept
Event Registration is the process by which an Event Handler gets registered with an Event
Listener so that the handler is called when the Event Listener fires the event. Though there
Learn more
https://www.tutorialspoint.com/android/android_event_handling.htm 2/9
5/14/2019 Android Event Handling
are several tricky ways to register your event listener for any event, but I'm going to list
down only top 3 ways, out of which you can use any of them based on the situation.
Below section will provide you detailed examples on all the three scenarios −
Touch Mode
Users can interact with their devices by using hardware keys or buttons or touching the
screen.Touching the screen puts the device into touch mode. The user can then interact
with it by touching the on-screen virtual buttons, images, etc.You can check if the device is
in touch mode by calling the View class’s isInTouchMode() method.
Focus
A view or widget is usually highlighted or displays a flashing cursor when it’s in focus. This
indicates that it’s ready to accept input from the user.
android:foucsUp="@=id/button_l"
onTouchEvent()
public boolean onTouchEvent(motionEvent event){
switch(event.getAction()){
case TOUCH_DOWN:
Toast.makeText(this,"you have clicked down Touch button",Toast.LENTH_LONG).show();
break();
case TOUCH_UP:
Toast.makeText(this,"you have clicked up touch button",Toast.LENTH_LONG).show();
break;
We usecase
cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
TOUCH_MOVE:
Toast.makeText(this,"you have clicked move touch button"Toast.LENTH_LONG).show();
break;
} Accept
return super.onTouchEvent(event) ;
}
Learn more
https://www.tutorialspoint.com/android/android_event_handling.htm 3/9
5/14/2019 Android Event Handling
But if you applied the handler to more than one control, you would 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.
Following are the simple steps to show how we will make use of separate Listener class to
register and capture click event. Similar way you can implement your listener for any other
required event type.
Step Description
1 You will use Android studio IDE to create an Android application and name it as
myapplication under a package com.example.myapplication as explained in the Hello
World Example chapter.
2 Modify src/MainActivity.java file to add click event listeners and handlers for the two
buttons defined.
4 No need to declare default string constants.Android studio takes care default constants.
5 Run the application to launch Android emulator and verify the result of the changes
done in the aplication.
package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Bundle;
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; Accept
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = new ProgressDialog(this);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView txtView = (TextView) findViewById(R.id.textView);
txtView.setTextSize(25);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView txtView = (TextView) findViewById(R.id.textView);
txtView.setTextSize(55);
}
});
}
}
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Handling "
We useandroid:layout_alignParentTop="true"
cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
Accept
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
Learn more
android:layout_height="wrap_content"
https://www.tutorialspoint.com/android/android_event_handling.htm 5/9
5/14/2019 Android Event Handling
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small font"
android:id="@+id/button"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Font"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:textSize="25dp" />
</RelativeLayout>
<application
android:allowBackup="true" Learn more
android:icon="@drawable/ic_launcher"
https://www.tutorialspoint.com/android/android_event_handling.htm 6/9
5/14/2019 Android Event Handling
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your myapplication application. I assume you had created your AVD
while doing environment setup. To run the app from Android Studio, open one of your
project's activity files and click Run icon from the toolbar. Android Studio installs the app
on your AVD and starts it and if everything is fine with your setup and application, it will
display following Emulator window −
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Accept
Learn more
https://www.tutorialspoint.com/android/android_event_handling.htm 7/9
5/14/2019 Android Event Handling
Now you try to click on two buttons, one by one and you will see that font of the Hello
World text will change, which happens because registered click event handler method is
being called against each click event.
Exercise
I will recommend to try writing different event handlers for different event types and
understand exact difference in different event types and their handling. Events related to
menu, spinner, pickers widgets are little different but they are also based on the same
concepts as explained above.
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Previous Page Next Page
Accept
Learn more
https://www.tutorialspoint.com/android/android_event_handling.htm 8/9
5/14/2019 Android Event Handling
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Accept
Learn more
https://www.tutorialspoint.com/android/android_event_handling.htm 9/9