Trainingandroid Pptxrepaired 120527192357 Phpapp02

75
Training Android @HCMC University of Technology Summer 2012 Editor Nguyễn Đức Minh Khôi [email protected] nguyenducminhkhoi.blogspot.com

description

Trainingandroid

Transcript of Trainingandroid Pptxrepaired 120527192357 Phpapp02

  • Training Android @HCMC University of Technology Summer 2012

    Editor Nguyn c Minh Khi [email protected] nguyenducminhkhoi.blogspot.com

  • Introduction What is Android?

    The Android operating system was developed by Android, Inc., Was acquired by Google in July 2005. Developing Android apps is the openness of the platform The operating system is open source and free.

    June 2012 Training Android

    Page: 4

  • Introduction (cont.)

    Java: was a logical choice for the Android platform enables you to develop apps that will run on a variety of devices without any platform-specific code. is object oriented and has access to powerful class libraries that help you develop apps quickly.

    June 2012 Training Android

    Page: 6

  • Introduction (cont.)

    XML: enables you to better separate the presentation of your application from the code that controls its behavior. makes it easier to visualize the structure of your UI, so it's easier to debug problems Is similar to HTML

    June 2012 Training Android

    Page: 7

  • Introduction (cont.) Multitouch screen:

    June 2012 Training Android

    Page: 8

  • Introduction (cont.)

    Android versions: Android 1.6 (Donut) Android 2.02.1 (Eclair) Android 2.2 (Froyo) Android 2.3 (Gingerbread) Android 3.0 (Honeycomb) Android 4.0 (Ice Cream Sandwich) Android 5.0 (Jelly Beans)

    See more at: http://developer.android.com/resources/dashboard/platform-versions.html

    June 2012 Training Android

    Page: 9

  • Introduction (cont.)

    Types of android apps (go to https://play.google.com/store) for more details Other sites you can download apps:

    June 2012 Training Android

    Page: 10

  • Introduction (cont.)

    June 2012 Training Android

    Page: 11

  • How to set up

    Software and hardware system requirement: To develop Android apps you need a Windows, Linux or Mac OS X system. Java SE 5 or higher Software Development Kit Eclipse 3.7 (Indigo) IDE for Java Developers Android SDK versions 2.2, 2.3.3 and 3.x ADT (Android Development Tools) Plugin for Eclipse

    June 2012 Training Android

    Page: 12

  • How to set up (cont.)

    Install JDK (Java Development Kit) 6: Go to www.oracle.com/technetwork/java/javase/index-137561.html

    Install Eclipse IDE Go to www.eclipse.org/downloads Choose Eclipse IDE for Java Developers

    Install the Android SDK Go to www.developer.android.com/sdk/index.html Click and choose your suitable platform

    Install ADT plugin for Eclipse Go to www.developer.android.com/sdk/eclipse-adt.html carefully follow the instructions for downloading and installing the ADT Plugin

    June 2012 Training Android

    Page: 13

  • How to set up (cont.)

    Install Android platform:

    June 2012 Training Android

    Page: 14

  • How to set up (cont.)

    June 2012 Training Android

    Page: 15

  • How to set up (cont.)

    Creating Android Virtual Device (AVD): The Android emulator, included in the Android SDK, allows you to run Android apps in a simulated environment on your computer rather than on an actual Android device.

    June 2012 Training Android

    Page: 16

  • How to set up (cont.)

    June 2012 Training Android

    Page: 17

  • Source for learning

    Best resource for learning and referencing: http://developer.android.com/index.html

    June 2012 Training Android

    Page: 18

  • Source for learning (cont.)

    June 2012 Training Android

    Page: 19

  • Source for learning (cont.)

    June 2012 Training Android

    Page: 20

  • Welcome apps (cont.)

    June 2012 Training Android

    Page: 22

  • Eclipse IDE

    June 2012 Training Android

    Page: 23

  • Eclipse IDE (cont.) Create new Project:

    June 2012 Training Android

    Page: 24

  • Eclipse IDE (cont.) Platform Codename API Level

    Android 1.5 Cupcake 3

    Android 1.6 Donut 4

    Android 2.1 Eclair 7

    Android 2.2 Froyo 8

    Android 2.3 -Android 2.3.2

    Gingerbread 9

    Android 2.3.3 -Android 2.3.7

    10

    Android 3.0 Honeycomb 11

    Android 3.1 12

    Android 3.2 13

    Android 4.0 -Android 4.0.2

    Ice Cream Sandwich

    14

    Android 4.0.3 -Android 4.0.4

    15

    June 2012 Training Android

    Page: 25

  • Developing GUI

    June 2012 Training Android

    Page: 26

  • Developing GUI (cont.)

    June 2012 Training Android

    Page: 27

  • Developing GUI (cont.)

    June 2012 Training Android

    Page: 28

  • Developing GUI (cont.)

    June 2012 Training Android

    Page: 29

  • Developing GUI (cont.)

    June 2012 Training Android

    Page: 30

  • Developing GUI (cont.)

    June 2012 Training Android

    Page: 31

  • Running the Welcome apps To run the app in an Android Virtual Device (AVD), right click the apps root node in the Package Explorer window and select Run As > Android Application.

    June 2012 Training Android

    Page: 32

  • Building Features

    Each app below will expand on one Class hour By practicing these apps you will learn some interesting features Each app will contain:

    Introductions and objectives New Features Building GUI Adding functions Results

    June 2012 Training Android

    Page: 33

  • Building Features

    Each app below will expand on one Class hour By practicing these apps you will learn some interesting features Each app will contain:

    Introductions and objectives Technologies Overview Building GUI Adding functions Results

    June 2012 Training Android

    Page: 34

  • Building Features (cont.)

    1.Tip Calculator App 2.Favorite Twitter Searches App 3.Flag Quiz Game App 4.Cannon Game App 5.Spot on Game App 6.Doodlz App 7.Address Book App 8.Slideshow App 9.Enhanced Slideshow App 10.Weather Viewer App

    June 2012 Training Android

    Page: 35

  • Tips Calculator App

    Objectives: Building an Android App with Java Design a GUI using a TableLayout Directly edit the XML of a GUI layout to customize properties Use Android Activitys subclass interact with EditTexts, a TextView and a SeekBar.

    June 2012 Training Android

    Page: 36

  • Tips Calculator App (cont.)

    Introduction:

    The Tip Calculator app calculates and displays tips

    for a restaurant bill. As the user enters a bill total,

    the app calculates and displays the tip amount and

    total bill for three common tipping percentages

    10%, 15% and 20%. The user can also specify a

    custom tip percentage by moving the thumb of a

    Seekbarthis updates the percentage shown to the

    right of the Seekbar. The suggested tips and bill

    totals are updated in response to each user

    interaction.

    June 2012 Training Android

    Page: 37

  • Tips Calculator App (cont.)

    Technologies Overview: TableLayout and TableRow introduction See more at: www.developer.android.com/reference/android/widget/TableLayout.html www.developer.android.com/reference/android/widget/TableRow.html

    June 2012 Training Android

    Page: 38

  • Tips Calculator App (cont.) Building GUI: Create GUI with the following information

    June 2012 Training Android

    Page: 39

  • Tips Calculator App (cont.)

    main.xml describe GUI like figure above String.xml

    June 2012 Training Android

    Page: 40

  • Tips Calculator App (cont.)

    Adding Functions: Remember when you want to import some library, use just type the name of the class (dont need to complete) and press (Ctrl + Space), it will import immediately Class Activity of package android.app provides the basic lifecycle methods of an appwell discuss these shortly. Class Bundle of package android.os represents an apps state information. An app can save its state when its sent to the background by the operating systemfor example, when the user launches another app or a phone call is received.

    June 2012 Training Android

    Page: 41

  • Tips Calculator App (cont.) Android apps doesnt have main method four types of componentsactivities, services, content providers and broadcast receivers A separate activity is typically associated with each screen of an app Throughout its life an activity can be in one of several statesactive (or running), paused or stopped.

    An active (or running) activity is visible on the screen and has the focus A paused activity is visible on the screen but doesnt have the focus. can be killed when its memory is needed by the operating system A stopped activity is not visible on the screen and is likely to be killed by the system when its memory is needed.

    See more at: www.developer.android.com/reference/android/app/Activity.html

    June 2012 Training Android

    Page: 42

  • Tips Calculator App (cont.)

    Override method: onCreate: called by the system when an Activity is startingthat is, when its GUI is about to be displayed so that the user can interact with the Activity. onSaveInstanceState: called by the system when the configuration of the device changes during the apps execution

    Remember: Each activity lifecycle method you override must call the superclasss version of that method first; otherwise, an exception will be thrown when that method is called.

    June 2012 Training Android

    Page: 43

  • Tips Calculator App (cont.)

    R content class: Class drawablecontains constants for any drawable items, such as images, that you put in the various drawable folders in your apps res folder Class idcontains constants for the GUI components in your XML layout files Class layoutcontains constants that represent each layout file in your project (such as, main.xml) Class stringcontains constants for each String in the strings.xml file

    June 2012 Training Android

    Page: 44

  • Tips Calculator App (cont.) Results:

    June 2012 Training Android

    Page: 45

  • Favorite Twitter Searches App Objectives: SharedPreferences, Buttons, Nested Layouts, Intents, AlertDialogs, Inflating XML Layouts and the Manifest File Use a ScrollView to display objects that do not fit on the screen. Programmatically open a website in a web browser by using an Intent. Programmatically hide the soft keyboard.

    June 2012 Training Android

    Page: 46

  • Favorite Twitter Searches App (cont.) Introduction: The Favorite Twitter Searches app allows users to save their favorite (possibly lengthy) Twitter search strings with easy-to-remember, user-chosen, short tag names. Users can then conveniently follow the tweets on their favorite topics. Twitter search queries can be finely tuned using Twitters search operators (dev.twitter.com/docs/using-search)but more complex queries are lengthy, time consuming and error prone to type on a mobile device. The users favorite searches are saved on the device, so theyre immediately available each time the app launches.

    June 2012 Training Android

    Page: 47

  • Favorite Twitter Searches App (cont.)

    June 2012 Training Android

    Page: 48

  • Favorite Twitter Searches App (cont.) Technologies Overview: ScrollView: is a ViewGroup that can contain other Views (like a layout) and that lets users scroll through content too large to display on the screen. To read the key/value pairs from this file well use SharedPreferences objects (package android.content). To modify the files contents, well use SharedPreferences.Editor objects (package android.content). The keys in the file must be Strings, and the values can be Strings or primitive-type values Intents are typically used to launch activitiesthey indicate an action to be performed and the data on which that action is to be performed. We load the URL into a web browser by creating a new Intent for viewing a URL, then passing that Intent to the startActivity method, which our Activity inherits indirectly from class Context

    June 2012 Training Android

    Page: 49

  • Favorite Twitter Searches App (cont.) New Features: LayoutInflater programmatically creates these GUI components from a predefined XML layout. The LayoutInflater inflates an XML layout file, thus creating the components specified in the XML. You can display messages and confirmations like these with an AlertDialog. We specify the settings for the dialog with an AlertDialog.Builder object, then use it to create the AlertDialog. The AndroidManifest.xml file is created for you when you create an app using the ADT Plugin in Eclipse. This file specifies settings such as the apps name, the package name, the target and minimum SDKs, the apps Activity name(s) and more

    June 2012 Training Android

    Page: 50

  • Favorite Twitter Searches App (cont.) Building the GUI:

    June 2012 Training Android

    Page: 51

  • Favorite Twitter Searches App (cont.) Adding functions: Package android.widget contains the widgets (i.e., GUI components) and layouts that are used in Android GUIs. Class Button of package android.widget represents a simple push button that the user touches to get the app to perform a specific action. You implement interface View.OnClickListener of package android.view to specify the code that should execute when the user touches a Button Overridden Method OnCreate of Class Activity, called by system when:

    when the app loads if the apps process was killed by the operating system while the app was in the background, and the app is then restored each time the configuration changes, such as when the user rotates the device or opens/closes a physical keyboard.

    June 2012 Training Android

    Page: 52

  • Favorite Twitter Searches App (cont.) Adding functions: Method getSharedPreferences to get a SharedPreferences object that can read tag/query pairs stored previously (if any) from the "searches" file. The first argument indicates the name of the file that contains the data. The second argument specifies the accessibility of the file and can be set to one of the following options:

    MODE_PRIVATE The file is accessible only to this app. In most cases, youll use this constant as the second argument to getSharedPreferences. MODE_WORLD_READABLE Any app on the device can read from the file. MODE_WORLD_WRITABLE Any app on the device can write to the file.

    June 2012 Training Android

    Page: 53

  • Favorite Twitter Searches App (cont.) AndroidManifest.xml For complete information, refer to this page: www.developer.android.com/guide/topics/manifest/manifest-intro.html

    June 2012 Training Android

    Page: 54

  • Favorite Twitter Searches App (cont.)

    June 2012 Training Android

    Page: 55

  • Flag Quiz Game App Objectives: Assets, AssetManager, Tweened Animations, Handler, Menus and Logging Error Messages. Use an AssetManager to get a list of all assets in an app. Use random-number generation to vary flag choices. Use a Drawable to display a flag image in an ImageView. Use a Handler to schedule a future action. Use an ArrayList to hold collections of items and a HashMap to hold namevalue pairs. Override Activitys onCreateOptionsMenu method to create a Menu and MenuItems that enable the user to configure the apps options. Use Androids logging mechanism to log error messages.

    June 2012 Training Android

    Page: 56

  • Flag Quiz Game App (cont.) Introduction:

    The Flag Quiz Game app tests the users ability to correctly identify country flags. Initially, the app presents the user with a flag image and three possible answersone matches the flag and the others are randomly selected, nonduplicated incorrect answers. The app displays the users progress throughout the quiz, showing the question number (out of 10) in a TextView above the current flag image.

    June 2012 Training Android

    Page: 57

  • Flag Quiz Game App (cont.)

    June 2012 Training Android

    Page: 58

  • Flag Quiz Game App (cont.) Technologies Overview: Using the Apps assets Folder: the assets folder may contain files of any type that can be organized in subfolderswe maintain the flag images for each region in a separate subfolder. Files in the assets folders are accessed via an AssetManager (package android.content.res), which can provide a list of all of the file names in a specified subfolder of assets and can be used to access each asset. Using a Menu to Provide App Options: override Activitys onCreateOptionsMenu method and add the options to the Menu that the method receives as an argument. Activity method onOptionsItemSelected is called to respond to the selection. Using a Handler to Execute a Runnable in the Future: . Handler method postDelayed receives as arguments a Runnable to execute and a delay in milliseconds.

    June 2012 Training Android

    Page: 59

  • Flag Quiz Game App (cont.) Technologies Overview (cont.): Animating the Flag When an Incorrect Choice Is Touched: AnimationUtils static method loadAnimation to load the animation from an XML file that specifies the animations options. We also specify the number of times the animation should repeat with Animation method setRepeatCount and perform the animation by calling View method startAnimation (with the Animation as an argument) on the ImageView. Logging Exception Messages with Log.e: When exceptions occur, you can log them for debugging purposes with Androids built-in logging mechanism, which uses a circular buffer to store the messages for a short time. Android provides class Log (package android.util) with several static methods that represent messages of varying detail. Java Data Structures: ArrayList, Map, Set

    June 2012 Training Android

    Page: 60

  • Flag Quiz Game App (cont.) Building the App GUI: Main.xml Linear Layout

    June 2012 Training Android

    Page: 61

  • Flag Quiz Game App (cont.) colors.xml & dimen.xml:

    June 2012 Training Android

    Page: 62

  • Flag Quiz Game App (cont.) string.xml:

    June 2012 Training Android

    Page: 63

  • Flag Quiz Game App (cont.)

    June 2012 Training Android

    Page: 64

  • Flag Quiz Game App (cont.) Study more: View animations to create a shake effect that consists of three animations in an animation set a collection of animations which make up a larger animation. Animation sets may contain any combination of tweened animations alpha (transparency), scale (resize), translate (move) and rotate. android:fromXDelta attribute is the Views offset when the animation starts and the android:toXDelta attribute is the Views offset when the animation ends. These attributes can have

    absolute values (in pixels) a percentage of the animated Views size a percentage of the animated Views parents size

    The android:duration attribute specifies how long the animation lasts in milliseconds. android:startOffset attribute to specify the number of milliseconds into the future at which an animation should begin.

    June 2012 Training Android

    Page: 65

  • Flag Quiz Game App (cont.) Building the App AndroidManifest.xml:

    June 2012 Training Android

    Page: 66

  • Cannon Game App Objectives: Create a simple game app thats easy to code and fun to play. Create a custom SurfaceView subclass and use it to display the games graphics from a separate thread of execution. Draw graphics using Paints and a Canvas. Override Activitys onTouchEvent to process touch events when the user touches the screen or drags a finger on the screen. Use a GestureDetector to recognize more sophisticated user touch motions, such as double taps. Perform simple collision detection. Add sound to your app using a SoundPool and the AudioManager. Override three additional Activity lifecycle methods.

    June 2012 Training Android

    Page: 67

  • Cannon Game App (cont.) Introduction:

    The Cannon Game app challenges you to destroy a seven-piece target before a ten-second time limit expires. The game consists of four visual componentsa cannon that you control, a cannonball, the target and a blocker that defends the target. You aim the cannon by touching the screenthe cannon then aims at the touched point. The cannon fires a cannonball when you double-tap the screen. At the end of the game, the app displays an AlertDialog indicating whether you won or lost, and showing the number of shots fired and the elapsed time

    June 2012 Training Android

    Page: 68

  • Cannon Game App (cont.)

    June 2012 Training Android

    Page: 69

  • Cannon Game App (cont.)

    June 2012 Training Android

    Page: 70

  • Cannon Game App (cont.) Technologies Overview Defining String Formatting Resources in strings.xml Attaching a Custom View to a Layout Using the Resource Folder raw Activity Lifecycle Methods onPause and onDestroy:

    Method onPause is called for the current Activity when another activity receives the focus, which sends the current activity to the background. We use onPause to suspend game play so that the game does not continue executing when the user cannot interact with it. When an Activity is shut down, its onDestroy method is called. We use this method to release the apps sound resources.

    Overriding Activity Method onTouchEvent

    June 2012 Training Android

    Page: 71

  • Cannon Game App (cont.) Technologies Overview (cont.) GestureDetector and SimpleOnGestureListener:

    A GestureDetector allows an app to react to more sophisticated user interactions such as flings, double-taps, long presses and scrolls. Your apps can respond to such events by implementing the methods of the GestureDetector. GestureDetector.SimpleOnGestureListener is an adapter class that implements all the methods of these two interfaces, so you can extend this class and override just the method(s) you need from these interfaces.

    Adding Sound with SoundPool and AudioManager Frame-by-Frame Animation with Threads, SurfaceView and SurfaceHolder Simple Collision Detection Drawing Graphics Using Paint and Canvas

    June 2012 Training Android

    Page: 72

  • Cannon Game App (cont.) Building Apps GUI and resource files:

    June 2012 Training Android

    Page: 73

  • Cannon Game App (cont.) Building Apps GUI and resource files (cont.):

    June 2012 Training Android

    Page: 74

  • Cannon Game App (cont.) Building Apps GUI and resource files (cont.): The notation 1$ in %1$.1f (line 5) indicates that the first argument after the format String should replace the format specifier %1$d. Similarly, %2$.1f indicates that the second argument after the format String should replace the format specifier %2$.1f.

    Adding functions (regarding to instructors)

    June 2012 June 2012 Training Android

    Page: 75