You are on page 1of 9

Mobile Programming

In this module, we will discover the fundamental design patterns of all android
applications. We will also learn about the different components of the android project which are
the “src” or source code, the auto generated codes and the included libraries. It is also in this
module that we will be demonstrating the “Hello World” program in android via video tutorial.

Objectives:

At the end of the session, the students should be able to:

1. Create a new Android Application;

2. Understand the Android Project Components;

3. Test and run a simple output program (“Hello World”); and

4. Familiarize himself with the terms relevant to the topic;

GETTING 1
STARTED
A. Creating a new Android Application
An Android Application is a software application
that runs on the android mobile operating system. Since the
android platform is built for mobile devices, a typical
android application is designed for a smartphone or tablet
PC. The ADT plugin will be able to provide a new project
wizard where you can create a new project or open one
from an existing code.
Creating a New Android Project
The ADT plugin provides a New Project Wizard
that you use to quickly create a new Android project (or a
project from existing code). These are the steps needed to
create a new project:
1. Select File > New > Project.
2. Select Android > Android Application Project, and
click Next.
3. Enter the basic settings for the project:
a. Enter an Application Name. This name is used
as the title of your application launcher icon
when it is installed on a device.
b. Enter a Project Name. This text is used as the
name of the folder where your project is created.
c. Enter a Package Name. This class package
namespace creates the initial package structure
for your applications code files and is added as
the package attribute in your application's
Android manifest file. This manifest value
serves as the unique identifier for your
application app when you distribute it to users.
The package name must follow the same rules
as packages in the Java programming language.
d. Select a Minimum Required SDK. This setting
indicates the lowest version of the Android
platform that your application supports. This
value sets the minSdkVersion attribute in the
<uses-sdk> element of your manifest file.
e. Select a Target SDK. This setting indicates the
highest version of Android with which you have
tested with your application and sets the
targetSdkVersion attribute in your application's'
manifest file.

(Note: You can change the target SDK for your


project at any time: Right-click the project in the
Package Explorer, select Properties, select
Android and then check the desired Project Build
Target.)

f. Select a Compile With API version. This


setting specifies what version of the SDK to
compile your project against. We strongly
recommend using the most recent version of the
API.
g. Select a Theme. This setting specifies which
standard Android visual style is applied to your
application and Click Next.
4. In the Configure Project page, select the desired
settings and click Next. Leave the Create activity
option checked so you can start your application with
some essential components.
5. In the Configure Launcher Icon page, create an icon
and click Next.
6. In the Create Activity page, select activity template
and click Next.
7. Click Finish and the wizard creates a new
project according to the options you have chosen.

Creating a New Library Project

A library project is a standard Android project, so


you can create a new one in the same way as you would a
new application project. These are the steps to create a new
library project:

1. Select File > New > Project.


2. Select Android > Android Application Project, and
click Next.
3. Enter the basic settings for the project, including
Application Name, Project Name, Package Name,
and SDK settings.
4. In the Configure Project page, select the Mark this
project as a library option to flag the project as a
library.
5. Set the other options as desired and click Next.
6. Follow the instructions to complete the wizard
and create a new library project.

Referencing a Library Project

If you are developing an application and want to


include the shared code or resources from a library project,
you can do so easily by adding a reference to the library
project in the application project's Properties. These are the
steps to add a reference to a library project:
1. Make sure that both the project library and the
application project that depends on it are in your
workspace. If one of the projects is missing, import
it into your workspace.
2. In the Package Explorer, right-click the dependent
project and select Properties.
3. In the Properties window, select the "Android"
properties group at left and locate the Library
properties at right.
4. Click Add to open the Project Selection dialog.
5. From the list of available library projects, select a
project and click OK.
6. When the dialog closes, click Apply in the Properties
window.
7. Click OK to close the Properties window.

(Note: If you are adding references to multiple libraries,


note that you can set their relative priority and merge order
by selecting a library and using the Up and Down controls.
The tools merge the referenced libraries with your
application starting from lowest priority (bottom of the list)
to highest (top of the list). If more than one library defines
the same resource ID, the tools select the resource from the
library with higher priority. The application itself has
highest priority and its resources are always used in
preference to identical resource IDs defined in libraries.)

B. The Android Project Components

There are three important items to note when


talking about the Android Project Components these are:
The Source Code, the Auto Generated Code and the
Included Libraries.
The Source code or the src code is the directory
which will contain all of your applications source files.
This is also the location of your java source code. On the
other hand, the auto generated code will reside in the
resources or res code. They can be the images, videos, and
strings that will be displayed to the user. In addition, by
abstracting resources from your application code, it’s trivial
to add support for new screen resolutions, orientations, and
languages. Lastly, the included libraries will be placed on
your AndroidManifest.xml file. This file declares
everything about your app that the Android operating
system needs to launch it. This includes the classes
composing the application, the permissions required by
your app, meta information like the minimum Android API
for your app, and the libraries that your app depends on.
This must be in the root directory of your project.
Typically, the SDK will update this file for you
automatically, so you shouldn’t have to edit it directly.
The manifest and source directory are relatively
straightforward, however the resource folder calls for a bit
more explanation. If you expand this folder in the Package
Explorer, you’ll find three types of subdirectories:
drawable/, layout/, and values/. The drawable/ folders
contain all of the graphics for your application, which can
be either image files or special XML files defining shapes.
The layout/ directory stores the UI of each screen displayed
by your application, and the values/ folder contains lists of
strings that are utilized by the UI. Now, the interesting part
is what comes after the hyphen. This portion of the folder
name is a qualifier that tells the system when to use the
contained resources. For example, images in drawable-
hdpi/ will be displayed when the host device has a high-
density screen (~240dpi), whereas devices with low-density
screens (~120dpi) will use the images in drawable-ldpi/.
By simply placing high-resolution images and low-
resolution images in the appropriate folders, your app will
be available to both types of devices with no changes to
your code. Similarly, your app can be ported to other
languages by appending en, es, fr, ar, or any other language
code after the values/ subdirectory. In this way, the res/
directory makes it very easy to support new devices and
reach new audiences.
Creating User Interface
Android apps use XML to define their user
interfaces. Each XML element in a layout file represents
either a ViewGroup or a View object. ViewGroups are
invisible containers for other View objects, and their main
job is to arrange child views (or nested view groups) into a
pleasing layout. View objects are visible UI components
like text fields, buttons, and tables. To configure the
properties of a view or view group, you edit the attributes
of the corresponding XML element.
The template project we used comes with a default
layout called activity_main.xml, which you should find in
the res/layout/ directory. When you double-click the file,
ADT displays the graphical UI editor by default. While you
can use this to visually edit the underlying XML, for this
book, we’ll be directly editing the XML markup to gain an
in-depth understanding of how Android user interfaces
work. To display the raw XML, click the
activity_main.xml tab in the bottom-left of Eclipse’s
editing area.

C. The “Hello World” Program

The following are the steps needed to create a simple


output program in Android.
1. Launch Eclipse ADT
2. Click File
a. Click New
b. Click Android Application Project
(Note: Application Name is user defined)
3. The Minimum Required SDK, Target SDK and
Compile With drop down lists should be within range
of each other (i.e. if you choose the
IceCreamSandwich version for Minimum Required
SDK, it should also be used for Target SDK and
Compile With).
4. Activity Name and Layout Name is user defined.
5. After your Emulator has loaded, Drag Text View in
your XML file.
a. Double Click the Text View to view the code.
b. Change android:text to “My First Android
Application”.
6. Click Run
a. Click Run Configurations
b. Click new Android Application
c. Name is user defined
d. Click Browse and Look for the Project Name
e. Click Apply and Run
7. Wait for Eclipse ADT to launch your emulator.
(Note: Please check the Module 4 video tutorial for
the demonstration of the “Hello World” program).
Reference:

Rahman K. (2013). Android Development Tools for


Eclipse. (1st Ed.) Packt Publishing

Hodson R. (2014). Android Programming


Succinctly (1st ed.) www.syncfusion.com

You might also like