Android Event Handling Example Tutorial
Events are a helpful manner to gather data in relation to a user’s interaction with interactive constituents of your app, such as button presses or screen touch etc.
The Android framework takes care of an event line up into which events are positioned as they take place and subsequently every event is removed from the line-up on a first-in, first-out (FIFO) basis.
You can incarcerate these events in your program and take suitable action as regards to requirements.
There are subsequent three concepts in relation to Android Event Management:
Event Listeners:
The View class is majorly involved in creating an Android GUI, same View category makes available a few Event Listeners. The Event Listener is the object that receives warning when an event occurs
Event Listeners Registration
Event Registration is the procedure by which an Event Handler is registered with an Event Listener in order that the handler is called when the Event Listener fires the event.
Event Handlers
When an event takes place and we have registered an event listener for that event, the event listener calls the Event Handlers, which is the procedure that in reality takes care of the event.
There are a lot more event listeners accessible as a section of View class like OnHoverListener, OnDragListener and so on which may be required for your application.
Therefore, I recommend to refer to official documentation for Android application development in case you are going to develop a complicated apps.
Event Listeners Registration
Event Registration is the procedure by which an Event Handler is registered with an Event Listener in order that the handler is called when the Event Listener fires the event.
Though there are a lot of tricky ways to register your event listener for any event, but here we are going to only talk about the 3 major ways, out of which you can make use any of them depending on the situation.
The use of an Anonymous Inner Class
Activity class implements the Listener interface.
The use of Layout files activity_main.xml to specify event handler directly.
The section below will make available to you detailed instances on all the three situations:
Event Handling instances
Event listeners registration with the use of an anonymous inner class. In this instance, you will produce an anonymous implementation of the listener and will be helpful if each class is applied to a single control only and you have benefit to pass arguments to event handler.
In this advance event handler procedures can have right of entry into private data of Activity. No reference is required to call to Activity 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 much more difficult to maintain.
Comprehension of Android Events
Events in Android can exist in a lot of various forms, but are normally produced in response to an external action.
The most general form of events, mainly for devices like tablets and smartphones, involve a few form of interaction with the touch screen.
Events like that fall into the class of input events.
The Android framework takes care of an event line up into which events are positioned as they take place. Events are then detached from the lineup on a first-in, first-out (FIFO) basis.
In the case of an input event like a touch on the screen, the event is transferred to the view placed at the position on the screen where the touch occurred.
In addition to the event notification, the view is as well passed a collection of information (in relation to the event type) about the nature of the event like the coordinates of the point of contact between the user’s fingertip and the screen.
In order to be capable of taking care of the event that it has been passed, the view ought to have in place an event listener.
The Android View category, from which every user interface constituents are derived, is made up of a range of event listener interfaces, each of which is made up of an abstract declaration for a callback procedure.
In order to be capable of reacting to an event of a specific form, a view ought to register the suitable event listener and carryout the corresponding callback.
For instance, if a button is to respond to a click event (the corresponding user touching and releasing the button view as though clicking on a physical button) it ought to both register the View.onClickListener event listener (through a call to the target view’s setOnClickListener() method) and carryout the corresponding onClick() callback procedure.
In the event that a “click” event is observed on the screen at the position of the button view, the Android framework will call the onClick() procedure of that view when that event is alienated from the event line up.
It is, of course, within the execution of the onClick() callback procedure that any tasks ought to be carried out or other procedures called in reaction to the button click.
Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet: AutoCompleteTextViewSuggestions
Package: com.inducesmile.autocompletetextviewsuggestions
Keep other default selections.
Continue to click on next button until Finish button is active, then click on Finish Button
Once you are done with creating your project, make sure you change the package name if you did not use the same package.
Open your project activity_main.xml file and copy and paste the following code.
 android: <Button android: <Button android: <Button android: </RelativeLayout>
The MainActivity.java file contains one TextView and three Button Views. Once any of the button View is clicked, it will change the font size of the text associated with the TextView. This signifies the button that has responded to the event.
Copy and paste the following code in your MainActivity.java
package inducesmile.com.androideventhandling; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); Button button1 = (Button)findViewById(R.id.button); Button button2 = (Button)findViewById(R.id.button2); Button button3 = (Button)findViewById(R.id.button3); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setTextSize(16); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setTextSize(20); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setTextSize(30); } }); } ); } }
You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.
Remember to subscribe with your email so that you will be among the first to receive our new post once it is published