Android Application

flosssnailsMobile - Wireless

Dec 10, 2013 (4 years and 21 days ago)

72 views

Android Programming

1

Outline


Installing Android Developing Environment


Developing in Eclipse, with ADT


Creating and running an Android Project


Hello World Example


2

Installing Android Developing Environment

1.
Prepare your development computer

To install Eclipse, you can download it from this location:

http://www.eclipse.org/downloads/

2.
Download and install the SDK starter package


Download SDK at
http://developer.android.com/sdk/index.html



Optionally, you may want to add the location of the SDK's primary
tools directory to your system PATH.

3.
Installing the ADT Plugin for Eclipse

4.
Adding Android Platforms and Other Components

5.
Exploring the SDK


3

Installing the ADT Plugin for Eclipse


Start Eclipse, then select
Help

>
Install New Software
.


In the Available Software dialog, click
Add...
.


In the Add Site dialog that appears, enter a name for the
remote site (for example, "Android Plugin") in the "Name"
field. In the "Location" field, enter this URL:


https://dl
-
ssl.google.com/android/eclipse/



Note: If you have trouble acquiring the plugin, you can try
using "http" in the URL, instead of "https" (https is
preferred for security reasons).


Click
OK
.

4

Installing the ADT Plugin for Eclipse


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
.


In the resulting Install Details dialog, the Android DDMS
and Android Development Tools features are listed. Click
Next

to read and accept the license agreement and install
any dependencies, then click
Finish
.


Restart Eclipse.


5

Adding Android Platforms and Other Components


The SDK repository offers these types of components:


SDK Tools

(pre
-
installed in the Android SDK starter package)


Contains the full set of SDK tools for developing, debugging, and
testing your application code and UI. You can read about the tools
in the
Dev Guide

and access them in the <sdk>/tools/ directory.


Android platforms



An SDK platform is available for every
production Android platform deployable to Android
-
powered
devices.


Each platform component includes a fully compliant Android
library and system image, sample code, emulator skins, and any
version specific tools.

6

Adding Android Platforms and Other Components


The SDK repository offers these types of components:


SDK Add
-
Ons



SDK add
-
ons provide a development
environment for specific Android external library or a customized
(but fully compliant) Android system image.


The Android SDK repository offers the Google APIs Add
-
On,
which gives your application access to powerful mapping
capabilities through the com.google.android.maps library.


USB Driver for Windows



Contains driver files that you can
install on your Windows computer, so that you can run and debug
your applications on an actual device


Samples



Contains the sample code and apps available for each
Android development platform. If you are just getting started with
Android development, make sure to download the samples to your
SDK.


Documentation



Contains a local copy of the latest multiversion
documentation for the Android framework API.

7

Adding Android Platforms and Other Components


The SDK repository offers these types of components:


SDK Add
-
Ons



SDK add
-
ons provide a development
environment for specific Android external library or a customized
(but fully compliant) Android system image.


The Android SDK repository offers the Google APIs Add
-
On,
which gives your application access to powerful mapping
capabilities through the com.google.android.maps library.


USB Driver for Windows



Contains driver files that you can
install on your Windows computer, so that you can run and debug
your applications on an actual device


Samples



Contains the sample code and apps available for each
Android development platform. If you are just getting started with
Android development, make sure to download the samples to your
SDK.


Documentation



Contains a local copy of the latest multiversion
documentation for the Android framework API.

8

Developing In Eclipse, with ADT


If you use Eclipse, the ADT plugin gives you an incredible
boost in developing Android applications:


It gives you access to other Android development tools from inside
the Eclipse IDE.


It provides a New Project Wizard, which helps you quickly create
and set up all of the basic files you'll need for a new Android
application.


It automates and simplifies the process of building your Android
application.


It provides an Android code editor that helps you write valid XML
for your Android manifest and resource files.


It will even export your project into a signed APK, which can be
distributed to users.


9

Developing In Eclipse, with ADT


To begin developing Android applications in the Eclipse
IDE with ADT, you first need to download the Eclipse
IDE and then download and install the ADT plugin. To do
so, follow the steps given in
Installing the ADT Plugin
.


If you are already developing applications using a version
of ADT earlier than 0.9, make sure to upgrade to the latest
version before continuing. See the guide to
Updating Your
ADT Plugin
.

10

Creating an Android Project


The ADT plugin provides a New Project Wizard that you
can use to quickly create a new Android project (or a
project from existing code). To create a new project:

1.
Select
File

>
New

>
Project
.

2.
Select
Android

>
Android Project
, and click
Next
.

3.
Select the contents for the project.

4.
Click
Finish
.


11

Creating an Android Project


Once you complete the New Project Wizard, ADT creates
the following folders and files in your new project:


src/ Includes your stub Activity Java file. All other Java files for
your application go here.


<Android Version>
/ (e.g., Android 1.1/) Includes the android.jar
file that your application will build against. This is determined by
the build target that you have chosen in the
New Project Wizard
.


gen/ This contains the Java files generated by ADT, such as your
R.java file and interfaces created from AIDL files.


assets/ This is empty. You can use it to store raw asset files. See
Resources and Assets
.

12

Creating an Android Project


Once you complete the New Project Wizard, ADT creates
the following folders and files in your new project:


res/ A folder for your application resources, such as drawable files,
layout files, string values, etc. See
Resources and Assets
.


AndroidManifest.xml The Android Manifest for your project. See
The AndroidManifest.xml File
.


default.properties This file contains project settings, such as the
build target. This files is integral to the project, as such, it should
be maintained in a Source Revision Control system. It should
never be edited manually


to edit project properties, right
-
click
the project folder and select "Properties".

13

Running Your Application


Before you can run your application on the Android
Emulator, you
must

create an Android Virtual Device
(AVD). An AVD is a configuration that specifies the
Android platform to be used on the emulator. You can
read more in the
Android Virtual Devices

document, but if
you just want to get started, follow the simple guide below
to create an AVD.


If you will be running your applications only on actual
device hardware, you do not need an AVD


see
Developing On a Device

for information on running your
application.

14

Creating an AVD


With ADT 0.9.3 and above, the Android SDK and AVD
Manager provides a simple graphical interface for creating
and managing AVDs. (If you're using ADT version 0.9.1
or older, you must use the android tool to create your
AVDs

read the AVD guide to
Creating an AVD
.)


To create an AVD with the AVD Manager:


Select
Window > Android SDK and AVD Manager
, or click the
Android SDK and AVD Manager icon (a black device) in the
Eclipse toolbar.


In the Virtual Devices panel, you'll see a list of existing AVDs.
Click
New

to create a new AVD.


Fill in the details for the AVD. Give it a name, a platform target,
an SD card image (optional), and a skin (HVGA is default).


Click
Create AVD
.


15

Running your application


To run (or debug) your application, select
Run

>
Run

(or
Run

>
Debug
) from the Eclipse main menu. The ADT
plugin will automatically create a default launch
configuration for the project.When you choose to run or
debug your application, Eclipse will perform the
following:


Compile the project (if there have been changes since the last
build).


Create a default launch configuration (if one does not already exist
for the project).


Install and start the application on an emulator or device (based on
the Deployment Target defined by the run configuration). By
default, Android application run configurations use an "automatic
target" mode for selecting a device target.


16

Creating a Run Configuration


The run configuration specifies the project to run, the
Activity to start, the emulator options to use, and so on.


To create or modify a launch configuration, follow these
steps as appropriate for your Eclipse version:

1.
Open the run configuration manager.


In Eclipse 3.4 (Ganymede), select
Run
>
Run Configurations

(or
Debug Configurations
)

17

Creating a Run Configuration

2.
Expand the
Android Application

item and create a new
configuration or open an existing one.


To create a new configuration:


Select
Android Application

and click the
New launch
configuration

icon above the list (or, right
-
click
Android
Application

and click
New
).


Enter a Name for your configuration.


In the Android tab, browse and select the project you'd like to run
with the configuration.


To open an existing configuration, select the configuration name
from the list nested below
Android Application
.

3.
Adjust your desired launch configuration settings.

18

Automatic and manual target modes


By default, a run configuration uses the
automatic

target
mode in order to select an AVD. In this mode, ADT will
select an AVD for the application in the following
manner:


If there's a device or emulator already running and its AVD
configuration meets the requirements of the application's build
target, the application is installed and run upon it.


If there's more than one device or emulator running, each of which
meets the requirements of the build target, a "device chooser" is
shown to let you select which device to use.


If there are no devices or emulators running that meet the
requirements of the build target, ADT looks at the available AVDs
upon which the application is installed and run.


If all else fails, the application will not be run and you will see a
console.


19

Signing your Applications


As you begin developing Android applications, understand
that all Android applications must be digitally signed
before the system will install them on an emulator or an
actual device.


There are two ways to do this: with a debug key (for
immediate testing on an emulator or development device)
or with a private key (for application distribution).


The ADT plugin helps you get started quickly by signing
your .apk files with a debug key, prior to installing them
on an emulator or development device.

20

Signing your Applications


However, please note that if you intend to publish your
application, you
must

sign the application with your own
private key, rather than the debug key generated by the
SDK tools.


Please read
Signing Your Applications
, which provides a
thorough guide to application signing on Android and
what it means to you as an Android application developer.
The document also includes a guide to exporting and
signing your application with the ADT's Export Wizard.


21

Hello, World


Before you start, you should already have the very latest
SDK installed, and if you're using Eclipse, you should
have installed the ADT plugin as well. If you have not
installed these, see
Installing the Android SDK

and return
here when you've completed the installation.

22

Hello, World

1.
From Eclipse, select
File > New > Project
. If the ADT
Plugin for Eclipse has been successfully installed, the
resulting dialog should have a folder labeled "Android"
which should contain "Android Project". (After you create
one or more Android projects, an entry for "Android XML
File" will also be available.)

2.
Select "Android Project" and click
Next
.

23

Hello, World


Fill in the project details with the following values:


Project name:

HelloAndroid


Application name:

Hello, Android


Package name:

com.example.helloandroid (or your own private
namespace)


Create Activity:

HelloAndroid


Min SDK Version:

2


Click
Finish
.


24

Hello, World


Your Android project is now ready. It should be visible in
the Package Explorer on the left. Open the
HelloAndroid.java file, located inside
HelloAndroid > src
> com.example.helloandroid
). It should look like this:


package com.example.helloandroid;


import android.app.Activity;

import android.os.Bundle;


public class HelloAndroid extends Activity {





/** Called when the activity is first created. */





@Override





public void onCreate(Bundle savedInstanceState) {









super.onCreate(savedInstanceState);









setContentView(R.layout.main);





}


25

Hello, World


Notice that the class is based on the
Activity

class. An
Activity is a single application entity that is used to
perform actions. An application may have many separate
activities, but the user interacts with them one at a time.
The
onCreate()

method will be called by the Android
system when your Activity starts


it is where you should
perform all initialization and UI setup. An activity is not
required to have a user interface, but usually will.

26

Hello, World
-

Construct the UI


Take a look at the revised code below and then make the
same changes to your HelloAndroid class. The bold items
are lines that have been added.


package com.android.helloandroid;


import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;


public class HelloAndroid extends Activity {




/** Called when the activity is first created. */




@Override




public void onCreate(Bundle savedInstanceState) {








super.onCreate(savedInstanceState);








TextView tv = new TextView(this);








tv.setText("Hello, Android");








setContentView(tv);




}

}

27

Hello, World


An Android user interface is composed of hierarchies of
objects called Views.


A
View

is a drawable object used as an element in your UI layout,
such as a button, image, or (in this case) a text label.


Each of these objects is a subclass of the View class and the
subclass that handles text is
TextView
.


In this change, you create a TextView with the class
constructor, which accepts an Android
Context

instance as
its parameter.


A Context is a handle to the system; it provides services like
resolving resources, obtaining access to databases and preferences,
and so on.


The Activity class inherits from Context, and because your
HelloAndroid class is a subclass of Activity, it is also a Context.
So, you can pass this as your Context reference to the TextView.


28

Hello, World


Next, you define the text content with
setText(CharSequence) setText()
.


Finally, you pass the TextView to
setContentView()

in
order to display it as the content for the Activity UI. If
your Activity doesn't call this method, then no UI is
present and the system will display a blank screen.


There it is


"Hello, World" in Android! The next step, of
course, is to see it running.


29

Hello, World


Run the Application

The Eclipse plugin makes it very easy to run your applications:


Select
Run > Run
.


Select "Android Application".


To learn more about creating and editing run
configurations in Eclipse, refer to
Developing In Eclipse,
with ADT
.


The Eclipse ADT will automatically create a new run
configuration for your project and the Android Emulator
will automatically launch. Once the emulator is booted up,
your application will appear after a moment. You should
now see something like this:


30

Hello, World


The "Hello, Android" you see in the grey bar is actually
the application title. The Eclipse plugin creates this
automatically (the string is defined in the
res/values/strings.xml file and referenced by your
AndroidManifest.xml file). The text below the title is the
actual text that you have created in the TextView object.


That concludes the basic "Hello World" tutorial, but you
should continue reading for some more valuable
information about developing Android applications.


31

Upgrade the UI to an XML Layout


The "Hello, World" example you just completed uses what
is called a "programmatic" UI layout. This means that you
constructed and built your application's UI directly in
source code. If you've done much UI programming, you're
probably familiar with how brittle that approach can
sometimes be: small changes in layout can result in big
source
-
code headaches. It's also very easy to forget to
properly connect Views together, which can result in
errors in your layout and wasted time debugging your
code.


That's why Android provides an alternate UI construction
model: XML
-
based layout files. The easiest way to
explain this concept is to show an example. Here's an
XML layout file that is identical in behavior to the
programmatically
-
constructed example:


32