9 min read

(For more resources related to this topic, see here.)

Introducing an Android app

Mobile software application that runs on Android is an Android app. The apps use the extension of .apk as the installer file extension. There are several popular examples of mobile apps, such as Foursquare, Angry Birds, and Fruit Ninja.

Primarily in an Eclipse environment, we use Java, which is then compiled into Dalvik bytecode (not the ordinary Java bytecode). Android provides Dalvik virtual machine (DVM) inside Android (not Java virtual machine JVM). Dalvik VM does not ally with Java SE and Java ME libraries, and is built on an Apache Harmony java implementation.

What is Dalvik virtual machine?

Dalvik VM is a register-based architecture, authored by Dan Bornstein. It is being optimized for low memory requirements, and the virtual machine was slimmed down to use less space and less power consumption.

Preparing for Android development Eclipse ADT

In this part of the article, we will see how to install the development environment for Android on Eclipse Juno (4.2). Eclipse is a major IDE for Android development. We need to install an Eclipse extension ADT Android Development Toolkit (ADT) for development of the Android application.

Debugging an Android project

It is advisable to use the Log class for this purpose, the reason being we can filter, print different colors, and define log types. This could be one of the ways of debugging your program, by displaying variables value or parameters. To use Log, import android.util.Log, and use one the following methods to print messages to LogCat:

v(String, String) (verbose) d(String, String) (debug) i(String, String) (information) w(String, String) (warning) e(String, String) (error)

LogCat is used to view the internal log of the Android system. It is useful to trace any activity happening inside the device or emulator through the Android Debug Bridge (ADB).

The Android project structure

The following table illustrates the brief description of the important folders and files available in an Android project:




The Java codes are placed in this folder.


It is generated automatically.


You can put your fonts, videos, and sounds here. It is more like a filesystem, and can also place CSS, JavaScript files, and so on.


It is an external library (normally in JAR).


It contains images, layout, and global variables.


It is used for extra high specification devices (for example, Tablet, Galaxy SIII, HTC One X).


It is used for high specification phones (for example, SGSI, SGSII)


It is used for medium specification phones (for example, Galaxy W and HTC Desire).


It is used for low specification phones (for example: Galaxy Y and HTC WildFire).


It includes all the XML file for the screen(s) layout.


XML files for screen menu.


It includes global constants.


These are template styles definitions for devices with Honeycomb (Android API level 11).


These are template styles definitions for devices with ICS (Android API level 14).


This is one of important files to define the apps. This is the first file located by the Android OS in order to run the app. It contains the app’s properties, activity declarations, and list of permissions.


Dalvik Debug Monitor Server (DDMS)

DDMS is a must have tool to view the emulator/device activities. To access DDMS in the Eclipse, navigate to Windows | Open Perspective | Other, and then choose DDMS. By default it is available in the Android SDK (it’s inside the folder android-sdk/tools by the file ddms). From this perspective the following aspects are available:

  • Devices: The list of the devices and AVD that are connected to ADB
  • Emulator control: It helps to carry out device functions
  • LogCat: It views real-time system log messages
  • Threads: It gives an idea of currently running threads within a VM
  • Heap: It shows heap usage by application
  • Allocation tracker: It provides information on memory allocation of objects
  • File explorer: It explores the device filesystem

Creating a new Android project using Eclipse ADT

To create a new Android project in Eclipse, navigate to File | New | Project. A new project window will appear, from there choose Android | Android Application Project from the list. Then click on the Next button.

  • Application Name: This is the name of your application, it will appear side-by-side to the launcher icon. Choose a project name that relevant to your application.
  • Project Name: This is typically similar to your application name. Avoid having the same name with existing projects in Eclipse, it is not permitted.
  • Package Name: This is the package name of the application. It will act as an ID in the Google Play app store if we wish to publish. Typically it will be the reverse of your domain name if we have one (since this is unique) followed by the application name, and a valid Java package name else we can have anything now and refactor it before publishing.

Running the application on an Android device

To run and deploy on real device, first install the driver of the device. This varies as per device model and manufacturer.

These are a few links you could refer to:

Make sure the Android phone is connected to the computer through the USB cable. To check whether the phone is properly connected to your PC and in debug mode, please switch to the DDMS perspective.

Adding multiple activity in Android application

This exercise is to add an information screen on the SimpleNumb3r5 app. The information regarding the developer, e-mail, Facebook fan page, and other information is displayed. Since the screen contains a lot of text information including several pictures, so we make use of an HTML page as our approach here:

  1. Create an activity class to handle the new screen. Open the src folder, right-click on the package name (net.kerul.SimpleNumb3r5), and choose New | Other… from the selections, choose to add a new Android activity, and click on the Next button. Then, choose a blank activity and click on Next.
  2. Set the activity name as Info, and the wizard will suggest the screen layout as info_activity. Click on the Finish button.

Adding the RadioGroup or RadioButton controls

Android SDK provides two types of radio controls to be used in conjunction, where only one control can be chosen at a given time. RadioGroup (Android widget RadioGroup) is used to encapsulate a set of RadioButton controls for this purpose.

Defining the preference screen

Preferences are an important aspect of the Android applications. It allows users to have the choice to modify and personalize it. Preferences can be set in two ways: first method is to create the preferences.xml file in the res/xml directory, and second method is to set the preferences from the code. We will use the former also the easier one, by creating the preferences.xml file.

Usually, there are five different preference views as listed in the following table:




It is a simple checkbox which returns true/false


It shows RadioGroup, where only 1 item can be selected


It shows dialog box to edit TextView, and returns String


It is a radioGroup that shows ringtone


It is a category with preferences


A fragment is an independent component that can be connected to an Activity or simply is subactivity. Typically it defines a part of UI but can also exist with no user interface, that is, headless. An instance of fragment must exist within an activity.

Fragments ease the reuse of components for different layouts. Fragments are the way to support UI variances across different types of screens. The most popular use is of building single pane layouts for phone and multipane layouts for tablets (large screens).

Adding an external library Android project – AdMob

An Android application cannot achieve everything on its own, it will always need the company of external jars/libraries to achieve different goals and serve various purposes. Almost every free Android application published on store has advertisement embedded in it, which makes use of external component to achieve it. Incorporating advertisement in the Android application is a vital aspect of today’s application development. In this article, we will continue on our DistanceConverter application, and make use of the external library, AdMob to incorporate advertisement in our application.

Adding the AdMob SDK to the project

Let’s extract the previously downloaded AdMob SDK zip file, and we should get the folder GoogleAdMobAdsSdkAndroid-6.*.*, under that folder there is GoogleAdMobAdsSdk-6.x.x.jar. You should copy this JAR file in the libs folder of the project.

Signing and distributing APK

The Android package (APK), in simple terms is similar to the runnable JAR or executable file (on Windows OS) which consists of everything that is needed to run the application.

The Android ecosystem uses a virtual machine, that is, Dalvik virtual machine (DVM) to run the Java applications. Dalvik uses its own bytecode which is quite different from the Java bytecode.

Generating a private key

An android application must be signed with our own private key. It identifies a person, corporate, or entity associated with the application. This can be generated using the program keytool from the Java SDK. The following command is used for generating the key:

keytool -genkey -v -keystore .keystore -alias
-keyalg RSA -keysize 2048 -validity 10000

We can use different key for each published application, and specify different name to identify it. Also, Google expects validity of at least 25 years or more. A very important thing to consider is to keep back up and securely store key, because once it is compromised it impossible to update an already published application.

Publishing to Google Play

Publishing at Google Play is very simple and involves register for Google play. You just have to visit and register it at https://play.google.com/. It requires $25 USD to register, and is fairly straight and can take a few days until you get the final access.


In this article, we learned how to install the Eclipse Juno (the IDE), the Android SDK and the testing platform.

Also, we learned about the fragment and its usage, and used it to have different layouts for landscape mode for our application DistanceConverter. We also learned about handling different screen types and persisting state during screen mode changes.

Resources for Article:

Further resources on this subject:

Subscribe to the weekly Packt Hub newsletter

* indicates required


Please enter your comment!
Please enter your name here