Android basics in android programming-chapter2

  • View
    79

  • Download
    1

Embed Size (px)

DESCRIPTION

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.

Text of Android basics in android programming-chapter2

  • 1. 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:

2. 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:AndroidEclipse 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. 3. 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. 4. Click Next 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. 5. 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. 6. 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 youre 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.xmlThe central configuration file for the application. project.propertiesA generated build file used by Eclipse and the Android ADT plug- in. Do not edit this file. /src folderRequired folder for all source code. /src/com.mjs.learnapp/MainActivity.javaMain 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.javaA generated resource management source file. Do not edit this file. 7. Figure 2.7 Package Explorer /assets folderRequired folder where uncompiled file resources can be included in the project. /res folderRequired folder where all application resources are managed. Application resources include animations, drawable graphics, layout files, data-like strings and numbers, and raw files. /res/drawableApplication icon graphic resources are included in several different sizes. /res/layout/main.xmlLayout file used by MainActivity to draw onscreen. /res/values/strings.xmlThe 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) 8. 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: ManifestThis 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). ApplicationThis 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. PermissionsThis 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 9. android.permission.READ_CONTACTS. InstrumentationThis tab is used for unit testing, using the various instrumentation classes available within the Android SDK. AndroidManifest.xmlThis 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 lets edit the Android manifest file. One setting youre 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. 10. 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 applications icon. /layoutthis 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. /valuesthis 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 e