Transcript
Page 1: Android basics in android programming-chapter2

Chapter 2

Basics in Android Programming

By

Dr. Ramkumar Lakshminarayanan

Objectives

2.1 Preparing the Environment for the Android Programming

2.2 Construction of Android Sample Application

Introduction

The Android operating system software stack consists of Java applications running on a Java based object oriented application framework on top of Java core libraries running on a Dalvik virtual machine featuring JIT compilation. Libraries written in C include the surface manager, OpenCore media framework, SQLite relational database management system, OpenGL ES 2.0 3D graphics API, WebKit layout engine, SGL graphics engine, SSL, and Bionic libc. To develop an Application Google provides Android SDK and Android NDK. The Android SDK provides the API libraries and developer tools necessary to build, test, and debug apps for Android. In this chapter we will discuss about setting up an environment for creating Android Application and creating an sample application.

Android Developer Tools

Google Provides free tools for developing Android Applications. Android Developer Tools (ADT) can be downloaded from http://developer.android.com.

ADT Bundles includes the following:

Eclipse + ADT plugin

Android SDK Tools

Android Platform-tools

The latest Android platform

The latest Android system image for the emulator

Operating System that can be used for the development is:

Page 2: Android basics in android programming-chapter2

Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit)

Mac OS X 10.5.8 or later (x86 only)

Linux (tested on Ubuntu Linux, Lucid Lynx)

o GNU C Library (glibc) 2.7 or later is required.

o On Ubuntu Linux, version 8.04 or later is required.

o 64-bit distributions must be capable of running 32-bit applications.

IDE support for Android Development is as follows:

Eclipse 3.6.2 (Helios) or greater.

Eclipse 3.5 (Galileo) is no longer supported with the latest version of ADT.

Eclipse JDT plug in (included in most Eclipse IDE packages)

JDK 6. JRE alone is not sufficient

Android Development Tools plugin

Setting the Environment using ADT Bundle

The ADT Bundle includes everything for developing an Android Application. Once it is downloaded from the developer.android.com unzip the pack to a folder, for example D:\Android\

Install JDK6 which is the pre-requisite for Android tools. After Extraction and installation open the folder D:\Android\Eclipse and launch eclipse. Now the IDE is ready for developing the Android Application.

Getting Started with the first Project

1. Click New in the toolbar.

2. In the window that appears, open the Android folder, select Android Application Project, and click Next

3. Fill in the form that appears:

Application Name is the app name that appears to users. For this project, use "LearnApp"

Project Name is the name of your project directory and the name visible in Eclipse.

Page 3: Android basics in android programming-chapter2

Package Name is the package namespace for your app (following the same rules as packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. For this reason, it's generally best if you use a name that begins with the reverse domain name of your organization or publisher entity. For this project, you can use something like "com.mjs.learnapp." However, you cannot publish your app on Google Play using the "com.mjs" namespace

Figure 2.1 New Android App Wizard Project

Minimum Required SDK is the lowest version of Android that the app supports. While the latest versions of Android often provide great APIs for your app, you should continue to support older versions of Android until more devices get updated. API Level is an integer value that uniquely identifies the framework API revision offered by a version of the Android platform.

Target SDK indicates the highest version of Android with which you have tested with your application.

Compile With is the platform version against which you will compile your app. By default, this is set to the latest version of Android available in your SDK.

Theme specifies the Android UI style to apply for your app. You can leave this alone.Click Next

Page 4: Android basics in android programming-chapter2

4. On the next screen to configure the project, leave the default selections and Click Next

Figure 2.2 Configure Project

5. The next screen can help you create a launcher icon for your app. You can customize an icon in several ways and the tool generates an icon for all screen densities. Before you publish your app, you should be sure your icon meets the specifications.

Page 5: Android basics in android programming-chapter2

Figure 2.4 Launcher Icon

6. Now you can select an activity template from which to begin building your app.

For this project, select BlankActivity and click Next.

Page 6: Android basics in android programming-chapter2

Figure 2.5 Activity Template

7. Leave all the details for the activity in their default state and click Finish. Your Android project is now set up with some default files and you’re ready to begin building the app.

Figure 2.6 Project Setup

Exploring the Android Project Files

You will now see a new Android project called Droid1 in the Eclipse File

Explorer. In addition to linking the appropriate Android SDK jar file, the following core

files and directories are created:

• AndroidManifest.xml—The central configuration file for the application.

• project.properties—A generated build file used by Eclipse and the Android

ADT plug- in. Do not edit this file.

• /src folder—Required folder for all source code.

• /src/com.mjs.learnapp/MainActivity.java—Main entry point to this

application, named MainActivity. This activity has been defined as the default

launch activity in the Android manifest file.

• /gen/com.mjs.learnapp/R.java—A generated resource management source file.

Do not edit this file.

Page 7: Android basics in android programming-chapter2

Figure 2.7 Package Explorer

• /assets folder—Required folder where uncompiled file resources can be

included in the project.

• /res folder—Required folder where all application resources are managed.

Application resources include animations, drawable graphics, layout files,

data-like strings and numbers, and raw files.

• /res/drawable—Application icon graphic resources are included in several

different sizes.

• /res/layout/main.xml—Layout file used by MainActivity to draw onscreen.

• /res/values/strings.xml—The path where string resources are defined.

Editing Project Resources

By default, the Android manifest file resource editor is opened when you create a

new Android project. If you have clicked away from this screen to check out the various

Project files; simply double-click the AndroidManifest.xml file within your new

project to return to the Android manifest file resource editor. (See Figure 2.8)

Page 8: Android basics in android programming-chapter2

Figure 2.8 Android Manifest File

Android Manifest File

The Android manifest file is the central configuration file for an Android application.

The editor organizes the manifest information into a number of tabs:

• Manifest—This tab, shown in Figure 2.8, is used for general application-

wide settings such as the package name and application version information

(used for installation and upgrade purposes).

• Application—This tab is used to define application details such as the name

and icon the application displays and what activities can be run (including the

default launch MainActivity) and other functionality and services that the

application provides.

Permissions—This tab is used to define the applications’ permissions. For

example, if application requires the ability to read the contacts from the phone,

then it must register a User-Permission tag within the manifest, with the name

Page 9: Android basics in android programming-chapter2

android.permission.READ_CONTACTS.

• Instrumentation—This tab is used for unit testing, using the various

instrumentation classes available within the Android SDK.

• AndroidManifest.xml—This tab provides a simple XML editor to edit the

manifest file directly. If you switch to the AndroidManifest.xml tab, your

manifest file will look something like this:

Editing the Android Manifest File

Now let’s edit the Android manifest file. One setting you’re going to want to know

about is the debuggable attribute. You will not be able to debug your application until you

set this value, so follow these steps:

1. Open the AndroidManifest.xml file in the resource editor.

2. Navigate to the Application tab. Click the Application Attributes Tab.

3. Pull down the drop-down for the debuggable attribute and choose true.

4. Save the manifest file.

<?xml version="1.0" encoding="utf-8"?><manifest

xmlns:android="http://schemas.android.com/apk/res/android" package="com.mjs.learnapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mjs.learnapp.MainActivity" 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></manifest>

Page 10: Android basics in android programming-chapter2

If you switch to the AndroidManifest.xml tab and look through the XML, you will

notice that the application tag now has the debuggable attribute:

android:debuggable=”true”.

Editing Other Resource Files

Most Android application resources are stored under the /res subdirectory of the

project. The following subdirectories are also available:

• /drawable-ldpi, /drawable-hdpi, /drawable-mdpi, /drawable-xhdpi —These

subdirectories store graphics and drawable resource files for different screen

densities and resolutions. If you browse through these directories using the

Eclipse Project Explorer, you will find the icon.png graphics file in each one;

this is your application’s icon.

• /layout—this subdirectory stores user interface layout files. Within this

subdirectory you will find the main.xml screen layout file that defines the user

interface for the default activity.

• /values—this subdirectory organizes the various types of resources, such as

text strings, color values, and other primitive types. Here you find the

strings.xml resource file, which contains all the resource strings used by the

application.

Edit the String Resource

If you inspect the main.xml layout file of the project, you will notice that it

displays a simple layout with a single TextView control. This user interface control

simply displays a string. In this case, the string displayed is defined in the string resource

called @string/hello. To edit the string resource called @string/hello, using the string

resource editor, follow these steps:

1. Open the strings.xml file in the resource editor.

2. Select the String called app_name and note the Name (App_Name) and

value (LearnApp) shown in the resource editor. (See Figure 2.9)

3. Within the Value field , change the text to Learn Android App

4. Save the file.

Page 11: Android basics in android programming-chapter2

Figure 2.9 Resources Editor – strings.xml

If you switch to the strings.xml tab and look through the raw XML, you will

notice that string elements are defined within a <resources> block:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">LearnApp</string>

<string name="hello_world">Learn Android App</string>

<string name="menu_settings">Settings</string>

</resources>

Page 12: Android basics in android programming-chapter2

Running the Application

To build and debug an Android application, you must first configure your project

for debugging. The ADT plug-in enables you to do this entirely within the Eclipse

development environment. Specifically, you need to do the following:

• Configure an Android Virtual Device (AVD) for the emulator

• Create a debug configuration for your project

• Build the Android project and launch the debug configuration

When you have completed each of these tasks, Eclipse will attach its debugger to

the Android emulator (or handset), and you are free to debug the application as needed.

To run an application in the Android emulator, you must configure an Android

Virtual Device (AVD). The AVD profile describes the type of device you want the

emulator to simulate, including which Android platform to support. You can specify

different screen sizes and orientations, and you can specify whether the emulator has an

SD card and, if so, its capacity. Here are the steps for creating a basic AVD:

1. Launch the Android Virtual Device Manager:

a. In Eclipse, click Android Virtual Device Manager from the toolbar.

b. From the command line, change directories to <sdk>/tools/ and execute:

android avd

2. In the Android Virtual Device Manager panel, click New.

3. Fill in the details for the AVD. Give it a name, a platform target, an SD card size,

and a skin (HVGA is default).

4. Click Create AVD.

5. Select the new AVD from the Android Virtual Device Manager (Figure 2.10) and

click Start.

6. After the emulator boots up, unlock the emulator screen.

Page 13: Android basics in android programming-chapter2

Figure 2.10 – Android Virtual Device Manager

To run the app from Eclipse:

1. Open one of your project's files and click Run from the toolbar.

2. In the Run as window that appears, select Android Application and click OK.

Eclipse installs the app on your AVD and starts it (Figure 2.11).

Page 14: Android basics in android programming-chapter2

Figure 2.11 Android Emulator with LearnApp Running

Chapter Summary

Android was designed to empower the developer to write innovative applications.

Google provides free tool for developing android applications. Setting up the environment

is done by ADT Bundles which is available from the Android Developer Website. The

Page 15: Android basics in android programming-chapter2

Android manifest file is the central configuration file for an Android application. When

you have completed setting up the project task, Eclipse will attach its debugger to the

Android emulator (or handset), and you are free to test and debug the application as

needed.