Tag Archives: mobile development

Getting started with Android in IntelliJ 9

JetBrains currently has a milstone release, version 9 of their awesome IDE IntelliJ. One of the included features is support for Google’s Andriod mobile platform.

I’ve been a big fan of JetBrains’ products for a long time, especially their TeamCity build server…but more on that later!

To get started on Android development, head over to Google and download the SDK (version 1.6 at time of writing). Extract it to a location of your choice!

New Project

Create a new project in IntelliJ, selecting an Android module and give it an appropriate name.

Module Types

Module Types

On the next screen, the default os to create a ‘hello world’ activity. The default package is com.example, which you’ll want to change for future projects (I’ve yet to track down where it is set in the preferences..).

The hello world activity is a useful starting block when learning how the basic Android components work, or just starting a new Android project, since it will set up the basics for you.

You’ll also need to set up the Android SDK against the project in the top section, add and browse to the location of your downloaded SDK. I suggest setting the platform level to Android 1.6 (you’re able to select 1.5 or Google APIs as the platform  if you wish. Google APIs gives you some extra functionality such as the Maps external library).

The generated project structure looks a little like this:

'Hello World!' Project

'Hello World!' Project

The R.java class is auto-generated each time you build the project. It contains references to the resource files in your project, contained under the res directory.

It is not intended to edited (any changes you make would be overwritten on the next compile).

The resources directory, res, contains all the non-java resources that make up you application, including image and string resources, layout definitions and other configuration xml files.

For example, main.xml under the layout directory contains an xml definition of the screen layout. Pretty simple really, just a layout container and a text label (see xml below). The TextView element specifies its text attribute as “@string/hello” – a reference to strings.xml, and the value of the hello element.

The java file MyActivity contains just enough code to tell the application to write the contents of main.xml to the screen.

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
    <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello"
     />
</LinearLayout>

strings.xml:

<resources>
 <string name="hello">Hello World, Lop!</string>
 <string name="app_name"></string>
</resources>

Android AVD

To run your shiny new application, you have to set up a virtual device to run it on.

The SDK prvides a tool – <SDK>/tools/android – that works as a GUI and command line interface to manage devices.

You can manage the devices through IntelliJ also – edit the run configuration (which was automatically generated for you), and click the ellipses to the right of the drop-down.

Side-by-side: IntelliJ's configuration screen and Google's AVD utility

Side-by-side: IntelliJ's configuration screen and Google's AVD utility

I prefer to use the Google utility, because I find the AVD will take a minute or two to start up, and so I’ll launch it when I open my project, not when I want to run my application the first time.

Running..

Once your AVD is configured and running, you should be able to launch your application, and see your friendly message!

Your running application!

Your running application!

Next..

Check out some of the tutorials in the developer guide of other basic components that are available.

And also, read the application fundamentals to get your head around the Android architecture!

References:

Developing Android applications in maia

Google Developer GuideHello World example

Using decimal and numeric input fields in Android

IMPORTANT!
This blog has moved to http://blog.brasskazoo.com!

Say I have a measurement field in my app that takes a decimal:

android-decimal

Google’s dev guide says that floats are bad, because the embedded hardware does not have floating point number support like desktop CPUs do.

And creating objects should be avoided too, if possible, because it is an expensive operation in an environment with limited resources. Garbage collection of objects used in UI code can lead to ‘hiccups’ in the user experience too.

So if a float is out, that leaves us with double, right? The best solution appears to be this:

double value = Double.parseDouble(txtInput);

parseDouble I think is better than valueOf, since we probably don’t want to be doing any auto-unboxing.

Note: I include these lines in the layout xml to restrict the field to a single-line decimal input:

<EditText
    ....
    android:singleLine="true"
    android:inputType="numberDecimal"
    ...
/>

The inputType also sets the soft keyboard (popup keyboard) to the numeric keyboard.