In Java, a local inner class is similar to other classes, but it’s created within a method. It has certain rules and limitations: it can’t have static members, and if you want to address the containing object such as an Activity, you have to know the name of the containing class. But these classes are critical to Android programming, especially when you want to declare an event handler with Java code.
- View Offline
- [Voiceover] In Java, an interface is a contract. An interface can declare one or more method headers. Each method header consists of the method name, the type of the data it returns or void, and the number, order, and types of the arguments. A class that implements an interface must implement those method headers. And the names, the arguments, and the return type all have to match exactly. Android developers should have a good understanding of interfaces because they're critical to using event handlers.
When you declare an event handler in Java code, you're implementing an interface. I'll describe this in the project event handlers. In the starting version of the project, when the user touches the run code button at the bottom, they display a message. And when they touch the clear button, the messages go away. This is being implemented through a combination of Java code and XML declarations. In my main activity, I have methods called onRunBtnClick and onClearBtnClick.
Those methods are being called through the XML layout file, activity_main.xml. Both of the button declarations have onClick attributes that call those specific methods. Notice that you only declare the method names and you don't call the parentheses or pass in arguments. That's all done for you in the background. Many developers, however, prefer to set up their event handlers using pure Java code. That it makes it completely apparent how the code is being called.
When you set it up using XML, the way I've done here, you have to know ahead of time that that's how it's being done. When you do it in pure Java, it's right there in front of you. So I'll go back to my XML layout file and I'll delete the onClick attributes for both buttons. Then I'll come back to my MainActivity class. My buttons both have ids in the XML layout file, and that will make it easy to setup the event handlers using Java code.
First, I'll declare two fields typed as Button. The first one will be mButtonRun and the second one will be mButtonClear. The m- prefix means member, and it's common to use that prefix in Android code to indicate that a variable is a member of a class. Next, I'll go down to the onCreate method and I'll initialize both buttons. I'll use the findViewById method.
I'll pass in the ids that are declared in the XML layout file, and I'll be sure to cast the return value as the button class. Now I'll duplicate that line. And for this one, I'll change it to mButtonClear. And I'll change the id. To duplicate a line of code in Android Studio, press Command + D on Mac or Control + D on Windows. Now, I'll set up the event handlers. I'll do this one button at a time. I'll call mButtonRun.setOnClickListener.
The setOnClickListener method requires an instance of the OnClickListener interface. And I'll satisfy that requirement by creating a new implementation of the interface, View.OnClickListener. I'll type the beginning of the interface name, OnCl, and then when I auto-complete it, Android Studio fills out the complete interface contract. To satisfy the interface, I have to implement the onClick method which requires a view object as its one and only argument.
Now, I'll refactor my existing code. I'll take this bit of code, calling displayMessage and I'll cut and paste it into my new onClick event method. Now I'll do the same thing for the clear button. I'll call setOnClickListener, and I'll create a new implementation of the OnClickListener interface. And once again, I'll cut and paste my code and move it into the event handler that's declared in Java.
And now, I no longer need these two methods, so I'll comment them out. And then, I'll run the code and make sure that everything is still working the way it did before. And I can still touch the run code button to display messages and the clear button to clear the output. So let's take a look at how these interfaces are working. To satisfy the requirements of the setOnClickListener method, I'm declaring an anonymous implementation of the view.OnClickListener interface.
I'll hold down Control or Command and click into that interface. Now remember, you'll only be able to get to this code if you have the Android source code downloaded to your system. The OnClickListener interface is declared here in View.Java and it declares just this one method header, onClick, returning void, and receiving a view argument. I'll also go back to the MainActivity class and drill down into setOnClickListener.
And this is also defined in the View.Java file. And it receives an instance of OnClickListener and it has the @Nullable annotation meaning that if you don't need to pass in an argument, you can simply pass in null. So this is a clue to how you set up callback methods in Android. First, you define the interface that declares the name and structure of the method that you're going to call. Then, you declare a class that implements that interface. In this example, it's an anonymous class.
It's not given a name in our Java code because it's only being used once. And it's being instantiated inline. So it's an anonymous implementation of an interface that's being used to setup a callback method. And then the underlying application framework is doing the actual work at runtime, making the call to the onClick method which it knows will be there because we've satisfied the interface or contract. If you understand how event handlers in Java work in Android, you can put that to work in your own code.
And I'll show you some examples of that in other places in this course.
Watch these Java tutorials to learn smarter, more efficient methods for Android app development.
- Preparing the development environment
- Packaging and running Android apps
- Optimizing Java code for Android
- Implementing event handler interfaces
- Defining custom callback methods
- Working with the Android SDK's Java packages
- Storing data
- Reading text files
- Parsing JSON and XML data
- Managing device sensors
- Playing audio