how to create an andriod application simple appliction

23
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:  Create an Android application using the New Project Wizard .  Explore an Android application as a tree-view of files and folders .  Create elements of an Android application  and manage static content resources, such as strings, colors, etc. via tight integration between resources and the  R.java file.  Run an application.  Configure an emulator  of a physical device to run Android applications on. 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 10.5 or higher.  Java SDK (JDK) and Android SDK are installed on your machine. This tutorial uses SDK 3.1. 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 (  Image 1).

Upload: ahmerjamil

Post on 05-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 1/23

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:

  Create an Android application using the New Project Wizard. 

  Explore an Android application as a tree-view of files and folders. 

  Create elements of an Android application and manage static content resources,

such as strings, colors, etc. via tight integration between resources and the R.java 

file.

  Run an application. 

  Configure an emulator of a physical device to run Android applications on.

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 10.5 or higher.

  Java SDK (JDK) and Android SDK are installed on your machine. This tutorial

uses SDK 3.1.

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 onthe Welcome screen ( Image 1).

Page 2: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 2/23

  Image 1. 

On the first page of the New Project wizard ( Image 2), make sure that the option Create

project from scratch is selected.

 Image 2. 

Page 3: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 3/23

On the second page of the wizard ( Image 3), choose the parent folder (1) to create the

project in and specify the name of the project (2). As you type the project name, IntelliJIDEA updates the path to the location of the project files accordingly. Select the Create

module check box (3) and choose Android Module (4) as the module type. By default,

the module will have the same name (5) as the project. The name of our project and

module will be android_hello_world.

 Image 3. 

Page 4: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 4/23

On the third page of the wizard ( Image 4), choose the Create source directory option

(1) and accept the default name src (2) for it. IntelliJ IDEA displays the full path to the

src folder (3) for your information.

 Image 4. 

Configuring Android SDK and Java SDK

On the fourth page of the wizard ( Image 5), we’ll integrate IntelliJ IDEA with the

Android SDK and specify the target Android platform for which the application isintended.

Previously, you could configure an Android SDK inside the Android module facet

settings, while the JDK played the role of the main project/module SDK. IntelliJ IDEA

10.5 considers the Android platform itself as the project/module SDK.

Besides an Android SDK, you still need a JDK because IntelliJ IDEA uses it in

compilation.

When you are creating a project for the first time after IntelliJ IDEA installation, no

Android or Java SDKs are configured at the IDE level yet. This means that IntelliJ IDEAdoes not know the location of any Android SDK or Java SDK home directories, even if 

you have several of them on your computer.

To configure a Java SDK and an Android SDK at the IDE level, click the Browse button

(1) next to the Android SDK drop-down list.

Page 5: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 5/23

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 thewizard 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.

 Image 5. 

Configuring Java SDK

Let’s first configure a Java SDK. In the Configure SDK dialog box that opens ( Image6 ), click the Add toolbar button (1) and choose JSDK (2) from the list:

Page 6: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 6/23

  Image 6. 

In the Select Home Directory for JSDK dialog box ( Image 7 ), specify the folder where

the relevant version of Java SDK is located (1):

 Image 7. 

Page 7: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 7/23

Upon clicking OK (2), you return to the Configure SDK dialog box ( Image 8), where

the selected JDK is already on the list (1):

 Image 8. 

Configuring Android SDK

Click the Add toolbar button (1) again. This time, choose Android SDK (2) from the list

( Image 9):

 Image 9. 

Page 8: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 8/23

In the Select Home Directory for Android SDK dialog box ( Image 10) that opens,

specify the folder where the relevant version of Android SDK is located (1):

 Image 10. 

When you click OK (2), IntelliJ IDEA asks you to specify which version of Java SDK(JDK) configured at the IDE level you want to use in this project ( Image 11):

 Image 11. 

We have just configured the first JDK after IntelliJ IDEA installation, so the list containsonly one item (1). Just click OK.

Page 9: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 9/23

In the dialog box that opens ( Image 12), select the Android target platform. This is the

Android platform for running on which your application will be intended. Choose

Android 3.1 (1):

 Image 12. 

Upon clicking OK, you return to the Configure SDK dialog box ( Image 13), where bothnew SDK configurations – the Java SDK (1) and the Android SDK (2)  – are on the list:

 Image 13. 

Click OK (3). At last we are back to the fourth page of the wizard ( Image 14). As youcan see, IntelliJ IDEA has detected and recognized the newly configured Android SDK

(1):

Page 10: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 10/23

  Image 14. 

Completing Project Creation

 Now let’s specify some additional set-up parameters for our project ( Image 15).

 Image 15. 

Page 11: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 11/23

1.  To have the data automatically arranged so the compiled application completely

fits the selected platform, select the Create default Android application

structure check box (1).

2.  Select the Application (2) project type. We are not going to share the module

resources with other modules so there is no need to declare it as library project. 

3. 

Accept the suggested application name (3) and package (4).4.  To have a sample application created, select the Create “Hello, World!” activity 

check box (5) and accept the suggested activity name MyActivity (6).

When you click Finish (7), IntelliJ IDEA creates a project and generates the skeleton of 

our application. First, let’s look deeper into its structure.

 Note: If you still have not configured an Android SDK yet, you can do it right now.

Choose File | Project Structure on the main menu, the click the SDKs node in the

Project Structure dialog box, that opens, and configure the Android SDK as describedabove. 

Exploring an Android Application

To explore our application, we’ll use the Project tool window ( Image 16 ) that shows the

following files and folders:

Page 12: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 12/23

  Image 16  

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

The src (2) folder contains the MyActivity.java (3) file source code that implementsthe 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.

Page 13: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 13/23

  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 layoutfolder can also contain, for example, a descriptor of colors.

 The drawable folders contain images (7).

The gen (8) folder contains the R.java (9) file that links the visual resources and the Javasource 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 bedisplayed on the screen.

As you can see from Image 17 , the source code of MyActivity.java (1) just references

the layout/main.xml file (4) that defines the application appearance. To navigate to the

referenced code, position the caret at the reference (2) and press Ctrl+B. The main.xml file (4) opens.

 Image 17. 

As you can see, the text to be displayed is Hello World, MyActivity.

Page 14: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 14/23

We need to add two things to the application layout: an explanation string and a color to

display it.

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 ( Image 18), IntelliJ IDEA reports an unresolved reference and highlights

it red.

 Image 18. 

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 ( Image 19):

 Image 19. 

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

to the list ( Image 20).

Page 15: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 15/23

  Image 20. 

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

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

 Image 21 illustrates the updated source code of the strings.xml file (1). If you click the

Go to resource button (2) in the left gutter area, the R.java file (3) opens, with thenew string added to the list of string resources (4) and the cursor positioned at this new

resource:

 Image 21. 

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 ( Image 22). In the project tree, rightclick the values folder (1) and choose New (2) | Values resource file (3) on the context

menu. In the New values resource file dialog that opens, specify color as the new file

name (4).

Page 16: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 16/23

 Image 22. 

When you click OK, IntelliJ IDEA displays the new file in the project tree ( Image 23):

 Image 23. 

Page 17: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 17/23

 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 thedesired color prepended with #</color>

In our example ( Image 24), the hexadecimal identifier is #ff00ff77. As you type thecolor definition (1), IntelliJ IDEA shows the preview of the specified color (2) in the left

gutter area:

 Image 24. 

As you might have guessed, the new resource definition is automatically reflected in

R.java ( Image 25):

 Image 25. 

 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"

Page 18: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 18/23

/>

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 ( Image 26 ) drop-down list on the toolbar, choose

Edit Configurations (1).

 Image 26. 

IntelliJ IDEA opens the Run/Debug Configurations dialog box ( Image 27 ) that shows

the details of the default android_hello_world run configuration (1). As you can see,

IntelliJ IDEA has already selected the module hello_world(2) to apply the configurationto and the activity to launch (3). Make sure the Deploy application check box (4) is

selected and the Choose target device manually check box (5) is cleared.

Page 19: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 19/23

  Image 27. 

Click the Browse button next to the Prefer Android Virtual Device for Deployment 

drop-down list (6). In the Select Android Virtual Device dialog box ( Image 28) thatopens. click Create:

 Image 28. 

The Create Android Virtual Device dialog box ( Image 20) opens. Accept the suggested

device name MyAvd0 (1) and Android 3.1 as the target platform (2):

Page 20: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 20/23

  Image 29. 

The target platform must be the one appointed during the module Android SDK

configuration. 

When you click OK, IntelliJ IDEA brings you to the Select Android Virtual Device 

dialog box ( Image 30), where the virtual device you’ve defined is already added to thelist and selected.

 Image 30. 

Click OK to save the settings and return to the Run/Debug Configurations dialog box( Image 31), where the Prefer Android Virtual Device for Deployment drop-down list

(1) now shows the emulator you’ve defined.

Page 21: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 21/23

  Image 31. 

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 ( Image 32), click (2) next to the Run/Debug Configuration (1) drop-down list where the android_hello_world run configuration is already selected by

default.

 Image 32. 

IntelliJ IDEA launches the configured emulator ( Image 33) and deploys the Hello world 

application on it:

Page 22: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 22/23

 Image 33. 

Click the lock symbol twice to have the application output displayed ( Image 34):

 Image 34. 

Page 23: How to Create an Andriod Application Simple Appliction

7/31/2019 How to Create an Andriod Application Simple Appliction

http://slidepdf.com/reader/full/how-to-create-an-andriod-application-simple-appliction 23/23

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

Related Articles

Developing Android applications on the base of existing sources 

Developing for Android in IntelliJ IDEA