IntelliJ IDEA 2017.1 Help

Making the Application Interactive

Although our sample application is fully functional at this point, it does not support any form of interaction yet. To make the application support tap events, you need to edit the HelloDroid class defined in the src folder.

1. Open the MyActivity class

In the Project view, locate the HelloDroid.java file under the src | com.example.HelloDroid folder and click on it:

/help/img/idea/2017.1/android_activity.png

2. Add references to visual elements

In Android, you need to explicitly obtain references to visual elements in order to manipulate them programmatically. You need to 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.

  1. Add the following code to the HelloDroid class:
    private TextView message; private ImageView droid;
  2. Add a call to a new method called InitializeApp in OnCreate. IntelliJ IDEA promptly detects that this method is missing and suggests generating it for you:
    /help/img/idea/2017.1/android_make_interactive_create_method.png
  3. In the InitializeApp method, assign private members a reference to a visual element:
    message = (TextView) findViewById(R.id.message); droid = (ImageView) findViewById(R.id.imageView);

    The expression R.id.xxx indicates a member of the auto-generated R class.

3. Add an event handler

In an application, no interaction is possible 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:

private View.OnClickListener droidTapListener;

Add this member to the HelloDroid class and initialize it in the InitializeApp method. You code should now look as following:

private void InitializeApp() { message = (TextView) findViewById(R.id.message); droid = (ImageView) findViewById(R.id.imageView); // Define and attach listeners droidTapListener = new View.OnClickListener() { public void onClick(View v) { TapDroid(); } }; droid.setOnClickListener(droidTapListener); }

The net effect of this code is that every time the user taps the image, the TapDroid method is invoked.

4. Handle the 'Click' event

  1. The TapDroid method just counts the times the user touched the image, and displays a message. You need to add a new private member to the HelloDroid class to count clicks:
    public class MyActivity extends Activity { private TextView message; private ImageView droid; private View.OnClickListener droidTapListener; private int counter = 0; // More code goes here ... }
  2. Next, define the TapDroid method as shown below:
    private void TapDroid() { counter++; String temp; switch (counter) { case 1: temp = "once"; break; case 2: temp = "twice"; break; default: temp = String.format("%d times", counter); } message.setText(String.format("You touched the droid %s", temp)); }

5. Build an application and launch it on a device

Your sample application is now complete. You can build and deploy it to an Android device.

  1. Create a run/debug configuration and select USB device under Target Device.
  2. Connect an Android device to the computer through a USB cable. If the device is connected for the first time, wait until all drivers are installed.
  3. If this is the first time you are deploying an application outside of the Android application stores, enable the USB Debugging mode on your device.

    On Android 4.2 or higher, do the following:

    • Open your device's Settings.
    • Scroll to About phone or About tablet and tap it.
    • Scroll to the bottom and tap Build number 7 times until you see the "You are a developer!" message. By doing so, you've unlocked the USB debugging mode on your device.
    • Now navigate to Settings | Developer Options | Debugging | USB Debugging to let your phone deploy non-packaged applications.
  4. Make sure the appropriate run/debug configuration is selected in the drop-down in the top-right corner of the editor and click the Run icon:
    /help/img/idea/2017.1/android_application_run_physical.png
  5. When the application has been successfully deployed on the device, tap the image and look at the changes to the user interface:
    /help/img/idea/2017.1/android_application_physical.png

See Also

Last modified: 18 July 2017