Android ¡Applica,on ¡ Development: ¡Hands-‑On ¡
- Dr. ¡Eddie ¡Chan ¡
Android Applica,on Development: Hands-On Dr. Eddie Chan - - PowerPoint PPT Presentation
Android Applica,on Development: Hands-On Dr. Eddie Chan csclchan@cse.ust.hk Venue and Materials The workshop will be conducted in room 4213 and room
Android Workshop (Eddie) Introduction 2
h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3168769&theme=WEB ¡
h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3216815&theme=WEB ¡
h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3261386&theme=WEB ¡
h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3230706&theme=WEB ¡
hOp://developer.android.com/develop/index.html ¡ hOp://android-‑developers.blogspot.hk/ ¡
Android Workshop (Eddie) Introduction 3
Android Workshop (Eddie) Introduction 4
Android Workshop (Eddie) Introduction 5
Android Workshop (Eddie) Introduction 6
Android Workshop (Eddie) Introduction 7
– You ¡should ¡choose ¡the ¡Java ¡code ¡edi,ng ¡environment ¡by ¡selec,ng ¡ Window ¡> ¡Open ¡Perspec[ve ¡> ¡Java ¡ ¡ – If ¡you ¡cannot ¡see ¡the ¡Java ¡icon, ¡you ¡click ¡Other... ¡to ¡look ¡for ¡the ¡Java ¡ code ¡edi,ng ¡environment ¡
Android Workshop (Eddie) Introduction 8
Android Workshop (Eddie) Introduction 9
Android Workshop (Eddie) Introduction 10
Android Workshop (Eddie) Introduction 11
Android ¡SDK ¡Manager ¡in ¡the ¡Android ¡SDK ¡and ¡AVD ¡Manager ¡group ¡ ¡
the ¡toolbar ¡you ¡should ¡see ¡an ¡Android ¡SDK ¡and ¡AVD ¡manager ¡grouping ¡
consists ¡of ¡three ¡views, ¡ Package ¡Explorer, ¡Outline ¡ and ¡Task ¡List ¡if ¡you ¡choose ¡to ¡ use ¡Java ¡perspec,ve ¡
find ¡all ¡informa,on ¡for ¡the ¡ project, ¡for ¡example, ¡source ¡ code, ¡compile ¡sources, ¡ libraries, ¡manifest, ¡intent-‑ filters, ¡uses-‑permissions ¡ ¡
Android Workshop (Eddie) Introduction 12
– “src” ¡stores ¡the ¡code ¡which ¡developers ¡write; ¡ ¡ – “gen” ¡stores ¡the ¡generated ¡Java ¡files ¡by ¡the ¡system; ¡ ¡ – “Android ¡Library” ¡contains ¡a ¡file ¡named ¡android.jar ¡which ¡is ¡the ¡ Android ¡library ¡class ¡file; ¡ – “assets” ¡stores ¡the ¡source ¡code ¡or ¡files ¡which ¡are ¡not ¡Java ¡classes ¡and ¡ later ¡retrieved ¡as ¡raw ¡byte ¡stream; ¡ ¡ – “bin” ¡stores ¡the ¡binary ¡and ¡executable ¡files ¡which ¡is ¡generated ¡by ¡ compiler ¡ ¡
Android Workshop (Eddie) Introduction 13
– “res” ¡stores ¡all ¡the ¡resources ¡used ¡by ¡your ¡Android ¡applica,on. ¡ ¡ For ¡example, ¡the ¡drawable ¡folder ¡contains ¡a ¡png ¡image ¡file ¡that ¡is ¡used ¡ as ¡the ¡icon ¡for ¡your ¡applica,on. ¡The ¡layout ¡folder ¡contains ¡an ¡XML ¡file ¡ used ¡to ¡represent ¡the ¡user ¡interface ¡of ¡your ¡Android ¡applica,on. ¡The ¡ values ¡folder ¡contains ¡an ¡XML ¡file ¡used ¡to ¡store ¡a ¡list ¡of ¡string ¡
– AndroidManifest.xml ¡file ¡is ¡an ¡applica,on ¡configura,on ¡file ¡that ¡ contains ¡detailed ¡informa,on ¡about ¡your ¡applica,on, ¡such ¡as ¡the ¡ number ¡of ¡ac,vi-‑ ¡,es ¡you ¡have ¡in ¡your ¡applica,on, ¡the ¡types ¡of ¡ permissions ¡your ¡applica,on ¡needs, ¡the ¡version ¡informa,on ¡of ¡your ¡ applica,on, ¡and ¡so ¡on. ¡
Android Workshop (Eddie) Introduction 14
Android Workshop (Eddie) Introduction 15
Android Workshop (Eddie) Introduction 16
Android Workshop (Eddie) Introduction 17
Android Workshop (Eddie) Introduction 18
Android Workshop (Eddie) Introduction 19
Android Workshop (Eddie) Introduction 20
Android Workshop (Eddie) Introduction 21
Android Workshop (Eddie) Introduction 22
Meaning ¡of ¡the ¡different ¡fields: ¡ – Project ¡Name ¡ ¡
contain ¡the ¡project ¡files. ¡Use ¡“helloandroid” ¡
– Build ¡Target ¡
Android ¡applica,ons ¡are ¡forward-‑compa,ble, ¡and ¡recall ¡that ¡we ¡have ¡ select ¡our ¡AVD ¡version ¡as ¡Android ¡2.2, ¡you ¡may ¡select ¡any ¡Android ¡version ¡ that ¡is ¡not ¡higher ¡than ¡2.2. ¡
– Applica5on ¡Name ¡ ¡
appear ¡on ¡the ¡Android ¡device. ¡Use ¡Hello ¡Android. ¡
Android Workshop (Eddie) Introduction 23
– Package ¡Name ¡ ¡
programming ¡language) ¡that ¡you ¡want ¡all ¡your ¡source ¡code ¡to ¡reside ¡under. ¡This ¡also ¡ sets ¡the ¡package ¡name ¡under ¡which ¡the ¡stub ¡Ac,vity ¡will ¡be ¡generated. ¡ ¡
for ¡this ¡reason, ¡it's ¡important ¡to ¡use ¡a ¡standard ¡domain-‑style ¡package ¡for ¡your ¡ applica,ons. ¡Here ¡we ¡use ¡the ¡”hkust.cse.HelloAndroid ¡" ¡namespace, ¡which ¡is ¡a ¡ namespace ¡reserved ¡for ¡example ¡documenta,on ¡— ¡when ¡you ¡develop ¡your ¡own ¡ applica,ons, ¡you ¡should ¡use ¡a ¡namespace ¡that's ¡appropriate ¡to ¡your ¡organiza,on ¡or ¡ en,ty. ¡ ¡
– Create ¡Ac5vity ¡ ¡
subclass ¡of ¡Android's ¡Ac,vity ¡class. ¡An ¡Ac,vity ¡is ¡simply ¡a ¡class ¡that ¡can ¡run ¡and ¡do ¡work. ¡ It ¡can ¡create ¡a ¡UI ¡if ¡it ¡chooses, ¡but ¡it ¡doesn't ¡need ¡to. ¡As ¡the ¡checkbox ¡suggests, ¡this ¡is ¡
use ¡HelloAndroid. ¡
– Min ¡SDK ¡Version ¡ ¡
set ¡to ¡the ¡API ¡Level ¡of ¡the ¡Build ¡Target ¡Plaworm. ¡As ¡new ¡APIs ¡are ¡added ¡to ¡newer ¡ Versions, ¡their ¡API ¡levels ¡increase ¡as ¡well. ¡A ¡Program ¡that ¡uses ¡an ¡API ¡Level ¡of ¡four ¡won't ¡ be ¡able ¡to ¡run ¡on ¡a ¡plaworm ¡that ¡has ¡a ¡lower ¡API ¡Level. ¡ ¡
Android Workshop (Eddie) Introduction 24
– No,ce ¡that ¡the ¡class ¡is ¡based ¡on ¡the ¡Ac[vity ¡class. ¡ – An ¡Ac,vity ¡is ¡a ¡single ¡applica,on ¡en,ty ¡that ¡is ¡used ¡to ¡perform ¡ac,ons. ¡ An ¡applica,on ¡may ¡have ¡many ¡separate ¡ac,vi,es, ¡but ¡the ¡user ¡ interacts ¡with ¡them ¡one ¡at ¡a ¡,me. ¡ – The ¡onCreate() ¡method ¡will ¡be ¡called ¡by ¡the ¡Android ¡system ¡when ¡ your ¡Ac,vity ¡starts ¡— ¡it ¡is ¡where ¡you ¡should ¡perform ¡all ¡ini,aliza,on ¡ and ¡UI ¡setup. ¡ – An ¡ac,vity ¡is ¡not ¡required ¡to ¡have ¡a ¡user ¡interface, ¡but ¡usually ¡will. ¡ ¡
Android Workshop (Eddie) Introduction 25
¡ ¡ ¡public ¡void ¡onCreate(Bundle ¡savedInstanceState) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡super.onCreate(savedInstanceState); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡TextView ¡tv ¡= ¡new ¡TextView(this); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tv.setText("Hello, ¡Android"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡setContentView(tv); ¡ ¡ ¡ ¡} ¡
– you ¡can ¡also ¡type ¡yourself: ¡import ¡android.widget.TextView; ¡ – Tip: ¡An ¡easy ¡way ¡to ¡add ¡import ¡packages ¡to ¡your ¡project ¡is ¡to ¡press ¡Ctrl-‑ ShiE-‑O ¡(Cmd-‑ShiE-‑O, ¡on ¡Mac). ¡This ¡is ¡an ¡Eclipse ¡shortcut ¡that ¡iden5fies ¡ missing ¡packages ¡based ¡on ¡your ¡code ¡and ¡adds ¡them ¡for ¡you. ¡ ¡
Android Workshop (Eddie) Introduction 26
A ¡View ¡is ¡a ¡drawable ¡object ¡used ¡as ¡an ¡element ¡in ¡your ¡UI ¡layout, ¡such ¡as ¡a ¡ buOon, ¡image, ¡or ¡(in ¡this ¡case) ¡a ¡text ¡label. ¡Each ¡of ¡these ¡objects ¡is ¡a ¡subclass ¡
accepts ¡an ¡Android ¡Context ¡instance ¡as ¡its ¡parameter. ¡A ¡Context ¡is ¡a ¡handle ¡to ¡ the ¡system; ¡it ¡provides ¡services ¡like ¡resolving ¡resources, ¡obtaining ¡access ¡to ¡ databases ¡and ¡preferences, ¡and ¡so ¡on. ¡The ¡Ac,vity ¡class ¡inherits ¡from ¡Context, ¡ and ¡because ¡your ¡HelloAndroid ¡class ¡is ¡a ¡subclass ¡of ¡Ac,vity, ¡it ¡is ¡also ¡a ¡
content ¡for ¡the ¡Ac,vity ¡UI. ¡If ¡your ¡Ac,vity ¡doesn't ¡call ¡this ¡method, ¡then ¡no ¡UI ¡ is ¡present ¡and ¡the ¡system ¡will ¡display ¡a ¡blank ¡screen. ¡
Android Workshop (Eddie) Introduction 27
– AIer ¡se~ng ¡a ¡breakpoint, ¡select ¡Run-‑> ¡Debug, ¡and ¡Eclipse ¡will ¡restart ¡ your ¡emulator. ¡But ¡this ¡,me ¡it ¡will ¡suspend ¡when ¡it ¡reaches ¡the ¡ breakpoint ¡you ¡set. ¡You ¡can ¡then ¡step ¡through ¡the ¡code ¡in ¡Eclipse's ¡ Debug ¡Perspec,ve, ¡just ¡as ¡you ¡would ¡for ¡any ¡other ¡applica,on. ¡ ¡
Android Workshop (Eddie) Introduction 28
– In ¡the ¡Eclipse ¡Package ¡Explorer, ¡select ¡/res/layout/main.xml ¡
interfaces ¡
– ¡Modify ¡the ¡contents ¡of ¡the ¡file ¡to ¡the ¡following: ¡
<?xml ¡version="1.0" ¡encoding="uw-‑8"?> ¡ <TextView ¡xmlns:android="hOp://schemas.android.com/apk/res/android" ¡ ¡ ¡android:id="@+id/textview" ¡ ¡ ¡android:layout_width="fill_parent" ¡ ¡ ¡android:layout_height="fill_parent" ¡ ¡ ¡android:text="@string/hello"/> ¡ ¡
Android Workshop (Eddie) Introduction 29
¡ ¡ ¡ ¡public ¡void ¡onCreate(Bundle ¡savedInstanceState) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡super.onCreate(savedInstanceState); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡setContentView(R.layout.main); ¡ ¡ ¡ ¡ ¡} ¡
android.widget.Textview ¡is ¡not ¡needed, ¡and ¡instead ¡of ¡passing ¡ setContentView() ¡a ¡View ¡object, ¡you ¡give ¡it ¡a ¡reference ¡to ¡the ¡ layout ¡resource. ¡The ¡resource ¡is ¡iden,fied ¡as ¡R.layout.main, ¡which ¡ is ¡actually ¡a ¡compiled ¡object ¡representa,on ¡of ¡the ¡layout ¡defined ¡ in ¡/res/layout/main.xml. ¡
applica,on ¡and ¡the ¡text ¡has ¡been ¡changed. ¡ ¡
Android Workshop (Eddie) Introduction 30
– you ¡can ¡see ¡the ¡values ¡of ¡the ¡two ¡strings: ¡hello ¡and ¡app_name ¡defined ¡
Android Workshop (Eddie) Introduction 31