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.

Installing Android Studio

We are using the Developer Preview (version 0.4.0) of Android Studio at the time of writing. Installation details may change so visit the Android Studio site to check out the latest instructions and to download the relevant setup program.

From the Android Studio site:

Android Studio requires JDK 6 or greater (JRE alone is not sufficient). To check if you have JDK installed on a Mac (and which version), open a terminal and type javac -version. If JDK is not available or the version is lower than 6, download JDK from here.
To install Android Studio:
  1. Download the Android Studio package from above.
  2. Install Android Studio and the SDK tools:
    1. Launch the downloaded EXE file, android-studio-bundle-<version>.exe.
    2. Follow the setup wizard to install Android Studio.
      Known issue: On some Windows systems, the launcher script does not find where Java is installed. If you encounter this problem, you need to set an environment variable indicating the correct location.
      Select Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and add a new system variable JAVA_HOME that points to your JDK folder, for exampleC:\Program Files\Java\jdk1.7.0_21.
    Mac OS X:
    1. Open the downloaded DMG file, android-studio-bundle-<version>.dmg.
    2. Drag and drop Android Studio into the Applications folder.
      Known issue: Depending on your security settings, when you attempt to open Android Studio, you might see a warning that says the package is damaged and should be moved to the trash. If this happens, go to System Preferences > Security & Privacy and under Allow applications downloaded from, select Anywhere. Then open Android Studio again.
    1. Unpack the downloaded Tar file, android-studio-bundle-<version>.tgz, into an appropriate location for your applications.
    2. To launch Android Studio, navigate to the android-studio/bin/ directory in a terminal and execute
      You may want to add android-studio/bin/ to your PATH environmental variable so that you can start Android Studio from any directory.
That's it! You're ready to start developing apps with Android Studio.
Note: On Windows and Mac, the individual tools and other SDK packages are saved within the Android Studio application directory. To access the tools directly, use a terminal to navigate into the application and locate the sdk/directory. For example:
Windows: \Users\<user>\AppData\Local\Android\android-studio\sdk\
Mac: /Applications/Android\
For a list of some known issues, see

Wednesday, May 7, 2014

About Android Studio

At Google I/O 2013, Google launched a new Integrated Development Environment (IDE) based on IntelliJ IDEA, called Android Studio.

Android Studio provides integrated Android developer tools for development and debugging. On top of the capabilities you expect from IntelliJ, Android Studio offers:

  • Gradle-based build support.
  • Android-specific refactoring and quick fixes.
  • Lint tools to catch performance, usability, version compatibility and other problems.
  • ProGuard and app-signing capabilities.
  • Template-based wizards to create common Android designs and components.
  • A rich layout editor that allows you to drag-and-drop UI components, preview layouts on multiple screen configurations, and much more.
  • Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine as server-side components.

Android Studio includes a powerful code editor, which supports smart editing, advanced code refactoring, and deep static code analysis.

Smart editing includes inline resource lookups that make it easier to read your code. Code refactoring allows you to transform your code across the scope of the entire project.

Static code analysis helps you identify bugs quickly. On top of the hundreds of code inspections that IntelliJ IDEA provides, Google have added custom inspections. For example,  metadata to the Android APIs, that flag which methods can return null and which can’t, which constants are allowed for which methods, and so on. Android Studio uses that data to analyze your code and find potential errors.

Be aware that Android Studio is currently available as an early access preview. Several features are either incomplete or not yet implemented and you may encounter bugs. If you are not comfortable using an unfinished product, you may want to instead download (or continue to use) the ADT Bundle (Eclipse with the ADT Plugin).

Instructions for downloading the latest version of Android Studio are available here: Versions are available for download on Windows, Mac OS X and Linux.