Writing a simple Android App

Those are some of my notes on writing a simple android app using IntelliJ IDEA Ultimate.

Initially we need Java SDK (1.7 is preferred, since Gradle and the Android SDK does not officially support builds with 1.8 as of the time of writing and there are some issues with it) and the Android SDK

By setting up the gradle plugin we can get (almost) instantaneous support for earlier versions of Android.

In the SDK there are few major pointers:

SDK Manager (to download additional packages, in the MACOSX Pakage is in the tools folder)

Extras (Those are the needed support libraries older versions of Android)

We need to download all the needed packages via the SDK Manager:

$ cd /Users/andrey.voev/android-sdk-macosx/tools/
$ ./android

In my case I went with the bare minimum:
If you are using Windows, make sure to run the SDK Manager as an Admin to avoid some package installation problems.

And set up an AVD:

$ cd /Users/andrey.voev/android-sdk-macosx/tools/
$ ./android avd

Android comes with different API support levels, and usually the higher the API Support level, the better. I am using API 21 (Lollipop) here. The only drawback in selecting the highest API level is that there may be some backwards incompatibility with older Android versions if you are using the newest API, but Gradle and IntelliJ are very good with figuring those out for you.

In IntelliJ go for File->New Project->Android->Gradle: Android Module (If you don’t have Gradle yet, the IDE will download and install it for you). From here you can select which previous versions of Android you want to support. My best bet was API 11+, since supporting older devices means that you need to do (sometimes major) additional work. I’ve selected the “Create Activity” to save some typing for the initial functionality. Select “Blank Activity” on the next screen and leave the default values of “MainActivity” and “activity_main” on the screen after that.

The last screen will ask for name and location of the App. Make sure to select a location to which you have sufficient privileges as well. Gradle will create the project structure and the main activity.

Few important pointers:

src/main/AndroidManifest.xml – This is where most of the app settings are located. Name, icons, intents and all that can be changed from here.

com/andreyvoev/myapplication/app/MainActivity.java – Where the initial action takes place. This is the location to which we will be adding logic.

Where android:title=”@string/action_exit” sets the value for the action which will be defined below (almost everything that has the “@” annotation is a reference to some resource of the android framework). As you can see, the “Settings” menu is already generated for us, along with all the proper references throughout the app.

The difference here is that we’ve used android:icon=”@android:drawable/ic_menu_add” to display the option as an icon and app:showAsAction=”ifRoom” to show the icon in the Action Bar instead as a part of the drop-down menu. We are using the generated android:title=”@string/title_activity_add_item” as a title.

This is how it looks so far:

The next thing that we need to do is to associate the newly created activity to an action