Introducing an Android platform

Exclusive offer: get 50% off this eBook here
Android Development Tools for Eclipse

Android Development Tools for Eclipse — Save 50%

Set up, build, and publish Android projects quickly using Android Development Tools for Eclipse with this book and ebook

$13.99    $7.00
by Khirulnizam Abd Rahman Sanjay Shah | September 2013 | Java Open Source

In this article by Sanjay Shah and Khirulnizam Abd Rahman, authors of Android Development Tools for Eclipse, we will be introduced to an Android platform and its features, ADT for Eclipse, and various Fragments.

In simple terms, Android is a Linux-based operating system for touch screen devices developed by Android Inc., financed by Google, and later bought in the year 2005.The beta version of Android came back in November 2007 and the commercial Version 1.0 was released in September, 2008. As of 2013, over 500 million active devices use the Android OS worldwide. Before we proceed with the installation guide, there are several basic information an Android developer must know.

(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:

Folder

Functions

/src

The Java codes are placed in this folder.

/gen

It is generated automatically.

/assets

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.

/libs

It is an external library (normally in JAR).

/res

It contains images, layout, and global variables.

/drawable-xhdpi

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

/drawable-hdpi 

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

/drawable-mdpi

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

/drawable-ldpi 

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

/layout 

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

/menu

XML files for screen menu.

/values

It includes global constants.

/values-v11

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

/values-v14

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

AndroidManifest.xml

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:

Views

Description

CheckBoxPreference

It is a simple checkbox which returns true/false

ListPreference

It shows RadioGroup, where only 1 item can be selected

EditTextPreference

It shows dialog box to edit TextView, and returns String

RingTonePreference

It is a radioGroup that shows ringtone

PreferenceCategory

It is a category with preferences

Fragment

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 <filename>.keystore -alias <key-name>
-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.

Summary

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:


Android Development Tools for Eclipse Set up, build, and publish Android projects quickly using Android Development Tools for Eclipse with this book and ebook
Published: July 2013
eBook Price: $13.99
Book Price: $29.99
See more
Select your format and quantity:

About the Author :


Khirulnizam Abd Rahman

Khirulnizam Abd Rahman is a Computer Science lecturer in the faculty of Information Science and Technology, Selangor International Islamic College, Malaysia. He has been teaching programming since the year 2000.

He started publishing Android apps in the year 2010, and his apps among others are Malay Proverb Dictionary (Peribahasa) and m-Mathurat. Currently, he is working on the apps for Windows Phone Version 8. PHP, C#, and Java are also the programming languages that he is familiar with.

Sanjay Shah

Sanjay Shah has worked on diverse areas of application development across mobile and web platforms with more than 8 years of experience. He is currently working as a Software Architect and works in the area of Cloud Based Big Data Analytics combined with Distributed Cognition leveraging various Java based technologies He is fond of philosophy and enjoys life in Nepal, the land of highest peak in the world, Mt. Everest.

Books From Packt


 Android 3.0 Application Development Cookbook
Android 3.0 Application Development Cookbook

 Android Database Programming
Android Database Programming

Android 4: New Features for Application Development
Android 4: New Features for Application Development

Android Native Development Kit Cookbook
Android Native Development Kit Cookbook

Android User Interface Development: Beginner's Guide
Android User Interface Development: Beginner's Guide

Android Application Programming with OpenCV
Android Application Programming with OpenCV

Augmented Reality for Android Application Development
Augmented Reality for Android Application Development

Developing AR Games for iOS and Android
Developing AR Games for iOS and Android


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2
F
L
b
3
c
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software