Android Adventures - Basic Controls And Events
Android Adventures - Basic Controls And Events
Written by Mike James   
Tuesday, 03 March 2015
Article Index
Android Adventures - Basic Controls And Events
Basic Input Controls
Three Buttons

Working with Android Studio 2 makes building the UI easy with an interactive editor, but you still need to find out how to handle the things it isn't quite so good at. In this chapter we look at the basic UI components and general event handling - i.e beyond onClick. 


Android Adventures With Android Studio



  1. Getting Started With Android Studio 2
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Basic Controls And Events
  5. Layout Containers
  6. UI Graphics A Deep Dive
  7. Menus & The Action Bar
  8. Menus, Context & Popup
  9. Resources
  10. Beginning Bitmap Graphics
  11. Staying Alive! Lifecycle & State
  12. Spinners
  13. Pickers
  14. ListView And Adapters
  15. Using Android Studio (coming soon)

If you are interested in creating custom template also see:

Custom Projects In Android Studio




We have looked at the general principles of building a UI but now we have to spend some time looking at the most basic components that can be used to build an application.

We also need to find out how to work with any event and not just onClick. Even though most controls can be used very successfully using just the onClick event - there are other types of event that cannot be handled simply by setting an onEvent property in the Property Window. In this chapter we look in depth at Android event handling.

As before the emphasis will be on using Android Studio and the Layout editor to get as much of the work done as possible. 

Let's start with the problem of how to write an event handler in Java. 

The General Android Event Handler

If you have used languages other than Java you might expect event handlers to simply be functions that are passed to controls to register them as event listeners - i.e. functions that are called when an event occurs. You might even have used an IDE that adds event handlers automatically for you but Android Studio doesn't currently do this - you have to add code to handle and event. It does however help with the automatic generation of this code. 

Events in Java are slightly more complicated because you cannot pass a function as an argument like you can in JavaScript, C# and many other languages - you can in the latest version of Java but it will be sometime before UI frameworks and Android catchup if they ever do. In Java functions can only exist as methods of objects so if you want to pass a function you have to pass an object that has the function as a method.

As a result what has to be passed to register an event listener is more complicated in Java than in many other languages. The way that it all works was invented in the early Java UI frameworks such as GWT and later refined in Swing. Android uses the same approach.

As already mentioned you can't pass a function you have to pass an object which contains a function that can handle the event. Clearly the event handling function has to be capable of handling the event and hence it is defined depending on the type of the event.

For example an onClick event needs a handler that returns void and accepts a View object, which is the object the event occurred on. 

To make sure that an event listener has the right sort of event handling function events are defined using an Interface. 

If you recall an interface is like a class but it simply defines the functions that a class has to support. If a class inherits or implements an interface then you have to write code for each function defined in the interface. 

For example the onClickListener is an interface that defines a single function 

void onClick(View v)

any class that implements onClickListener has to have an onClick function of this type. 

To create an event handler for the onClick event you have to do the following things:

  1. Create a new class that implements the onClickListener interface

  2. Define your custom onClick event handler within the class

  3. Create an instance of the new class within the Activity

  4. Use the button's setOnClickListener method to add the listener object created in step 3 to the event. 

This is the general pattern that you have to follow when implementing any event handler. The only things that change are the interface you have to implement and the event handling function it defines.

Sounds complicated - well it is and there are ways of simplifying it but first let's see how it is done exactly as described. 

This is not the best way to do the job but it is how it all works in principle and it is important to understand what is going on. 

This is as much an exercise for Java beginners as it is an explanation of event handling. If your Java is good feel free to skip ahead. 

Start a new project called FullClassEvent and add a button using the designer.

To implement an event handler for the onClick event first we need to add a new class to the project. Right click on the folder under the Java folder and select New, Java Class and call it MyOnClickListener. 



This creates a new Java file, in the directory.

Note: For users of other languages it is worth pointing out that Java has an almost unbreakable convention that each class is stored in its own file of the the same name. 

Next edit MyOnClickListener to read:

import android.view.View;
public class MyOnClickListener 
             implements View.OnClickListener {
 public void onClick(View v){
   //process event

Remember to use alt,enter to generate the import statement needed. 

Notice we have now created a class with a single method which is our event handler. In a real case you would write lots of code in place of the single comment that is used in this example. 

That is step one completed. Next we mover to the Activity and first retrieve the Button object:

Button button=

Next we create an instance of our event listener:

MyOnClickListener listener=
                      new MyOnClickListener();

and finally we use the button's setOnClickEventListener method to connect its onClick event to our new listener object:


That is putting it all together:

protected void onCreate(
                  Bundle savedInstanceState) {
 Button  button=
 MyOnClickListener listener=
                      new MyOnClickListener();

Now if you run the program you will find that the onClick function in the MyOnClickListener instance is called when the button is clicked. 

The same overall method works for any event.

You simply have to discover the name of the event listener interface, create a class that implements it, create an instance of the class and register the listener with the component that generates the event using a method something like setOnEvent(handler). Notice that you register the event object i.e. listener and not the event handler method i.e. not onClick().

This is a perfectly good method of implementing event handler but it isn't very practical because you are going to need to create a new class for every event you want to handle and in addition these classes are going to contain one method and be instantiated just once. That is a lot of boiler plate code for something that you are going to have to do often.

There has to be a better way! 

Now that you have seen the fundamentals of Java event handling we can example the two ways that it tends to be done in practice.


Last Updated ( Thursday, 28 April 2016 )

Copyright © 2016 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.