Developing applications for Android in IntelliJ IDEA

From IntelliJ-Wiki

Jump to: navigation, search

Contents

Introduction

IntelliJ IDEA supports development of applications to be executed on mobile phones that run under the Android operating system. Besides general coding assistance, the IDE lets you test Android applications on user-configured emulators of physical devices.

IntelliJ IDEA helps:

This tutorial will walk you step-by-step through developing and launching a simple Android application.

Prerequisites

  • You are working with IntelliJ IDEA Ultimate edition version 9 or higher.
  • JDK is available on your machine.
  • Android SDK is installed on your machine. This tutorial uses SDK 2.2. FroYo.

Creating a New Project

Let’s start from the very beginning and create a project for our application.

Choose File | New Project on the main menu or click the Create New Project icon on the Welcome screen.

File:welcome_screen_create_new_project.png

On the first page of the New Project wizard, make sure that the option Create project from scratch is selected.

File:create_new_project_page_1_from_scratch.png

On the second page of the wizard, specify the name of the project. Select the Create module check box and choose Android Module as the module type. The name of this module will be android_hello_world.

File:create_new_project_page_2_android_module.png

On the third page of the wizard, choose the Create source directory option and accept the default name src for it.

File:4 create_new_project_page_3_src_folder.png

When you are creating a project for the first time after IntelliJ IDEA installation, no JDKs (Java SDKs) are configured at the IDE level yet. This means that IntelliJ IDEA does not know the location of any JDK home directories, even if you have several JDKs on your computer. In this case, IntelliJ IDEA opens a page where you are asked to specify the JDK for your project. Click the Configure button and specify the location of the desired JDK in the Select Home Directory for JSDK dialog box that opens. When you click OK, you return to the wizard where the selected JDK is listed as the Project JDK. The specified JDK is now available at the IDE level. IntelliJ IDEA will treat this JDK as the default project JDK for any new project and will not bring you to this page anymore.

On the fourth page of the wizard, we’ll integrate IntelliJ IDEA with the Android SDK and specify the target Android platform for which the application is intended. To do that, click New in the SDK properties area.

- Edit 6th Sep 2011 - Please note that in version 10.5.2 ( 31st August 2011 ) instead of the below procedure; do this.

1) Click on the ellipses ( to bring up a new screen )

2) Click on the + icon ( top left )

3) Select your Android SDK base directory

4) Follow the wizard.

- End Edit 6th Sept 2011 -

File: create_new_project_page_4_integrate_sdk.png

In the Select Path dialog box that opens, specify the installation folder of the Android SDK.

File:select_path_to_adndorid_sdk.png

When you click OK, the Select Android Build Target dialog box opens. In this dialog box, specify the Android platform for which the application will be intended.

File:select_android_build_target.png

When you click OK, you return to the fourth Wizard page where the chosen platform is displayed in the Android Platform drop-down list (1). To have a sample application created, select the Create “Hello, World!” project check box (2) and accept the suggested activity name MyActivity (3).

File:create_new_project_page_4_final.png

When you click Finish, IntelliJ IDEA creates a project and generates the skeleton of our application. First, let’s look deeper into its structure.

Exploring an Android Application

To explore our application, we’ll use the Project tool window that shows the following files and folders:

File:project_structure.png

The .idea (1) folder contains a number of subfolders, mainly with internal IntelliJ IDEA information.

The src (2) folder contains the MyActivity.java (3) file source code that implements the functionality of your application. The file belongs to the com.example package.

The res (4) folder contains various visual resources.

  • The layout/main.xml file (5) defines the appearance of the application constituted of resources of various types.
  • The values folder (6) is intended for storing .xml files that describe resources of various types. Presently, the folder contains a strings.xml file with String resources definitions. As you will see from the Adding a Color section, the layout folder can also contain, for example, a descriptor of colors.
  • The drawable folder (7) contains images.

The gen (8) folder contains the R.java (9) file that links the visual resources and the Java source code. As you will see from the sections below, IntelliJ IDEA supports tight integration between static resources and R.java. As soon as any resources are added or removed, the corresponding classes and class fields in R.java are automatically generated or removed accordingly. The R.java file also belongs to the com.example package.

Creating Elements of Your Application

To illustrate the mainstream Android development workflow in IntelliJ IDEA, let’s expand the stub “Hello world!” application with a piece of text to explain the main goals of the application. Let’s also define the color in which the explanation text will be displayed on the screen.

As you can see from the source code of MyActivity.java (1), it just references (2) the layout/main.xml file that defines the application appearance.

File:my_activity.png

Now, let’s open the main.xml file (3) where IntelliJ IDEA has already listed the “hello” (4) string and referenced the string definition (6) in the res/values/strings.xml file (5):

File:main_xml_default_v9.png

We need to add three things to the application layout: an explanation string and a color to display the string.

Adding a String

In the main.xml file, place the following piece of code inside the <LinearLayout /> tag:

     
           <TextView android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     android:text="@string/explanation"
            />
     

As you can see, IntelliJ IDEA reports an unresolved reference and highlights it red.

File:add_string_defintion_unresolved_reference_1.png

But IntelliJ IDEA also suggests a quick fix for the problem: just press Alt+Enter or click the red bulb (1), then choose Create resource explanation in strings.xml option (2) from the list:

File:add_string_defintion_unresolved_reference_1_intention_actions.png

IntelliJ IDEA opens the strings.xml file where a new empty string definition is added to the list.

File:add_string_defintion_unresolved_reference_1_string_defintion_version9.png

Type the application description inside the <string /> tag, for example:

This simple Android application illustrates the mainstream Android development workflow in IntelliJ IDEA.

If you now open the R.java file, you will see that IntelliJ IDEA has added the new string to the list of string resources:

File:add_string_defintion_unresolved_reference_1_string_defintion_added_to_r_java_v9.png

Now, let’s add a string_color resource definition.

Open the color.xml file in the editor by double-clicking it in the Project tool window. Type the hexadecimal identifier of the desired color prepended with # (1), for example, #ff00ff77, inside the <resources> tag. As you type, IntelliJ IDEA shows the preview of the specified color (2) in the left gutter area: File:Add string defintion unresolved reference 2 color defintion.png

Adding a Color

Now let’s define the color to display the explanation string, To do that, we will create a color resource file with a string_color definition. In the project tree, right click the values folder (1) and choose New | Values resource file (2) on the context menu. In the New values resource file dialog that opens, specify color as the new file name (3).

File:add_values_resource_color.png

When you click OK, IntelliJ IDEA displays the new file in the project tree:

File:add_values_resource_color_file_added.png

Now, let’s add a string_color resource definition. Open the color.xml file in the editor by double-clicking it in the Project tool window. Inside the <resources /> tag, type the color definition in the form:

     
             <color name="string_color">hexadecimal identifier of the desired color prepended with #</color>
     

In our example the hexadecimal identifier is #ff00ff77, As you type the color definition (1), IntelliJ IDEA shows the preview of the specified color (2) in the left gutter area:

File:add_string_defintion_unresolved_reference_2_color_defintion_v9.png

As you might have guessed, the new resource definition is automatically reflected in R.java:

File:color_added_to_r_java_v9.png

Now, let’s apply the new color to the explanation string. In the main.xml file, locate the following <TextView /> item:

     
           <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/explanation"
            />
     

and add the following code inside it below android:text="@string/explanation":

     
             android:textColor="@color/string_color"
     

That’s all with developing the sources of our application.

Running Android Application

During the project creation, IntelliJ IDEA has generated the default run configuration android_hello_world. To launch the application straight away, we only need to configure a virtual device to run it on. This device will emulate execution of the application on the target physical device.

Configuring a Virtual Android Device

From the Run/Debug Configuration (1) drop-down list on the toolbar, choose Edit Configurations.

File:edit_configurations.png

IntelliJ IDEA opens the Run/Debug Configurations dialog box that shows the details of the default android_hello_world run configuration (1).

File:run_debug_configuration_dialog_hello_world.png

Click the Browse button next to the Prefer Android Virtual Device for Deployment drop-down list (2). The Select Android Virtual Device dialog box opens, click Create:

File:select_virtual_device_empty.png

In the Create Android Virtual Device dialog box that opens, accept the suggested device name MyAvd0 (1):

File:create_android_virtual_device.png

When you click OK, IntelliJ IDEA brings you to the Select Android Virtual Device dialog box, where the virtual device you’ve defined is already added to the list and selected.

File:select_virtual_device.png

Click OK to save the settings and return to the Run/Debug Configurations dialog box, where the Prefer Android Virtual Device for Deployment drop-down list now shows the emulator you’ve defined.

File:run_debug_configuration_device_specified.png

Complete the configuration definition settings by clicking OK.

Fortunately, you need to configure a virtual device only once, during the first application start. IntelliJ IDEA will use it by default when you start your application with the android_hello_world run configuration. Moreover, IntelliJ IDEA remembers this virtual device at the IDE level so you can use it for running other applications.

Start Application

Now that we are through with all the preliminary steps, let’s launch our application.

On the toolbar, click File:run.png (2) next to the Run/Debug Configuration (1) drop-down list where the android_hello_world run configuration is already selected by default.

File:run_default_hello_world_configuration.png

IntelliJ IDEA launches the configured emulator:

File:emulator_1_froyo.png

Next, the IDE deploys the Hello world application to it, and displays the application output on the screen:

File:running_application_hello_world_explanation_froyo.png

Congratulations! You have developed and launched a simple Android application.

Related Articles

Developing applications for Android using existing sources

Personal tools