Saturday, May 24, 2014

Your First Android App - Hello World!

1. Welcome to Android Studio

In this first tutorial we will do the traditional thing and write a "Hello World" Android app. This is very simple with Android Studio and doesn't require any actual coding since the default app says Hello World. Nevertheless, it does demonstrate how to build an Android Project from scratch and understand the options in the New Project Wizard.

Figure 1. Welcome to Android Studio

When you have installed and launched Android Studio, the first screen that you will see is shown in Figure 1. Clicking on "New Project" will start the New Project Wizard (Figure 2).

Figure 2. New Project Wizard

2. The New Project Wizard

The New Project Wizard has the following options:
  1. Application Name - This is the title of your application and what is shown in the Google Play store. It is also what is shown in the Manage Applications list in Settings. We have imaginatively used Tutorial1 for this first app. This name is stored within the strings.xml file under the variable "app_name" should you need to use it in your project.
  2. Module Name - The module name is only used within the Android Studio Integrated Development Environment (IDE). Some quick terminology, a project in Android Studio represents a complete Android app. Android Studio projects consist of one or more modules. A module is a component of your app that you can build, test, or debug independently. We only have one module in our app so just use the application name.
  3. Package Name - The package name is the unique identifier of your application. To ensure that it is unique, Android recommend that you use the reverse domain name of your organisation with an application identifier (e.g. It must be a valid Java package name, note the use of lower case for the application name. Users will not normally see the package name but it is associated with your app for its lifetime. It is how multiple versions of your app are considered the same.
  4. Project Location - Where the app source and build files will be located. This doesn't need to be anywhere special but for ease of finding and backup we put all of our projects in a directory called AndroidStudioProjects.
  5. Minimum Required SDK: This is the earliest version of Android that your application will support. The default is API 7: Android 2.1 (Eclair). Your choice is a tradeoff of supported devices vs new language features. According to Android, targeting API 8 or later will reach approximately 95% of the market. To get access to Ads using Google's Admob you will need at least API 8. For this app, we are not interested in supporting legacy devices so we will select the latest API 19: Android 4.4 (Kit Kat).
  6. Target SDK - This is the highest API that the application is known to work with. If you do not target the current version then your app could look dated. The latest version currently is API 19: Android 4.4 (Kit Kat).
  7. Compile With - The target API that you want to compile your code against. Typically this is the most recent version or the first version that supports all the API's that you want to access. As before we will select API 19.
  8. Language Level - Selects what Java language level you want to use. KitKat supports full Java 7, Gingerbread and up support Java 6 and older versions use Java 5, at least when it comes to the core Java API's. The wizard defaults to 6.0 which isn't a bad option, although Java 7 did introduce some nice new features, including:
  • Strings in switch
  • Binary literals and underscores in numeric literals
  • Multi-catch and more precise rethrow
  • Improved type inference for generic instance creation (diamond)
  • try-with-resources statement

3. Android App Themes

Figure 3. Holo Dark & Holo Light themes

The final option is the Theme. Themes are Android's mechanism for applying a consistent style to an app or activity. The style specifies the visual properties of the elements that make up your user interface, such as colour, height, padding and font size. To promote greater cohesion between all apps on the platform, Android provides two system themes that you can choose from when building apps:
  • Holo Light
  • Holo Dark
Applying these themes will go a long way in helping you to build apps that fit right into the general visual language of Android. Pick the system theme that best matches the needs and design aesthetics for your app. If your desire is to have a more distinct look for your app, using one of the system themes as a starting point for your customisations is a good idea. The system themes provide a solid foundation on top of which you can selectively implement your own visual stylings.

For this first app, select the default, Holo Light with Dark Action Bar. Click the Next button (bottom right of the Wizard screen) and the Select Icon (Figure 4) page is shown. For our first app we will just use the default icon provided.

Figure 4. Select Icon

4. Activity Template

Click Next on the Select Icon page to bring up the Activity Template shown in Figure 5.

Figure 5. Activity Template

Selecting the Blank Activity and clicking Next takes us to the Template Detail form (Figure 6). You can keep these all as default if you wish. Note that from Android Studio 0.3, a Fragment Layout is added by default.

Figure 6. Template Detail

Clicking Finish will generate our Hello World app. The first project build will take a while as Android Studio downloads Gradle. You will need internet access at this point. The Gradle download only happens on the first build. We will discuss Gradle further in a separate tutorial.

Once the project has completed its build, the Android Studio IDE will open up and should look something like Figure 7.

Figure 7. Android Studio IDE with Hello World App.

5. The Emulator

At this point you would probably like to run your app in the emulator or on a device. To do this, attach the device if you are going to use an actual phone or tablet for development and click on the green run button in the top tool bar (next to the drop down box with the app name - it looks like a play icon). This will compile your app, build it with Gradle and display the screen shown in Figure 8.

Figure 8.

If you have already configured a virtual device or have a real device connected to your development computer then you can select it and click Ok. We have a virtual device configured as a Nexus 7 (since this is our development tablet). Eventually, the emulator will launch your app and you should see the result shown in Figure 9. If this is the first time that you have launched the Emulator you may need to click the run button again after the Emulator has launched.

Figure 9. Emulator running Hello World app.

The first thing that you will notice is how slow the emulator is to launch a newly compiled app. For development purposes an actual device is MUCH quicker and is a good investment regardless. If you haven't set up a virtual device then we will show you how in the next tutorial.

No comments:

Post a Comment