Sie sind auf Seite 1von 49

INTRODUCTION

A. Module Descriptions This module explain about how to develop android application such as simple calculator, ebook application and etc. In this module we will learn about basic for android programming such as layout, image, text , dialog, widget, intent, multimedia and database. Before learn this module , participants must have

knowledge about Java Programming, xml and android basic structure.

B. Module Manual At the begining of lesson the participants learn about using ADT-Bundle Software to develop Android Application. It will follow with creating mobile application projects with Android programming.

C. Competence Standard After learning this module the participants are able to develop mobile application with Android programming.

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 1

LEARNING ACTIVITY I INTRODUCTION TO ANDROID

A. Learning Objectives In this section, we will be discussing the characteristics of mobile devices and how it affects program development for these devices. We will be introduced to the Android. At the end of the lesson, the participants should be able to: Creating a new blank project in Eclipse Understanding errors Creating an emulator Setting up and copying launch configurations Running your first app Studying the anatomy of a project Creating Layout Displaying Image

B. Description 1. Android Overview Computing is more accessible than ever before. Handheld devices have transformed into computing platforms.Be it a phone or a tablet, the mobile device is now so capable of general-purpose computing that its becoming the realpersonal computer (PC). Every traditional PC manufacturer isproducing devices of variousform factors based on the Android OS. The battles between operating systems, computing platforms, programming languages, and development frameworks are being shifted and reapplied to mobile devices.

We are also seeing a surge in mobile programming as more and more IT


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 2

applications start to offer mobile counterparts. In this book, well show you how to take advantage of your Java skills to write programs for devices that run on Googles Android platform (http://developer.android.com/index.html), an open source platform for mobile and tablet development.

Why Develop for Android? Market share As a developer, you have an opportunity to develop apps for a fairly new market that is booming on a daily basis. Android is currently set to outpace many other carriers in market share in the industry in coming months. With so many users, its never been easier to write an application that can be downloaded and used by real people! The Android Market puts your app right into your users hands easily! Users dont have to go searching the Internet to find an app to install. They just simply go to the Android Market that is preinstalled on their device, and they have access to all your apps. Because the Android Market comes preinstalled on most Android devices (I discuss a few exceptions later), users typically search the Android Market for all of their app needs. Its not hard to see an apps number of downloads soar in just a few days. Time to market With all the application programming interfaces (APIs) that Android comes packed with, its easy to develop full -featured applications in a relatively short time frame. After youve signed up with the Android Market, just upload your apps and publish them. Wait, you may say, are you sure? Why, yes, I am! Unlike other mobile marketplaces, the Android Market has no app-approval process. All you have to do is write apps and publish them.Technically, anyone can publish anything, but its good karma to keep within Googles terms of service and keep your apps family-friendly. Remember that Android users come from diverse areas of the world and are in all age categories.
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 3

Open platform The Android operating system is open platform,meaning that its not tied toone hardware manufacturer and/or one provider. As you can imagine, the openness of Android is allowing it to gain market share quickly. All hardware manufacturers and providers can make and sell Android devices. The Android source code is available at

http://source.android.comfor you to view and/or modify. Nothing is holding you back from digging into the source code to see how a certain task is handled. The open-source code allows phone manufacturers to create custom user interfaces (UIs) and add built-in features to some devices. This also puts all developers on an even playing field. Everyone can access the raw Android source code.

Android Programming Basics If youve never programmed before, this book may not be the best place to start. I advise that you pick up a copy of Beginning Programming with Java For Dummies,by Barry Burd (Wiley Publishing), to learn the ropes. After you have a basic understanding of Java under your belt, you should be ready to tackle this book. Although the majority of Android is Java, small parts of the framework arent. Android also encompasses the XML language as well as basic Apache Ant scripting for build processes. I advise you to have a basic understanding of XML before delving into this book.

If you need an introduction to XML, check out XML For Dummies,by Lucinda Dykes and Ed Tittel (Wiley). If you already know Java and XML, congratulations youre ahead of the curve!

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 4

Java: Your Android programming language Android applications are written in Java not the full-blown Java that J2EE developers are used to, but a subset of Java that is sometimes known as the Dalvik virtual machine.This smaller subset of Java excludes classes that dont make sense for mobile devices. If you have any experience in Java, you should be right at home.

It may be a good idea to keep a Java reference book on hand, but in any ase, you can always Google what you dont understand. Because Java is nothing new, you can find plenty of examples on the Web that demonstrate how to do just about anything.

In Java source code, not all libraries are included. Verify that the package is available to you. If its not, an alternative is probably bundled with Android that can work for your needs.

Activities Android applications are made up of one or more activities. Your app must contain at least one activity, but an Android application can contain several. Think of an activity as being a container for your UI, holding your UI as well as the code that runs it. Its kind of like a for m, for you Windows programmers out there.

Intents Intentsmake up the core message system that runs Android. An intent is composed of an action that it needs to perform (View, Edit, Dial, and so on) and data. The action is the general action to be performed when the intent is received, and the data is the data to operate on. The data might be a contact item, for example.

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 5

Intents are used to start activities and to communicate among various parts of the Android system. Your application can either broadcast an intent or receive an intent.

Cursorless controls Unlike PCs, which let you use a mouse to move the cursor across the screen, Android devices let you use your fingers to do just about anything a mouse can do. But how do you right-click? Instead of supporting right-clicking, Android has implemented the long press. Press and hold a button, icon, or screen for an extended period of time, and a context menu appears. As a developer, you can create and manipulate context menus. You can allow users to use two fingers on an Android device instead of just one mouse cursor, for example. Keep in mind that fingers come in all sizes, however, and design your user interface accordingly. Make the buttons large enough, with enough spacing, so that even users with large fingers can interact with your apps easily.

Views and widgets What the heck is a view? A viewis a basic UI element a rectangular area on the screen that is responsible for drawing and event handling. I like to think of views as being basic controls, such as a label control in HTML. Here are a few examples of views: ContextMenu Menu View Surface view Widgetsare more-advanced UI elements, such as check boxes. Think of them as being the controls that your users interact with. Here are a few widgets: Button CheckBox
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 6

DatePicker DigitalClock Gallery FrameLayout ImageView RelativeLayout PopupWindow Many more widgets are ready for you to use. Check out the android. widgetpackage in the Android documentation at http://developer.

android.com/reference/android/widget/package-summary.html for complete details. Background services If youre a Windows user, you may already know what a serviceis: an application that runs in the background and doesnt necessarily have a UI. A classic example is an antivirus application that usually runs in the background as a service. Even though you dont see it, you know that its running. 2. Software for Development Developer Tools The Android Developer Tools (ADT) plugin for Eclipse provides a professional-grade development environment for building Android apps. It's a full Java IDE with advanced features to help you build, test, debug, and package your Android apps. Full Java IDE

Android-specific refactoring, quick fixes, integrated navigation between Java and XML resources.

Enhanced XML editors for Android XML resources.

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 7

Static analysis tools to catch performance, usability, and correctness problems.

Build support for complex projects, command-line support for CI through Ant. Includes ProGuard and app-signing.

Template-based wizard to create standard Android projects and component

Graphical UI Builders

Build rich Android UI with drag and drop. Visualize your UI on tablets, phones, and other devices. Switch themes, locales, even platform versions instantly, without building.

Visual refactoring lets you extracts layout for inclusion, convert layouts, extract styles.

Editor support for working with custom UI components

On-device Developer Options


Enable debugging over USB. Quickly capture bug reports onto the device. Show CPU usage on screen. Draw debugging information on screen such as layout bounds, updates on GPU views and hardware layers, and other information.

Plus many more options to simulate app stresses or enable debugging options. To access these settings, open the Developer options in the system Settings. On Android 4.2 and higher, the Developer options screen is hidden by default. To make it available, go toSettings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 8

Fig. 1. 1 Developer Options

Develop on Hardware Devices


Use any commercial Android hardware device or multiple devices. Deploy your app to connected devices directy from the IDE. Live, on-device debugging, testing, and profiling.

Develop on Virtual Devices

Emulate any device. Use custom screen sizes, keyboards, and other hardware components.

Advanced hardware emulation, including camera, sensors, multitouch, telephony.

Develop and test for broad device compatibility.

Powerful Debugging

Full Java debugger with on-device debugging and Android-specific tools. Built-in memory analysis, performance/CPU profiling, OpenGL ES tracing.
Page 9

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Graphical tools for debugging and optimizing UI, runtime inspecton of UI structure and performance.

Runtime graphical analysis of your app's network bandwidth usage.

Testing

Fully instrumentated, scriptable test environment. Integrated reports using standard test UI. Create and run unit tests on hardware devices or emulator.

Native Development

Support for compiling and packaging existing code written in C or C++. Support for packaging multiple architectures in a single binary, for broad compatibility.

. C. Evaluation 1. What you must prepare for android development ? Operating System Android SDK JDK Bigger and Brighter Mobile Screen Much Money 2. What would motivate you to write programs for mobile devices ? the challenge of writing optimized applications new learning experience fun factor

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 10

D. Practical Guide 1. Tools and Materials a. Laptop (Min Dual Core 1,8 GHz , Memory 2GB) b. Java SDK 1.7 c. ADT-Bundle d. Tablet (Min 512 RAM)

2. Work steps 1) Setup eclipse with ADT Plugin


1) Start Eclipse, if its not already running. 2) Choose HelpInstall New Software. The Install window pops up (see Figure 2-19). This window allows you to install new plug-ins in Eclipse. 3) Click the Add button to add a new site that will display the Add Repository window (see Figure). Sitesare the Web addresses where the software is hosted on the Internet. dding a site to Eclipse makes it easier for you to update the software when a ew version is released

Fig. 1. 2 Repository Window


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 11

4) Type a name in the Name field. I recommend using Android ADT, but it can be anything you choose. 5) Type field. https://dl-ssl.google.com/android/eclipse/in the Location

Fig. 1. 3 Add Repository

6) Click the OK button. Android ADT is selected in the Work With drop-down menu, and the available options are displayed in the Name and Version window of the Install Details dialog box. 7) In the Install dialog box, select the check box next to Developer Tools, and click the Next button (see Figure Below).

Fig. 1. 4 Developer Tools


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 12

The Install Details dialog box should list both the Android Dalvik Debug Monitor Server (DDMS; see Get physical with a real Android device, later in this chapter) and the ADT.

Fig. 1. 5 Install Details

8) Click the Next button to review the software licenses. 9) Click the Finish button. 10) When youre prompted to do so, click the Restart Now button to restart Eclipse. The ADT plug-in is installed.

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 13

b. Liner Layout A layout that arranges its children in a single row 1) Step 1 (Horizontal Layout) Run eclipse,choose File > New > Android Project.

2) Step 2 Fill the new box dialogue with these datas.

Fig. 1. 6 LinearLayout1

Click Next.

Fig. 1. 7 Create Activity LinearLayout1

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 14

Click Next.

Fig. 1. 8 Launcher LinearLayout1

Click Next.

Fig. 1. 9 Blank Activity LinearLayout1

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 15

Click Next.

Fig. 1. 10 Activity Name LinearLayout1

Click Finish.

3) Step 3 Choose LinearLayout1 /res/layout/activity_linear_layout1.xml

Fig. 1. 11 Activity LinearLayout1 XML

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 16

4) Step 4 Type these codes. Although it is too long, for right now you just need to write one pattern, just copy-paste-Edit. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent">

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/LinearLayout1" android:orientation="horizontal" android:layout_weight="1">

<TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:layout_gravity="center_horizontal" android:background="#aa0000" android:text="Red" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:layout_gravity="center_horizontal" android:background="#0000aa"
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 17

android:text="Blue" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:layout_gravity="center_horizontal" android:background="#00aa00" android:text="Green" />

</LinearLayout>

</LinearLayout> 5) Step 5 Layout activity_linear_layout1.xml, if we see this from its hierarky from outline style. It will appear like this

Fig. 1. 12 OutLine LinearLayout1

6) Step 6 Open LinearLayout1.java , see figure below.

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 18

Fig. 1. 13 LinearLayout1.java

and Run Program with shortcut CTRL+F11 or right click and choose package > run as > android Project.

7) Step 7 You will See the result like this

Fig. 1. 14 Output LinearLayout1

8) Step 1 (Vertical Layout) Run eclipse,choose File > New > Android Project.
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 19

9) Step 2 Fill the new box dialogue with these datas.

Fig. 1. 15 New App LinearLayout2

Click Next. Click Next. Click Next. Click Next.

Fig. 1. 16 Activity Name LinearLayout2

Click Finish.

10) Step 3 Open LinearLayout2 /res/layout/activity_linear_layout2.xml


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 20

Fig. 1. 17 LinearLayout2 XML

11) Step 4 Type these codes. Although it is too long, for right now you just need to write one pattern, just copy-paste-Edit. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent">

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/LinearLayout2" android:orientation="vertical" android:layout_weight="1">


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 21

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#aa0000" android:textSize="15pt" android:text="Line 1" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#0000aa" android:textSize="15pt" android:text="Line 2" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#00aa00" android:textSize="15pt" android:text="Line 2" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#aaaa00" android:textSize="15pt" android:text="Line 4" /> </LinearLayout> </LinearLayout>
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 22

12) Step 5 Layout activity_linear_layout2.xml, if we see this from its hierarky from outline style. It will appear like this

Fig. 1. 18 OutLine LinearLayout2

13) Step 6 Open LinearLayout2.java , see figure below.

Fig. 1. 19 LinearLayout2.java

and Run Program with shortcut CTRL+F11 or right click and choose package > run as > android Project.

14) Step 7 You will See the result like this

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 23

Fig. 1. 20 Output Program LinearLayout2

c. Relative Layout RelativeLayout is a layout where the position of components

symbol,text,etc) can be managed and placed in front of other components. For example OKbutton can be placed under Editext , the Cancel button can be placed on the left side of OK button and under Edittext. The main consept is that another. 1) Step 1 Run eclipse,choose File > New > Android Project. it has connection between one button and

2) Step 2 Fill the new box dialogue with these datas.

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 24

Fig. 1. 21 New App RelativeLayout1

Click Next. Click Next. Click Next. Click Next.

Fig. 1. 22 Activity Name RelativeLayout1

Click Finish.

3) Step 3 Open RelativeLayout1 /res/layout/activity_relative_layout1.xml

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 25

Fig. 1. 23 RelativeLayout1 XML

4) Step 4 Type these codes. Although it is too long, for right now you just need to write one pattern, just copy-paste-Edit. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >

<TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Belajar :" />

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 26

<Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="57dp" android:text="OK" />

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/ok" android:layout_toLeftOf="@+id/ok" android:layout_below="@+id/entry" android:text="Cancel" />

<EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/label" android:ems="10" />

</RelativeLayout>

5) Step 5 Layout activity_relative_layout1.xml, if we see this from its hierarky from outline style. It will appear like this
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 27

Fig. 1. 24 Outline RelativeLayout1

6) Step 6 Open RelativeLayout1.java , see figure below.

Fig. 1. 25 RelativeLayout1.java

and Run Program with shortcut CTRL+F11 or right click and choose package > run as > android Project.

7) Step 7 You will See the result like this

Fig. 1. 26 Output program RelativeLayout1


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 28

d. Table Layout A layout that arranges its children into rows and columns. 1) Step 1 Run eclipse,choose File > New > Android Project.

2) Step 2 Fill the new box dialogue with these datas.

Fig. 1. 27 New App TableLayout1

Click Next. Click Next. Click Next. Click Next.

Fig. 1. 28 Activity Name TableLayout1

Click Finish.
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 29

3) Step 3 Open TableLayout1 /res/layout/activity_table_layout1.xml

Fig. 1. 29 TableLayout1 XML

4) Step 4 Type these codes. Although it is too long, for right now you just need to write one pattern, just copy-paste-Edit. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1">

<TableRow> <TextView android:padding="3dp" android:text="Open..." /> <TextView android:text="Ctrl + O"


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 30

android:gravity="right" android:padding="3dp"/> </TableRow>

<TableRow> <TextView android:padding="3dp" android:text="Save..." /> <TextView android:text="Ctrl + S" android:gravity="right" android:padding="3dp"/> </TableRow>

<TableRow> <TextView android:padding="3dp" android:text="Save As..." /> <TextView android:text="Ctrl + Shift + S" android:gravity="right" android:padding="3dp"/> </TableRow>

<View android:layout_height="2dp" android:background="#FF909090"/> <TableRow> <TextView android:padding="3dp"


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 31

android:text="X Import..." /> </TableRow>

<TableRow> <TextView android:padding="3dp" android:text="X Export..." /> <TextView android:text="Ctrl + E" android:gravity="right" android:padding="3dp"/> </TableRow>

<View android:layout_height="2dp" android:background="#FF909090"/> <TableRow>

<TextView android:padding="3dp" android:text="Quit" /> </TableRow> </TableLayout>

5) Step 5 Layout activity_table_layout1.xml, if we see this from its hierarky from outline style. It will appear like this

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 32

Fig. 1. 30 OutLine TableLayout1

6) Step 6 Open TableLayout1.java , see figure below.

Fig. 1. 31 TableLayout1.java

and Run Program with shortcut CTRL+F11 or right click and choose package > run as > android Project.

7) Step 7 You will See the result like this

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 33

Fig. 1. 32 Output Program TableLayout1

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 34

e. Image In this step, you added the ImageViewinside your program. An ImageViewallows you to project an image to the devices screen. 1) Step 1 Run eclipse,choose File > New > Android Project.

2) Step 2 Fill the new box dialogue with these datas.

Fig. 1. 33 New App Image1

Click Next. Click Next. Click Next. Click Next.

Fig. 1. 34 Activity Name Image1

Click Finish.
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 35

3) Step 3 Open Image1 /res/layout/activity_image1.xml

Fig. 1. 35 Image1 XML

4) Step 4 Create a folder with name drawable , in Image1/res/ folder. Like this picture.

Fig. 1. 36 Drawable folder on Image1


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 36

5) Step 5 Prepare your image that you want to display. For example trilateral.png. And copy to folder drawable , so you can see like this

Fig. 1. 37 Copy trilateral.png to drawable folder

6) Step 6 Type these codes. Although it is too long, for right now you just need to write one pattern, just copy-paste-Edit.

<ImageSwitcher xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ImageSwitcher1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >

<ImageView android:id="@+id/imageView1"
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 37

android:layout_width="278dp" android:layout_height="355dp" android:src="@drawable/trilateral" /> </ImageSwitcher>

7) Step 5 Layout activity_image1.xml, if we see this from its hierarky from outline style. It will appear like this

Fig. 1. 38 Outline Image1

8) Step 6 Open Image1.java , see figure below.

Fig. 1. 39 Image1.java

and Run Program with shortcut CTRL+F11 or right click and choose package > run as > android Project. You will see the result like this .

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 38

Fig. 1. 40 Output Image1 Program

9) Step 7 Click on this image on your emulator.

Fig. 1. 41 Menu Image

, You will see your program and the others program Like this

Fig. 1. 42 Application on Android


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 39

10) Step 8 Prepare an Image for your icon , for example icon.png.

Fig. 1. 43 Icon.png

Copy icon.png to drawable folder.

Fig. 1. 44 Copy icon.png to drawable folder

11) Step 9 Open file /Image1/AndroidManifest.xml ,

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 40

Fig. 1. 45 AndroidManifest.xml

12) Step 10 Change this line : android:icon="@drawable/ic_launcher" Like this : android:icon="@drawable/icon"

13) Step 11 Run your application , click this image

Fig. 1. 46 Menu Image

You will see in your application icon like this

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 41

Fig. 1. 47 Application on Android

f. Text Auto Complete 1) Step 1 (Horizontal Layout) Run eclipse,choose File > New > Android Project.

2) Step 2 Fill the new box dialogue with these datas.

Fig. 1. 48 New App TextAuto1

Click Next.
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 42

Click Next. Click Next. Click Next.

Fig. 1. 49 Activity Name TextAuto

Click Finish.

3) Step 3 Open TextAuto1 /res/layout/activity_text_auto1.xml

Fig. 1. 50 TextAuto XML

4) Step 4 Type these codes. Although it is too long, for right now you just need to write one pattern, just copy-paste-Edit.
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 43

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" >

<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Command" />

<AutoCompleteTextView android:id="@+id/auto_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ems="10" android:completionThreshold="3"> <requestFocus /> </AutoCompleteTextView>

<TextView android:id="@+id/m_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout>


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 44

5) Step 5 Layout activity_text_auto1.xml, if we see this from its hierarky from outline style. It will appear like this

Fig. 1. 51 Outline TextAuto

6) Step 6 Open TextAuto1/res/values/string.xml , edit like this : <?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">Make Text Auto Complete Program</string> <string name="Command">Fill with minimal 3 character</string> <string name="hello_world">Hello world!</string>

</resources>

7) Step 7 Open LinearLayout1.java , and change like this code :

package com.example.textauto1;

import android.os.Bundle; import android.app.Activity;


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 45

import android.graphics.Color; import android.view.Menu; import android.text.Editable; import android.text.TextWatcher; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.TextView;

public class TextAuto1 extends Activity implements TextWatcher{

TextView result; AutoCompleteTextView edit; String[] item = { "Indonesia", "China", "Japan", "Qatar", "Germany","Singapura", "Filipina", "Kamboja", "Brunei Darussalam", "Timor Leste","Myanmar", "Thailand", "Malaysia","India" };

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(android.R.style.Theme_Light); setContentView(R.layout.activity_text_auto1);

result = (TextView)findViewById(R.id.m_result); edit = (AutoCompleteTextView)findViewById(R.id.auto_edit);

edit.addTextChangedListener(this); edit.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item)); edit.setTextColor(Color.BLACK); }


Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 46

@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.text_auto1, menu); return true; }

@Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub }

@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub }

@Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub result.setText(edit.getText()); } }

and Run Program with shortcut CTRL+F11 or right click and choose package > run as > android Project.
Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013 Page 47

8) Step 7 You will See the result like this

Fig. 1. 52 Output Program Text Auto

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 48

E. Assigment Implement any/all of the following projects using simple text boxes (EditText, TextView) and buttons : Create Common Calculator

Fig. 1. 53 Assignment Calculator

Change your calculator icon to this image

Fig. 1. 54 Icon for Assignment

Myanmar Vocational Teacher Training/ module compilation guidance/ batch 3/ 2013

Page 49

Das könnte Ihnen auch gefallen