Android Programming In Java - Android Events
Written by Mike James   
Monday, 30 July 2018
Article Index
Android Programming In Java - Android Events
Lambda Types

Working with Android Studio 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 - events. Even if you are a skilled Java programmer you are likely not to be familiar with events and now Android has multiple ways of dealing with them.

In earlier chapters we used Android Studio to add onClick event handlers. We now need to find out how to work with any event, 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 Attributes window.

This extract from Android Programming In Java: Starting with an App 3rd Ed (I/O Press) covers events described in Chapter 4.


In this chapter we look in depth at Android event handling. As before, the emphasis will be on using Android Studio and the Designer to get as much of the work done as possible. 

This is a moderately advanced topic and many Android programmers don’t bother to understand events in their entirety, preferring to just follow the boilerplate code given in examples. You can do this, but understanding is usually better.

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

The Lambda Approach

If you have used languages other than Java you might expect event handlers to simply be functions that are passed to UI 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. Android Studio currently doesn’t do this – you have to add code to handle an event. It does, however, help with the automatic generation of this code. 

Events in Java are slightly more complicated because originally everything in Java was an object and the only way a function can exist is as a method, i.e. as part of an object. Java 8 changed all of this with the introduction of lambda functions. A lambda function is a function that doesn’t belong to an object, it isn’t a method and it can be passed as a parameter to another function. This is the key property of a lambda function that makes it very easy to implement general event handling – i.e. not just onClick.

The only drawback of using Java 8 features is that they are not enabled by default and some are only available for use with Nougat (API level 24) or higher. However, lambdas, which make event handling so much easier, will work with all versions of Android and are well worth using.

For a new project all you have to do is select source and target compatibility at 1.8 in the Project Structure dialog box accessed using File, Project Structure:




As long as you have enabled Java 8, you can make use of lambda functions both to implement event handlers and in more general situations.

First what is a lambda?

A lambda is a function that you can define using special notation. The function doesn’t have a name, it is an anonymous function. A lambda can also be stored in a suitable variable and passed to another function.

You define a lambda by supplying a head with parameters and a body with code:

(parameter list) -> { code
  return something;

For example:

  return a+b;

If you are a long time Java programmer what will be most sh

ocking is that there are no type specifiers in this declaration. Don’t worry a lambda expression is strongly typed but the compiler infers the type of the parameters and the return type from the context. The point is that lambda expressions are always pass as a parameter or assigned to a variable and it is the type of the parameter or variable that determines the types used in the lambda.

It is not an error to include the data type of the parameters but it is always unnecessary.

There are a few simplifications to the lambda notation. If there is just a single parameter you can omit the parenthesis.

For example:

n ->{
  return -n;

If the body is a single expression you can omit the brackets and the return and the result of the expression will be automatically returned.

For example:

n -> -n;

defines the same function as given above. If you use a return then you need the brackets. This is generally called an expression lambda.

These rules can make lambda expressions look very strange in your code and this might make it harder to read. Don’t go for the shortest and most compact expression make sure your code is easy to understand.



Last Updated ( Saturday, 04 August 2018 )