Skip to end of metadata
Go to start of metadata

Redirection Notice


Although fully functional, the sample application doesn't support any form of interaction yet. To make the application support tap events, you need to edit the class MyActivity defined in the src folder.

1. Open the class MyActivity

From the Project view, locate the class and click on it.

2. Add references to visual elements

In Android, you need to explicitly obtain references to visual elements in order to manipulate them programmatically. You define private members on the activity class to hold these references and then initialize these members in a newly created method invoked from within onCreate. You add the following code to MyActivity class:

Next, you add a call to a new method called InitializeAppin onCreate. IntelliJ IDEA promptly detects the method is missing and offers to generate it for you.

In the method InitializeAppyou assign private members a reference to a visual element:

The expression a member on the Rauto-generated class. The Rclass gathers references to any declared resource for the application.

3. Add an event handler

In any application, there's no interaction without events and event handlers. As an example, let's add a click handler to the droid image view and display a message every time the user touches the image. In Java, an event handler takes the following form:

You add this member to the class MyActivity and initialize it in the InitializeAppmethod:

The net effect of this code is that every time the user clicks on the image the TapDroidmethod is invoked.

4. Handle the Click event

The TapDroidmethod just counts the times you touched the image and displays a summary message. You add a new private member to the MyActivity class to count clicks:

Next, you define the method TapDroidas below:

5.  Build the final version of the application

The sample application is now complete. You can build and deploy it to the device. Once ready, tap on the displayed image and look at the changes to the user interface.

The present tutorial provided a quick introduction to the fundamental aspects of Android development and also introduced to the most common features of IntelliJ IDEA you might want to use when building Android applications. What remains to discuss is how to write unit tests and package the application for the app store.

  • No labels