Tuesday, July 10, 2012

Android Quick Start

Android Training

Do you want to know the secrets of Android Mobile Programming and Development and use it for the advancement of your career in IT? Do you have problems in your thesis or research about Mobile apps or games? Here is an instant solution for your problem. Avail the Android Mobile Programming and Development Training package of Prof Erwin Globio and get free tips about Android Mobile Platform Secrets. For more free information about Android and Android Training visit the following sites: http://erwinglobio.sulit.com.ph/http://erwinglobio.multiply.com/ http://erwinglobio.wordpress.com/. You may contact the Android Trainer at 09393741359 or 09323956678. Call now and be one of the Android Experts.


In this chapter, you will learn how to set up your environment for Android develop-
ment. I’ll go beyond just listing where you can download the software, and will cover
some of the best practices in getting set up. I’ll look at development operating system
choices as well as the Android tools available. You will see the good, the bad, and the
ugly of the various tool and platform choices that you’re about to make (or that some-
one else has already made for you).
By the end of this chapter, you will have your entire development environment set up.
You’ll be able to write a Hello World application, build it, and run it on the emulator
(or a physical device, if you want).
I’m going to use ~ to refer to your home directory. On Mac OS X, that’s
typically something like /Users/marko. On Linux, it would be /home/
marko, and on Windows Vista and 7, C:\Users\marko (in Windows XP,
it would be C:\Documents and Settings\marko). Also, I’m going to use
Unix-style forward slashes and not Windows backslashes to denote file
path separators.
So, if you’re on Windows, just change ~ to C:\Users\YourUserName
and / to \. Other than that, everything should be pretty much for different
operating systems, regardless of whether you use OS X, Linux, or
Windows.

Installing the Android SDK
The Android Software Development Kit (SDK) is all you need to develop applications
for Android. The SDK comes with a set of tools as well as a platform to run it and see
it all work. You can download the Android SDK for your particular platform from the
Android SDK Download page.


Once you download it, unzip (or on Linux, untar) it into a folder that is easy to get to.
Further examples in the book will assume your SDK is in the folder ~/android-sdk. If
it’s in a different location, use that location instead of ~/android-sdk. For example:

Windows
    C:\apps\android-sdk-windows

Linux
    /home/YourUserName/android-sdk-linux_86

Mac OS X
    /Users/YourUserName/android-sdk-mac_86

For Windows users, I strongly recommend choosing directories without
spaces in them. This is because we’ll be doing work on the command
line and spaces just complicate things. Because the Windows XP home
directory is in C:\Documents and Settings, I would recommend putting
android-sdk in a top-level directory that you create, such as C:\apps.
However, on Windows Vista or 7, you can simply extract android-sdk
into C:\Users\YourUserName.

Setting Up a PATH to Tools
The Android SDK has a folder that contains all its major tools. Since we’re going to use
these tools from the command line, it is very helpful to add your ~/android-sdk/tools/
and your ~/android-skd/platform-tools/ directories to your system PATH variable. This
will make it easier to access your tools without having to navigate to their specific
location every single time.

Details for setting up the PATH variable depend on the platform; see step 2 of the docu-
ment “Installing Android SDK”.

Eclipse is an open source collection of programming tools originally created by IBM
for Java. Nowadays, most developers in the Java community favor Eclipse as their
Integrated Development Environment (IDE) of choice. Eclipse lives at http://eclipse.org.
Eclipse has a lot of time-saving features, which I’ll be pointing out as we continue. Keep
in mind that, although powerful, Eclipse tends to be very resource-hungry, and so you
might want to restart it once a day if it starts running sluggishly.
Although you can do Android development with any favorite text editor or integrated
development environment (IDE), most developers seem to be using Eclipse, and thus
that’s what I use in this book.


If you choose not to use Eclipse, please refer to “Developing in Other IDEs”.

Download Eclipse at http://www.eclipse.org/downloads/. I recommend Eclipse IDE for
Java Developers (not the twice-as-large Eclipse for Java EE Developers). You can install
it in any directory you’d like.

Eclipse Workspace
Eclipse organizes all your work by projects. Projects are placed in a workspace, which
is a location you choose. So, where you put your workspace is significant. I recommend
~/workspace as a simple place for your code. On Windows, however, I recommend
storing your workspace in a directory that doesn’t have spaces in it (they complicate
anything you might do at the command line). C:\workspace is a good choice for Win-
dows users.

Setting Up Android Development Tools

You also need to set up Android Tools for Eclipse. The instructions are:
1. Start Eclipse, then select Help→Install New Software (see Figure 3-1).
2. In the Available Software dialog, click Add.
3. In the Add Site dialog that appears, enter a name for the remote site (for example,
   “Android Plugin”) in the “Name” field.
4. In the “Location” field, enter this URL: https://dl-ssl.google.com/android/
   eclipse/.
5. Click OK.
6. Back in the Available Software view, you should now see “Developer Tools” added
   to the list. Select the checkbox next to Developer Tools, which will automatically
   select the nested tools Android DDMS and Android Development Tools. Click
   Next.
7. In the resulting Install Details dialog, the Android DDMS and Android Develop-
   ment Tools features are listed. Click Next to read and accept the license agreement
   and install any dependencies, then click Finish.
8. Restart Eclipse.


If you have trouble downloading the plug-in, you can try using “http”
in the URL instead of “https” (https is preferred for security reasons).



Hello, World
To make sure everything is set up properly, we’re going to write a simple Hello World
program. As a matter of fact, there’s not much for us to write, but a lot to understand.
This is because Eclipse will create the project shell for us from some predefined
templates.


Creating a New Project

In Eclipse, choose File→New→Android Project. Sometimes (especially the first time you
run Eclipse) the Android tools may not be appear there right away. They should show
up in the future after you’ve used them for the first time. If Android Project is not an
option under File→New, choose Other and look for Android Project in there.


In the new project dialog window, fill out the following:

1. “Project name” is an Eclipse construct. Eclipse organizes everything into projects.
   A project name should be one word. I like to use the CamelCase naming convention
   here. Go ahead and type HelloWorld.
2. Next, you need to choose the build target. The build target tells the build tools
   which version of the Android platform you are building for. In here you should see
   a list of available platforms and add-ons you have installed as part of your SDK.
   Go ahead and pick one of the newer ones, such as Android 2.2 (but don’t choose
   the targets named Google APIs—those are Google’s proprietary extensions to the
   Android platform). For our purposes, we’ll stick to Android Open Source versions
   of the Android platform.
3. You need to fill out your project properties next. The application name is the plain
   English name of your application. Go ahead and enter something like Hello,
   World!!!.
4. The package name is a Java construct. In Java, all source code is organized into
   packages. Packages are important because, among other things, they specify the
   visibility of objects between the various Java classes in your project. In Android,
   packages are also important for application signing purposes. Your package name
   should be the reverse of your domain name with optional subdomains. I might use
   com.example.calculator if I were building a calculator app and my domain name
   was example.com. I’m going to be using com.marakana for my package name here.
5. You can optionally specify an activity. I haven’t covered activities yet (you’ll learn
   about them in Chapter 6), but think of them as corresponding to the various screens
   in your application. An activity is going to be represented by a Java class, and
   therefore its name should adhere to Java class naming conventions: start with an
   upper-case letter and use CamelCase to separate words. So, type HelloWorld for
   your activity name.
6. The minimum SDK version is the minimum version of Android—as represented
   by API level—that is required for the device to run this application. You want this
   number to be as low as possible so that your app can run on as many devices as
   possible. I’m going to put 8 here to represent Android 2.2, which I know I have
   installed.


Finally, click on the Finish button, and Eclipse will create your project. Let’s look at
the various files that this process created.



Manifest File
The manifest file glues everything together. It is this file that explains what the appli-
cation consists of, what all its main building blocks are, what permissions it requires.

Example 3-1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.marakana" android:versionCode="1" android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloWorld" android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>


Layout XML Code

The layout file specifies the layout of your screen. In this case, shown in Example 3-2,
we have only one screen, and it’s loaded by the HelloWorld.java code seen in Exam-
ple 3-5.
Example 3-2. res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>


Strings

This is another XML file that contains all the text that your application uses. For ex-
ample, the names of buttons, labels, default text, and similar types of strings go into
this file. This is the best practice for separating the concerns of various files, even if they
are XML files. In other words, layout XML is responsible for the layout of widgets, but
strings XML is responsible for their textual content (see Example 3-3).
Example 3-3. res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloWorld!</string>
    <string name="app_name">Hello, World!!!</string>
</resources>


The R File

The R file is the glue between the world of Java and the world of resources (see Exam-
ple 3-4). It is an automatically generated file, and as such, you never modify it. It is
recreated every time you change anything in the res directory, for example, when you
add an image or XML file.
You don’t need to look at this file much. We will use the data in it quite a bit, but we’ll
use Eclipse to help us refer to values stored in this file.

Example 3-4. gen/com/marakana/R.java


/* AUTO-GENERATED FILE. DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found. It
 * should not be modified by hand.
 */
package com.marakana;
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}


Java Source Code
The Java code is what drives everything. This is the code that ultimately gets converted
to a Dalvik executable and runs your application (see Example 3-5).

Example 3-5. HelloWorld.java

package com.marakana;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorld extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


setContentView(R.layout.main);
}
}


The Emulator

Running your application on a physical device versus an emulated device is pretty much
the same thing. That is because the emulator is an actual code emulator, meaning it
runs the same code base as the actual device, all the way down to the machine layer.
A simulator and an emulator sound very similar, but are fundamentally
different. To emulate means to imitate the machine executing the binary
code. So, an emulator is sort of like a virtual machine. A simulator merely
simulates the behavior of the code at a higher level. Android SDK ships
with a true emulator, based on QEMU.

To use the emulator, we’ll have to create an Android Virtual Device (AVD). The easiest
way to do that is to start the android tool via Eclipse.

To create a new AVD, start the tool called Android SDK and AVD Manager (see Fig-
  icon or via theure 3-3). You can start this tool from Eclipse by clicking on the
command line by starting the tool called android, which is located in your SDK/tools
directory.


From within the Android SDK and AVD Manager window, choosing “New…” pops
up a Create New AVD dialog window (see Figure 3-4). In this dialog, you specify the

parameters for your new AVD. The name can be any name you choose. The target
designates which version of Android you want installed on this particular AVD. The
list of possible targets is based on platforms and add-ons that you have installed into
your SDK. If you don’t have any targets, go back to the Android SDK and AVD Manager
window and choose the “Available packages” tab to install at least one platform, for
example, Android 2.3 - API level 9.
Each AVD can have an SD card. You can just specify a number here for your built-in
card, in megabytes. The skin is the look and feel of your device as well as its form factor.
The Hardware option lets you fine-tune what this AVD does and doesn’t support.


An Emulator Versus a Physical Phone

For the most part, running your application on the emulator is identical to running it
on a physical phone. There are some notable exceptions, mostly things that are just
hard to virtualize, such as sensors. Other hardware-related features such as telephony
and location services, can be simulated in the emulator.



Do you want to know the secrets of Android Mobile Programming and Development and use it for the advancement of your career in IT? Do you have problems in your thesis or research about Mobile apps or games? Here is an instant solution for your problem. Avail the Android Mobile Programming and Development Training package of Prof Erwin Globio and get free tips about Android Mobile Platform Secrets. For more free information about Android and Android Training visit the following sites: http://erwinglobio.sulit.com.ph/http://erwinglobio.multiply.com/ http://erwinglobio.wordpress.com/. You may contact the Android Trainer at 09393741359 or 09323956678. Call now and be one of the Android Experts.

Android Workshop Manila