Paper See how to create an example web service in Android. Page 174,  Chapter 6 Getting Started with Android – Alternative Formats See how to create an exa

Paper See how to create an example web service in Android. Page 174,  Chapter 6 Getting Started with Android – Alternative Formats
See how to create an example web service in iOS. Page 214,  Chapter 7 Getting Started with iOS – Alternative Formats

For submission : Read carefully about 2 different approaches for creating a web service from above. Then

Don't use plagiarized sources. Get Your Custom Essay on
Paper See how to create an example web service in Android. Page 174,  Chapter 6 Getting Started with Android – Alternative Formats See how to create an exa
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

(a) compare the two approaches and

(b) explain 2 advantages of each approach relative to the other. Getting Started with Android
WHAT’S IN THIS CHAPTER?

! Deciding to target Android as your mobile platform

! Getting the tools you need to develop Android

! Creating a new project

! Creating the Derby project in Android

Android Inc. was initially started in 2003, out of a frustration with the smartphone market as
it existed at the time. It was acquired by Google in 2005.

The hardware side of Android is supported by the Open Handset Alliance (OHA), which
is a conglomeration of many handset manufacturers, and the software is maintained by the
Android Open Source Project, which is led by Google.

Android had its fi rst major release in late 2008; the fi rst major phone company to support it
was T-Mobile, and the original handset was the HTC Dream (G1).

The Android OS was built on a modifi ed Linux kernel and applications are written in Java. By
using Java as the development framework for the applications, Android enables you to develop
your application on all major platforms.

By leveraging the Eclipse IDE, Android affords the user almost the exact same user experience
for development on all major OS platforms.

Additionally, when researching Android you may come across the name Dalvik. It is the
virtual machine that runs on the Android device, and your applications run within it. What
does this mean to developers? Because your applications run inside this virtual space, it
provides a level of security to the base OS. Also, Dalvik has been designed with performance
in mind. As of Android 2.2 it also provides just-in-time compilation to your apps (because
Dalvik requires specially compiled .dex fi les and not just the standard .class fi les generated
in a normal Java compilation).

6

c06.indd 151c06.indd 151 28/07/12 6:01 PM28/07/12 6:01 PM

152 ” CHAPTER 6 GETTING STARTED WITH ANDROID

WHY TARGET ANDROID?
Among the many reasons to target the Android platform, fi rst and foremost is cost. On average
you can get an Android smartphone for a fraction of the cost of an iPhone. They may not have
commensurate features, but thrift is a major component for new smartphone buyers.

Next is fl exibility. More types of Android devices are available, so if your application fi ts a specifi c
niche market, there is probably a device that would support your needs already in production. At
the time of writing, there are effectively two iOS devices (iPhone/iPod touch and iPad); four if you
include the retina display versions, versus roughly 15 form factors to develop for.

If you are already a Java developer, adding Android to your repertoire is a snap. What Java is to
Android, Cocoa is to CocoaTouch and C# is to Silverlight. All of the frameworks that mobile
developers use are a combination of subsets and supersets of the functionality of a given technology.

Identifying an application that exists on another platform but does not yet exist on Android is
another perfectly good reason to target Android. That being said, you should do some research,
because if a developer has targeted iOS or BlackBerry as the primary platform, you have to assume
that Android is potentially on the horizon.

WHO SUPPORTS ANDROID?
HTC, LG, Motorola, and Samsung are the major players in the Android smartphone market.
Archos, Dell, Samsung, and Toshiba hold the largest pieces of the Android tablet market. You
should note that Amazon’s Kindle Fire and Nook Color are up-and-comers and use a customized
version of the Android tablet (Version 3) OS on their devices.

ANDROID AS COMPETITION TO ITSELF
Because Android was designed to be run on many different types of devices, created by many
different manufacturers (as opposed to the closed system that Apple has maintained), it has left
itself open to the will of said manufacturers. Because of the open nature of the Android OS, it is
commonplace for manufacturers to create vendor-specifi c builds of Android, and when this happens
you are beholden to them for OS updates. Additionally in these custom builds, vendor-specifi c
limitations have arisen such as the vendor-specifi c market. You then have another hurdle to cross
when releasing your application for sale to the public because some devices may not be able to
purchase it because of these limitations.

Another issue that has cropped up is the lack of over-the-air (OTA) distribution of OS updates by
cellular carriers. Often your device is perfectly capable of running a later version of the Android
software, but carriers are often slow to distribute that to their customers.

Multiple Markets and Market Locks
Depending on your version of Android, and depending on the manufacturer of a given device, you
may fi nd yourself locked into using a vendor-specifi c Android marketplace. Additionally, application

c06.indd 152c06.indd 152 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 153

vendors can list their application not only on Google Play or vendor-specifi c marketplaces, but
also on the Amazon App Store. You often fi nd on cheap and imported Android devices a version of
Google Play that is maintained by the manufacturer. They pick and choose what applications are
available from the whole set in the marketplace. You should develop as you expect to be available
to all Android devices; just note when purchasing large quantities for an enterprise deployment that
you will have to watch out for these inconsistencies.

The version of the Android SDK that you need to support depends on what devices you want to
support. If you want to target most phones available right now, you should support Android 2.2
or 2.3. “Gingerbread” (2.3) is the last widely available version for those devices. The Android 3.x
versions are for tablets, such as the Samsung Galaxy Tab. The Android 4.x versions are the newest,
and are a combination of the Android 2.x and Android 3.x functionality meant to pull back on the
version splintering seen in devices, but not many devices currently in release support it.

Once you have decided on a version to deploy your application against, you need to set up your
development environment. In the next section you will learn all about confi guring your IDE, Java
and Android SDKs, and building emulators.

GETTING THE TOOLS YOU NEED
This section paraphrases the installation instructions from the Android Developer section, and we
added some personal notes from our experiences.

Downloading and Installing JDK
The fi rst thing that you need to do to develop Android applications is to visit http://www.oracle
.com/technetwork/java/javase/downloads/index.html and ensure that you have the Java JDK
installed. Because so many different acronyms
and versions appear on the Java download
website, Figure 6-1 points you in the direction
you need to get past all of the potential
distractions on that site.

The JDK is the Java Development Kit. You
need this package to do any Java development
on your machine, Android or otherwise. Be
sure to look for the Java Platform, Standard
Edition JDK.

Downloading and
Installing Eclipse

After you have successfully installed the JDK,
you will need a development environment.
The open source IDE Eclipse is recommended by Android directly in its documentation. You are
not limited only to Eclipse, but the tooling has been honed over time to be the easiest solution to

FIGURE 6!1: JDK download page

c06.indd 153c06.indd 153 28/07/12 6:01 PM28/07/12 6:01 PM

154 ” CHAPTER 6 GETTING STARTED WITH ANDROID

get up and running. Figure 6-2 shows the Eclipse download page (www.eclipse.org/downloads).
Download the version of Eclipse Classic that is appropriate for your operating system.

FIGURE 6!2: Eclipse download site

Downloading and Installing the Android SDK
After you have installed the Eclipse IDE, you need to install the Android Software Developer Kit
(http://developer.android.com/sdk/index.html). This includes all the tools necessary to build
Android apps, because the SDK is not built directly into Eclipse. Figure 6-3 shows the Android SDK
download page; make sure to get the right version for your OS.

Don’t use the installer for 64-bit Windows. Just get the zip fi le and unzip it to
c:Android. At the time of writing the installer package has diffi culty fi nding
the Java SDK installed on the machine.

For Mac deploy it to /android in your root volume.

FIGURE 6!3: Android SDK download page

c06.indd 154c06.indd 154 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 155

Downloading and Confi guring the Eclipse ADT Plug-in
After you have installed the Android SDK you need the ADT plug-in. What this does is add the
features necessary to create Android Projects (and Android Test Projects), because they are not
bundled with the base Eclipse install. Additionally, the plug-in adds debugging tools to Eclipse to
help during the Android development process. Figure 6-4 shows the interface for installing the ADT
plug-in. You will also use this interface when upgrading ADT. The tooling generally gets a revision
when a new version of the Android OS is released.

FIGURE 6!4: Installing the ADT plug-in

Use the Update Manager feature of your Eclipse installation to install the latest revision of ADT on
your development computer. Follow these steps:

1. Start Eclipse and select Help # Install New Software.
2. Click Add in the top-right corner.
3. In the Add Repository dialog box that appears, enter ADT plug-in for the name and the

following URL for the location: https://dl-ssl.google.com/android/eclipse/.

4. Click OK. If you have trouble acquiring the plug-in, try using “http” in the Location URL
instead of “https” (“https” is preferred for security reasons).

5. In the Available Software dialog box, select the checkbox next to Developer Tools and
click Next.

c06.indd 155c06.indd 155 28/07/12 6:01 PM28/07/12 6:01 PM

156 ” CHAPTER 6 GETTING STARTED WITH ANDROID

6. The next window shows a list of the tools to be downloaded. Click Next.
7. Read and accept the license agreements and then click Finish. If you get a security warning

saying that the authenticity or validity of the software can’t be established, click OK.

8. When the installation completes, restart Eclipse.
Once you have downloaded the ADT plug-in you need to set it up to talk to the Android SDK that
you downloaded earlier. This allows Eclipse to build, run, and debug Android applications without
needing to open a terminal or command shell. Figure 6-5 shows where you need to add the link to
the Android SDK in the Eclipse preferences.

After you’ve successfully downloaded the ADT, the next step is to modify your ADT preferences in
Eclipse to point to the Android SDK directory (see Figure 6-5):

1. Select Window # Preferences to open the Preferences panel. In Mac OS X, click Eclipse #
Preferences.

2. Select Android from the left panel.
3. You may see a dialog box asking whether you want to send usage statistics to Google. If

so, make your choice and click Proceed. You cannot continue with this procedure until you
click Proceed.

4. For the SDK Location in the main panel, click Browse and locate your downloaded SDK
directory.

5. Click Apply and then click OK.

FIGURE 6!5: ADT confi guration screen

c06.indd 156c06.indd 156 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 157

Installing Additional SDK Components
The last step in preparing your development environment for Android is to download additional
Android OS packages. This enables you to build applications that target that OS, and also gives you
the tools you need to emulate a device running that OS on which to test all of your applications,
whether or not they have been targeted to that OS version. Figure 6-6 shows just how many options
you have when looking to target Android OS versions.

FIGURE 6!6: Working with the SDK Manager

Correctly confi guring and using this tool will ensure that you have all the latest SDKs and utilities
afforded to you. Note that you will not necessarily need all of the versions of the SDKs listed in
Figure 6-6; this was merely to illustrate the full breadth of your options.

Loading the Android SDK Manager in Eclipse takes only a few steps:

1. Open Eclipse.
2. Select Window # Android SDK and AVD Manager.

c06.indd 157c06.indd 157 28/07/12 6:01 PM28/07/12 6:01 PM

158 ” CHAPTER 6 GETTING STARTED WITH ANDROID

3. Select Available Packages in the left panel. This reveals all of the components that are cur-
rently available for download from the SDK repository.

4. Select the component(s) you’d like to install and click Install Selected.
5. Verify and accept the components you want (ensure each one is selected with a green check-

mark) and click Install. The components will now be installed into your existing Android
SDK directories.

I recommend that you download and install an Android 2.2.x, Android 2.3.x,
and Android 3.x version. This will give you the latest two handset-specifi c
versions of Android, and the current tablet version of Android. As Android
4.0 is so new, you may choose to get it, but understand that you may need to
purchase a newer device to test apps targeted to that version.

Development
The following sections discuss the application layout and Android app development.

Creating a New Project
First things fi rst — you need to create a new Android project. The line highlighted in Figure 6-7 is
the type of project you want.

FIGURE 6!7 Creating a new Android project

c06.indd 158c06.indd 158 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 159

First you need to name your application and
add it to a workspace. Think of a workspace
as the folder in which your application resides.
Figure 6-8 illustrates what the New Android
Project screen looks like.

After you have named your application
you will need to give it a package name,
set the minimum SDK required to run your
application, and name the initial Activity that
will run when your application runs. If you
want to add a test project to your workspace,
you can do so at this time. Figure 6-9 shows
a completed Application Info step in the new
project wizard.

An important note at this point: Make sure
that your package name is unique. The
standard format for package names is
com.companyName.applicationName. This
must be unique because that is how it is known
on the Android device and in the Android
Market. When you make updates you can make
them only within your package name. If you
change your package name there will be no
upgrade path between versions.

The minimum SDK required is generally set
when you are leveraging a permission or piece
of functionality that did not exist when the core
Android version was released, or if you want
to target a specifi c type of device (tablet versus
handset). The major jumps are between 1.6
and 2.1, 2.3 and 3.x, and 3.x and 4.x.
Figure 6-10 shows you all of the SDKs that you
have installed that you can target when creating
your application. Please note that the reason
this screen is full of SDKs is because I took the
time to download them all for demonstration
purposes.

FIGURE 6!8: Naming your project

FIGURE 6!9: Confi guring application information

c06.indd 159c06.indd 159 28/07/12 6:01 PM28/07/12 6:01 PM

160 ” CHAPTER 6 GETTING STARTED WITH ANDROID

This step is also very important when building your application. The minimum SDK version you
set specifi es the lowest possible version of the SDK in which your application will run, and it is the
primary version in which your application will run. Android 1.5 is the lowest version of the SDK
still supported, and Android 4.0.3 (at the time of this writing) is the highest.

FIGURE 6!10: Choosing the SDK version for your app

Figure 6-10 shows a Google version of the SDK alongside all of the versions I
have installed on my machine. The Google APIs add additional functionality to
each API Level. Please use your best judgment when deciding whether to use the
Google APIs, and research if you need the functionality they provide.

Project Structure
The major sections to note in Figure 6-11 are the src and res folders and the AndroidManifest
.xml fi le. It shows the project layout for the application that I have been building in the
previous steps.

c06.indd 160c06.indd 160 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 161

All of your code lives within your src folder, under your Package
Namespace. The res folder holds layouts and resources for different
hardware specs. HDPI, LDPI, and MDPI are the resolutions for which
you can create images. The layout subfolder holds all of your XML
layouts. These are how your application gets rendered. The code will
be how to populate these layouts. All of your XML layouts are stored
in the layout subfolder of res, and your code will be linked under the
namespace in your src folder of the project view.

The Android Manifest is the heart of your application. It holds the entire
confi guration of your app (Figure 6-12) — the permissions you request
(Figure 6-14), the application attributes (Figure 6-13), and links to
instrumentation to be attached to your app. You can edit this in Eclipse’s
Manifest Editor or in XML (Figure 6-15) because that is how it is saved.

FIGURE 6!11: Basic project
structure

FIGURE 6!12: Main Manifest Editor

c06.indd 161c06.indd 161 28/07/12 6:01 PM28/07/12 6:01 PM

162 ” CHAPTER 6 GETTING STARTED WITH ANDROID

The Manifest Editor is where the initial information of your application is stored when you create
it. This interface also has links to export your application. Exporting is necessary when submitting
your app to Google Play. In Eclipse there is a specifi c menu option and wizard that expedites the
submission process.

Figure 6-13 shows all of the base properties that can be set for a given application in the app’s
AndroidManifest.xml fi le. The most common properties to edit are the Label (the text shown
under the icon, often referenced in a resource fi le) and Icon (the icon shown in the launcher UI of
your device, the icon your users will click on to launch the app).

FIGURE 6!13: Manifest Application Info Editor

The spartan view shown in Figure 6-14 is the Permissions Editor. Here you can add permission
requests to your application. The most common one is android.permission.INTERNET,
which allows the application to use the device Internet connectivity. This, along with GPS and
accelerometer, are the permissions you will add to the Derby Names application.

c06.indd 162c06.indd 162 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 163

Last is the XML Editor shown in Figure 6-15. As you make changes in the other tabs they are
refl ected here. If you feel more comfortable editing the XML by hand you can use this interface to
add, update, and remove properties as you see fi t.

FIGURE 6!14: Android Manifest Permissions Editor

FIGURE 6!15: Android Manifest XML Editor

c06.indd 163c06.indd 163 28/07/12 6:01 PM28/07/12 6:01 PM

164 ” CHAPTER 6 GETTING STARTED WITH ANDROID

Android Basics
You have two options for starting your application. You can build the layout you would like
to populate, or you can build the code that will populate the layout. Either is acceptable; it just
depends on what you feel most comfortable with.

Creating User Interfaces
This section describes the common widgets that come with Android, and shows examples of the
different layout elements you can use to coordinate the fl ow of your application’s activities.

Basic Android UI Elements
All of the basic elements in Android are stored in the android.widgets namespace in the SDK.

The most commonly used elements include:

! Button: This is a standard button element.

The following XML specifi es the layout of the Button widget:

Code: This code is necessary to handle the Click event noted in the XML layout.

public void btnClick (View view) {
//Do Something.
}

! TextView: When I see this I want to think text box, but it isn’t a text box. TextView is
effectively the same as a label in other languages. It is just a place to display text.

Layout:

Code:

TextView tvToShow = (TextView)this.findViewById(R.id.textview);
tvToShow.setText(“Ta-Dah!”);

! EditText: This is the text box widget. You can edit the contents of the text box and save
those values in code.

c06.indd 164c06.indd 164 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 165

Layout:

Code:

EditText txtUserName = (EditText) findViewById(R.id.txtUsername);
String username = txtUserName.getText().ToString();

! CheckBox: This is a standard checkbox element.

Layout:

Code:

final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
//It’s Checked
}
else {
//Not Checked
}
}
});

! RadioButton: This is a standard radio button element. To really get the most bang for your
buck, though, you need a RadioGroup.

Layout:




c06.indd 165c06.indd 165 28/07/12 6:01 PM28/07/12 6:01 PM

166 ” CHAPTER 6 GETTING STARTED WITH ANDROID

Code:

private OnClickListener radioButtonOnClick = new OnClickListener() {
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
// Do with it what you will
//rb.getText();
}
};

//This assigns this event to the radio buttons.
RadioButton radio_uno = (RadioButton) findViewById(R.id.radio_uno);
RadioButton radio_dos = (RadioButton) findViewById(R.id.radio_dos);
radio_uno.setOnClickListener(radio_listener);
radio_dos.setOnClickListener(radio_listener);

Figure 6-16 shows all of the major UI widgets.

! ListView: This is the element you use if you want to
show lists of data. You can overload its display and
put lots of elements in each row, or you can just bind
a text item and a value to each. The trick is using an
ArrayAdapter where T is the type of object that you
want bound. Additionally, creating a layout XML for
how you want each item displayed is a good strategy.

Layout:

Code:

static final String[] words = new String[]{ “Hello”, “World” };
lstWords = (ListView)findViewById(R.id.lstWords);
lstWords.setAdapter
new ArrayAdapter(this.getApplicationContext(),R.id.list_content,words));

Basic Android Layouts and Views
! FrameLayout: This is very simplistic and can really contain only a single UI element. You

can, in fact, have multiple elements but they overlap each other by default.

The example code shown here is rendered in Figure 6-17:

FIGURE 6!16: Major UI elements all
together

c06.indd 166c06.indd 166 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 167




This is a simple FrameLayout that contains a single EditText widget with the text “EditText.”

LinearLayout: This lays out your UI elements along a given direction: horizontal or vertical.
Figure 6-18 is a linear layout, which contains four consecutive TextView widgets along the vertical.

FIGURE 6!17: FrameLayout
rendered

FIGURE 6!18: LinearLayout
rendered

Example:


c06.indd 167c06.indd 167 28/07/12 6:01 PM28/07/12 6:01 PM

168 ” CHAPTER 6 GETTING STARTED WITH ANDROID





! TableLayout: Think tables in HTML and this is the type of organization you get with this
layout. TableLayouts contain rows and columns, representing a grid, and you can put other
UI elements into it. The following code results in a table with two rows, each with two cells,
and is visualized in Figure 6-19:






c06.indd 168c06.indd 168 28/07/12 6:01 PM28/07/12 6:01 PM

Getting the Tools You Need ” 169





! RelativeLayout: This is the most complex layout of the
four mentioned in this section. You specify relationships
between UI elements to lay out your interface.

The following code represents a simple form with a
TextView acting as a label for a blank EditText widget,
with Cancel and OK widgets docked …

superadmin

Recent Posts

What is the easy difination of science | Quick Solution

Science is the pursuit and application of knowledge and understanding of the natural and social…

3 years ago

definition, values, meaning of such values and type of goods with such elasticity value …….. | Quick Solution

Clearly stating the definition, the values, the meaning of such values and the type of…

3 years ago

Acct 422 – Nora D | Quick Solution

All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…

3 years ago

Acct 322 – Nora D | Quick Solution

All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…

3 years ago

Macro Economics Question | Quick Solution

https://www.npr.org/sections/ed/2018/04/25/605092520/high-paying-trade-jobs-sit-empty-while-high-school-grads-line-up-for-university Click on the link above. Read the entire link and answer the questions below…

3 years ago

MGT 322 – Nora D | Quick Solution

All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…

3 years ago