LAB Creating a First Android Project
In this lab, you will create a first Android project to display the same Hello World message that you displayed before. Before completing this lab, you must have Eclipse and the Android SDKs installed. This software is installed in the COBA labs.
Abstract
In this lab, you will perform the following tasks:
-
Review the XML language as it is used to implement nearly all layout and other configuration files
-
Understand Android SDK versioning
-
Create a first Android application with a single activity
-
Explore the files that make up an Android application
-
Compile the application
-
Configure and run an Android emulator
-
Run the Android application
Key Terms -
An activity represents a thing that the user can do. Conceptually, think of an activity as a Visual Basic form, and its code behind (VB) file. An activity has a Java code file and one or more layout files that defines the visual interface for the activity.
-
The eXtensible Markup Language (XML) is used to describe data. It is used extensively by Android configuration, resource, and layout files.
-
A layout depicts the visual interface for an activity. An activity might have many layouts to support different screen resolutions, sizes, or orientations.
-
One purpose of an intent is to start activities. An intent is a messaging object used start activities, service, or deliver broadcasts. Data is also passed from one activity to another via an intent.
-
A resource describes data that is used by an applications. Resources include strings and other data. Resources are stored as XML files.
-
If it’s visible on the screen, it’s a view. Each of an activity’s layouts is a view. Widgets are views. Dialogs are also views. A view is a rectangular area on the screen. It has a visible part and is responsible for drawing. A view also responds to events. Layouts, buttons, text boxes, check boxes and other widgets are all views.
-
A Widget has roughly the same as it does for a Visual Basic control. That is, it has a visual interface, and supports a code behind file (Java), and can handle events.
An XML Review
In this section, the eXtensible Markup Language (XML) and key XML concepts are summarized. However, this is not a complete discussion of XML and related standards. XML does not pertain to just to Android. XML is supported and used by many programming languages and development environments including .NET.
XML is an acronym for the eXtensible Markup Language. Its purpose is to represent, store, and provide a way to transfer data. The following list describes the rules of XML:
-
Start tags are enclosed in angle brackets <>.
-
End tags are enclosed in angle brackets the (/) preceding the tag name.
-
Tags must be properly nested.
-
Every document (or document fragment) must have exactly one and only one root element.
-
XML documents are represented as a tree structure and operate similar to the HTML DOM.
Following is a simple XML document:
Zaphod
Beeblebrox
-
In the above, the first line indicates that the document is an XML document. The syntax is the same for all XML documents.
-
The root (outermost) node is named
.
-
It contains a child node named .
-
The node contains two child nodes named and . These two nodes contain content. Content appears between the starting and ending node tags. The other elements do not have content.
-
Each opening tag has a corresponding closing tag.
Android elements can have 0, 1 or many attributes. Android attributes contain data about an attribute. XML attributes have the same syntax as HTML attributes.
-
Attributes appear inside the opening element tag.
-
Attributes appear as key / value pairs.
-
An equal sign separates each key / value pair.
-
Values appear in quotation marks.
-
Attribute key/value pairs can appear in any order.
-
A space separates each key value pair.
Android uses XML for several purposes as you will see throughout this course.
-
An XML document named android.manifest is used to identify the files that make up an Android Application project.
-
It is used to describe the user interface for each screen in the application. These files are known as layout resources.
-
It is used to describe the strings and other constants that make up the application. Strings are known as string resources.
Another feature of XML that you need to know is the notion of XML namespaces. XML namespaces are used to qualify an element name or an attribute name and to determine the XML vocabulary to which the element belongs. To understand the concept and syntax of an XML namespace look at the following Android code. This file will be explained in detail more later in this tutorial.
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.helloandroid.MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
The above code introduces the notion of an XML namespace:
-
The prefix xmlns, is itself a namespace prefix. The (:) follows the xmlns prefix, followed by another name called a namespace prefix. The namespace value is a URL. Note that this URL is just a name. It does not necessarily point to a Web Site, physical, or logical file. An XML namespace typically defines a vocabulary. (the element names and the order in which the elements must be assembled.)
-
In the above declaration, the android namespace prefixes are android and tools.
-
The attribute named android:layout_width=”match_parent” is saying that the layout_width attribute belongs to the android namespace.
-
The attribute named tools:context is saying that the context attribute belongs to the tools namespace.
-
In addition, note that the root node is . It has a child node named . Both elements have several attributes, each referencing a particular namespace.
Creating a First Android Application
The first lab which discussed the installation of Eclipse and Android pointed out Android version numbers, names, and the corresponding SDKs. When you create an Android project, you give the project a name and a package to which the project belongs. The following three SDK versions are defined for an application:
-
Minimum Required SDK: This is the oldest Android version on which the application is designed to execute. For my demonstrations, the Minimum Required SDK version will be 5.0.1 or 5.1.1.
-
Target SDK: This is the latest Android version on which the application is designed to run. This version is usually the latest Android version.
-
Compile With SDK: This is the Android Version used to compile the application. It must be greater than or equal to the Target SDK. In my examples, this will be the current Android SDK version.
-
Theme: For now, suffice to say that the Theme describes the look of an application and you will select the default themes
HANDS-ON ACTIVITY – Creating a new Android Application Project
-
Make sure that Eclipse is running and that the default workspace is open. Android uses the same notion of a Workspace as the Java programs that you created previously..
-
On the Eclipse menu bar, click File, New, Android Application Project. If this option does not appear, click File, New, Project. In the dialog box that appears, select Android Application Project.
-
Complete the information as shown in the following dialog box:
Make sure to set the Application Name to HellloAndroid. When you do, the Project name and Package Name are completed automatically. As you can see as you work with the dialog, you can create the application to support any number of different Android versions. For the most part, we will use API Version 22: This is the version running on most phones. Set the SDK versions as shown in the above figure.
You can ignore the warning error about using the package named “com.example”. When an application is deployed, it should be created in a unique package name. However, we will not worry about deploying packages yet.
-
Click Next to display the following dialog box:
There are a couple of important configuration options in this step. The check box titled Create custom launcher icon should be checked along with the check box titled Create activity. The first configures an icon for the application. The second causes a subsequent dialog box to be displayed that will allow you create the application’s default activity (first screen). Make sure that the options in the following figure are checked:
-
Click Next to display the following dialog box. You will not change the default icon. However, realize that Android devices are designed to use one of many resolutions (and corresponding icons) shown. Depending on the device itself, one of the resolutions will be chosen by the Android system. Creating device-independent applications will be discussed further in subsequent tutorials.
-
Click Next to display the following dialog box:
Recall on the initial screen you told Eclipse to create a launch activity that will be run when the program starts. Here, you tell Eclipse what kind of activity to create. Activities will be discussed in much more detail later. However the default Blank Activity will create a screen that displays the all too familiar Hello World message, and the necessary code to display that screen (activity).
-
Make sure that Blank Activity is selected.
-
Click Next to display the following dialog box:
-
An activity has two parts. The Activity Name is the name of the Java class containing the code for the activity. The default activity (class) name is MainActivity and is stored in the file MainActivity.java. The Layout Name contains the XML file that describes the layout (screen formatting). By default, the name of the layout file is activity_main.xml.
-
Use the default options and click Finish to create the project. When you do, Eclipse creates many different files as shown in the Package Explorer.
The purpose and general content of these files is discussed in the next section:
Understanding the Structure of an Android Application
An Android application has a well-defined structure. That is, an application’s files are put in different folders based on the purpose of the file. In addition, certain configuration files and resource files always have the same name. Roughly speaking, Android application files are categorized as follows:
-
Applications contain one or mode code files. There is a code file for each activity and fragment, and other code file types.
-
Images, string constants, menus, layouts, and others are implemented as resources. Most resources are represented as XML files.
By default, every application, no matter how simple, will contain the following folders:
-
The folder named src contains the application’s source code (Java Code files). The application’s source code will be discussed later in this document.
-
When a program is compiled, Android generates a few files that are used by your program code. These files appear in the folder named gen. (generated)
-
res is short for resources. In this folder, there are sub-folders that store images, and the XML code used to describe the application’s screens and other constant data.
-
The folders named bin and libs contain binary files and other libraries needed by the application.
Other folders are discussed later, and in more detail.
The AndroidManifest.xml File
The purpose and structure of the AndroidManifest.xml file is fully documented at: http://developer.android.com/guide/topics/manifest/manifest-intro.html
Every Android application has a file named AndroidManifest.xml (again, an XML document) appearing in the application’s root folder. It contains the information necessary to compile and run the application. The following code segment shows the AndroidManifest.xml file for this Hello World application:
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="19"
android:targetSdkVersion="23" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >
"android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" />
It uses the following android XML namespace as most Android configuration files do.
xmlns:android="http://schemas.android.com/apk/res/android"
Every Android application belongs to a package. The package attribute appears as follows.
package="com.example.helloandroid"
For most applications in this class, you will use the default package name. However, applications that you deploy should have package names for registered domains. The element has a child element named . The element tells the Android system versions for which the application is compatible.
-
The attribute android:minSdkVersion=”value” depicts the minimum API level on which the application will install and run.
-
android:targetSdkVersion=”value” contains the version for which the application is designed to run.
-
android:maxSdkVersion=”value” contains the maximum Android version for this the application is designed to run. If this value is not specified, the current Android version is implied.
The element has a child element named used to declare the application’s components. In the above code segment, the following attributes appear:
-
Android supports a backup and restore infrastructure. The attribute android:allowBackup defines whether the application will participate in this infrastructure.
-
android:icon depicts the application’s icon. The attribute’s value is set to @drawable/ic_launcher. The fragment @drawable tells the compiler where to look for the icon named ic_launcher. Referencing resources will be discussed later in this lab.
-
Think of android:label as containing the application’s title. The value "@string/app_name" tells the compiler to look for the resource named app_name.
-
Android applications have a theme. For now, think of a theme as CSS file for an application. It is used to set the application’s default appearance. The attribute named android:theme defines the application’s theme. For most of this class, you will use Android-provided themes.
The element has a child element named . An activity is roughly equivalent to a Visual Basic or C# form. They have both a user interface (XML), and a code behind (Java). There should be one element for each in the application. In this example there is one activity with the following two attributes:
-
The attribute named android:name=”.MainActivity” contains the name of a class that implements the Activity class or one of its subclasses. In this example, the class is named MainActivity. This is the name that you chose for the activity when you created the project. Note that the value begins with a leading dot (.), indicating that MainActivity is a class.
-
The android:label=”@string/app_name” attribute defines the activity’s title. If this attribute is not defined, the application’s label is used. Again, the attributes value points to the resource named app_name.
The element has a child element named . In .NET, you create forms and display them with a method such as Form.Show(). In Android, activities are started and rendered using intents. And an intent filter tells Android the name of the action to perform. An intent filter, in turn, as two child elements named and .
-
The element’s android:name attribute should contain the name of the activity to start.
-
Android describes what are referred to as “standard actions”. The action “android.intent.action.MAIN” is one of those standard actions. It causes the action to be the startup action and therefore the program’s entry point. Thus, in this example, MainActivity is configured as the startup action and will run automatically when the application starts.
-
Intents also have a . The standard value android.intent.category.LAUNCHER works with the to tell the system that this is the initial activity and should be listed in the system’s application launcher. Other Android categories will be discussed when they are used.
In summary, the manifest file tells the system everything it needs to compile and run the application including viable android versions, the application’s components, and the startup (default) activity.
HANDS-ON ACTIVITY – Viewing the Application’s Manifest
In this set of steps, you will examine the Application’s manifest, and the Eclipse tools used to manage it.
-
In the Package Explorer, make sure that the HelloAndroid project is expanded. Double-click on the AndroidManifest.xml file to open the editor shown in the following figure. Click the AndroidManifest.xml tab, if necessary.
The XML shown in the above figure is the same as the AndroidManifest.xml file just discussed. Its content can be edited directly.
-
Click the Manifest tab to display the editor shown in the following window:
The Manifest tab, shown in the above figure, contains a WYSIWYG editor that writes the XML for you.
-
Click the Application tab to display the following window:
The Application tab contains another WYSIWYG editor that writes the XML pertaining to application resources. As you can see, the resource names are the same as those in the XML files. Note that, Permissions and Instrumentation are not discussed in this section.
The res Folder
The folder named res contains the application’s resources. Most of these files are XML files although a few contain images. This folder contains several important sub-folders as shown in the following figure:
The res/drawable Folders
The folders beginning with the word drawable contains the application’s images. The folder suffixes depict the resolution of the images in the folder. When and Android application runs, the runtime will choose the best image resolution based on the device’s screen size and resolution: The following list describes the commonly used resolutions:
Name
|
Resolution
|
hdpi
|
240 dpi
|
ldpi
|
120 dpi
|
xhdpi
|
320 dpi
|
xxhdpi
|
490 dpi
|
Note that the drawable folder names in the res folder are significant. If the folders are not named correctly, the images will not be located by the compiler and runtime.
The following figure shows (roughly) how the sizes are mapped based on the size and resolution of the device:
The res/layout Folder
The folder named layout, as the name implies, contains the layout(s) for each activity in the package. Think of a layout as the screen design for a Visual Basic form. A layout describes the kind of layout and the controls (widgets) appearing on the layouts, their position, and other formatting properties. Layouts are considered resources, and are therefore XML files. In this example, there is one activity named activity_main.xml. Most applications have multiple activities and at least one layout per activity. As you will see in subsequent labs, it is possible for one activity to have several layouts that will be used based on the device size and resolution.
HANDS-ON ACTIVITY – Viewing the Main Layout
In these steps, you will look at the XML for the layout named Activity_Main.xml, and the accompanying Eclipse Layout Editor.
-
In the Package Explorer, expand the layout folder and double-click the file named activity_main.xml. If necessary, double-click the ativity_main.xml tab.
As you can see, the file is an XML file. The outermost element is named , which renders the widgets relative to each other or the layout window itself. Layouts will be discussed in much more detail later The element is a child of the . It displays a prompt much like the Visual Basic Label control. The various attributes describes the application’s resources and positioning innnformation.
-
Click the Graphical Layout tab to display the window shown in the following figure:
Note that the first time the graphical editor is loaded, it might take a minute or two. As you can see, the visual editor works like most visual editors. It contains a Palette containing the controls (widgets) supported by Android. It contains a visual surface where you can see what the Layout will look like.
The res/Menu folder
The menu folder contains the application’s menu(s) if one exists. Again, this file is an XML file and just another resource.
The
Directory: faculty -> ekedahl -> IS389faculty -> Curriculum vitae wei chen professorfaculty -> Digital image warpingfaculty -> Samples of Elements Exam Question III contains All Prior Exam Qs III exceptfaculty -> 【Education&Working Experience】faculty -> References Abe, M., A. Kitoh and T. Yasunari, 2003: An evolution of the Asian summer monsoon associated with mountain uplift —Simulation with the mri atmosphere-ocean coupled gcm. J. Meteor. Soc. Japan, 81faculty -> Ralph R. Ferraro Chief, Satellite Climate Studies Branch, noaa/nesdisfaculty -> Unit IV text: Types of Oil, Types of Prices Grammar: that/those of, with revisionIS389 -> Installing java, eclipse, and android sdks to setup an android development environmentIS389 -> Is 389 android fragment lab ekedahlIS389 -> Please download a current copy. I have modified it since the initial upload
Share with your friends: |