Android Applica,on Development: Hands-On Dr. Eddie Chan - - PowerPoint PPT Presentation

android applica on development hands on
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Android ¡Applica,on ¡ Development: ¡Hands-­‑On ¡

  • Dr. ¡Eddie ¡Chan ¡

csclchan@cse.ust.hk ¡

slide-2
SLIDE 2

Venue ¡and ¡Materials ¡

  • The ¡workshop ¡will ¡be ¡conducted ¡in ¡room ¡4213 ¡and ¡room ¡4210 ¡

near ¡to ¡the ¡LiI ¡21, ¡HKUST ¡

  • All ¡materials ¡will ¡be ¡posted ¡one ¡week ¡before ¡the ¡workshop: ¡

hOp://www.cse.ust.hk/AADC/2013/notes.html ¡

  • Encourage ¡your ¡students ¡to ¡visit ¡and ¡raise ¡many ¡ques,ons ¡in ¡
  • ur ¡Facebook ¡page: ¡

hOps://www.facebook.com/pages/Hong-­‑Kong-­‑Android-­‑ Applica,on-­‑Design-­‑Contest-­‑2013/209579215883406 ¡

Android Workshop (Eddie) Introduction 2

slide-3
SLIDE 3

Chinese ¡Reference ¡Books ¡

  • 前進Android ¡Market! ¡Google ¡Android ¡SDK實戰演練 / ¡何孟翰著. ¡

h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3168769&theme=WEB ¡

  • Android實戰活用學 : ¡無痛學Android ¡/ ¡Fenix編著. ¡

h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3216815&theme=WEB ¡

  • Android錦囊妙計 = ¡Android ¡cookbook ¡/ ¡Ian ¡F. ¡Darwin編著 ; ¡李宜修譯. ¡

h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3261386&theme=WEB ¡

  • Android ¡4.X ¡App開發教戰手冊 : ¡適用Android ¡4.x-­‑2.x ¡/ ¡[黃彬華著]. ¡

h7ps://webcat.hkpl.gov.hk/lib/item?id=chamo:3230706&theme=WEB ¡

  • Useful ¡Online ¡References: ¡

hOp://developer.android.com/develop/index.html ¡ hOp://android-­‑developers.blogspot.hk/ ¡

Android Workshop (Eddie) Introduction 3

slide-4
SLIDE 4

Useful ¡YouTube ¡Videos ¡for ¡Learning ¡ Android ¡

  • HKUST ¡Android ¡Lecture ¡by ¡Prof. ¡Muppala: ¡

hOp://www.youtube.com/user/jmuppala ¡

  • Hands-­‑on ¡Android ¡Prac,ce:

hOp://www.youtube.com/watch?v=CxPh1tgiK2g ¡

  • Android ¡教學 ¡(普通話) ¡/ ¡Putonghua ¡

hOp://www.youtube.com/watch?v=x795UuKIuAA ¡ hOp://www.youtube.com/watch?v=QMqfRrDa9NA ¡

Android Workshop (Eddie) Introduction 4

slide-5
SLIDE 5

Bonus ¡Points ¡

  • To ¡encourage ¡the ¡par,cipa,ons, ¡in ¡each ¡workshop, ¡we ¡have ¡a ¡

set ¡of ¡task ¡required ¡for ¡students ¡to ¡finish ¡ ¡

  • If ¡they ¡can ¡complete ¡the ¡tasks, ¡2 ¡bonus ¡points ¡will ¡be ¡counted ¡

into ¡the ¡evalua,on ¡of ¡the ¡compe,,on. ¡We ¡have ¡4 ¡workshops, ¡ total: ¡4 ¡x ¡2 ¡points ¡

Android Workshop (Eddie) Introduction 5

slide-6
SLIDE 6

Configure ¡the ¡Android ¡SDK ¡

  • SDK ¡= ¡SoIware ¡Development ¡Kit ¡
  • First ¡move ¡to ¡the ¡Eclipse ¡directory ¡D:\eclipse ¡
  • Start ¡Eclipse ¡by ¡double ¡clicking ¡it ¡
  • Set ¡your ¡workspace ¡to ¡be ¡D:\workspace ¡
  • Click ¡Window-­‑> ¡Preferences-­‑> ¡Android, ¡and ¡choose ¡the ¡SDK ¡

loca,on ¡to ¡where ¡you ¡put ¡the ¡Android ¡SDK ¡(must ¡be ¡D: \android-­‑sdk-­‑windows) ¡

Android Workshop (Eddie) Introduction 6

slide-7
SLIDE 7

Get ¡Familiar ¡with ¡Eclipse ¡

Android Workshop (Eddie) Introduction 7

slide-8
SLIDE 8

Get ¡Ready ¡with ¡Your ¡Eclipse ¡

  • As ¡Eclipse ¡is ¡not ¡only ¡designed ¡for ¡Android ¡

development, ¡there ¡might ¡have ¡some ¡slight ¡ difference ¡

– 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 ¡

  • We ¡can ¡divide ¡it ¡into ¡5 ¡components: ¡menu ¡bar, ¡tool ¡

bar, ¡naviga,on ¡area, ¡editor ¡area ¡and ¡debug ¡area ¡ ¡

Android Workshop (Eddie) Introduction 8

slide-9
SLIDE 9

5 ¡Components ¡in ¡Eclipse ¡

Android Workshop (Eddie) Introduction 9

slide-10
SLIDE 10

Menu ¡Bar ¡

  • Eclipse ¡menu ¡bar ¡contains ¡familiar ¡func,ons ¡like ¡file ¡and ¡

edi,ng ¡opera,ons ¡

  • The ¡File ¡menu ¡contains ¡menu ¡items ¡for ¡Import ¡and ¡Export, ¡

which ¡are ¡used ¡to ¡import ¡project ¡files ¡into ¡the ¡Workspace, ¡ and ¡export ¡them ¡out ¡again ¡

  • In ¡the ¡Run ¡menu, ¡you ¡will ¡find ¡commands ¡related ¡to ¡running ¡

and ¡debugging ¡applica,on ¡code, ¡and ¡launching ¡external ¡tools ¡ such ¡as ¡Android ¡emulator ¡

  • In ¡the ¡Help ¡menu, ¡you ¡can ¡search ¡helps ¡and ¡check ¡for ¡updates ¡
  • f ¡Eclipse ¡soIware ¡and ¡plugin ¡by ¡selec,ng ¡Help ¡> ¡Check ¡for ¡

Updates ¡... ¡ ¡

Android Workshop (Eddie) Introduction 10

slide-11
SLIDE 11

Toolbar ¡

Android Workshop (Eddie) Introduction 11

  • One ¡important ¡feature ¡of ¡toolbar ¡is ¡to ¡provide ¡you ¡with ¡a ¡quick ¡access ¡of ¡

Android ¡SDK ¡Manager ¡in ¡the ¡Android ¡SDK ¡and ¡AVD ¡Manager ¡group ¡ ¡

  • If ¡you ¡have ¡installed ¡Android ¡development ¡tools ¡for ¡Eclipse, ¡on ¡the ¡leI ¡side ¡of ¡

the ¡toolbar ¡you ¡should ¡see ¡an ¡Android ¡SDK ¡and ¡AVD ¡manager ¡grouping ¡

  • You ¡can ¡click ¡these ¡buOons ¡to ¡update ¡and ¡install ¡SDK ¡and ¡virtual ¡devices. ¡ ¡
slide-12
SLIDE 12

Naviga,on ¡Area ¡

  • Naviga,on ¡area ¡usually ¡

consists ¡of ¡three ¡views, ¡ Package ¡Explorer, ¡Outline ¡ and ¡Task ¡List ¡if ¡you ¡choose ¡to ¡ use ¡Java ¡perspec,ve ¡

  • Let’s ¡talk ¡about ¡the ¡first ¡two ¡
  • Package ¡Explorer ¡helps ¡to ¡

find ¡all ¡informa,on ¡for ¡the ¡ project, ¡for ¡example, ¡source ¡ code, ¡compile ¡sources, ¡ libraries, ¡manifest, ¡intent-­‑ filters, ¡uses-­‑permissions ¡ ¡

Android Workshop (Eddie) Introduction 12

slide-13
SLIDE 13

Naviga,on ¡Area ¡-­‑ ¡Package ¡Explorer ¡(1/2) ¡

  • The ¡first ¡folder ¡is ¡named ¡by ¡the ¡project ¡name. ¡It ¡includes ¡the ¡6 ¡
  • subfolders. ¡They ¡are ¡src, ¡gen, ¡Android ¡Library, ¡assets, ¡bin ¡and ¡
  • res. ¡ ¡

– “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

slide-14
SLIDE 14

Naviga,on ¡Area ¡-­‑ ¡Package ¡Explorer ¡(2/2) ¡

– “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 ¡

  • constants. ¡ ¡

– 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

slide-15
SLIDE 15

Naviga,on ¡Area ¡-­‑ ¡Outline ¡View ¡

  • The ¡Outline ¡view ¡displays ¡an ¡outline ¡of ¡a ¡structured ¡file ¡that ¡is ¡

currently ¡open ¡in ¡the ¡editor ¡area, ¡and ¡lists ¡structural ¡elements ¡

  • It ¡also ¡provides ¡the ¡descrip,ons ¡of ¡all ¡the ¡small ¡buOons ¡in ¡the ¡

Outline ¡view ¡

Android Workshop (Eddie) Introduction 15

slide-16
SLIDE 16

Editor ¡Area ¡

  • Editor ¡area ¡provides ¡two ¡types ¡of ¡user ¡interface, ¡a ¡

“smart” ¡code ¡sheet ¡and ¡a ¡graphical ¡layout ¡builder. ¡ ¡

  • Smart ¡code ¡sheet ¡is ¡a ¡text ¡editor ¡area ¡for ¡wri,ng ¡the ¡

source ¡code ¡of ¡applica,on ¡

  • Eclipse ¡provides ¡a ¡graphical ¡layout ¡editor ¡to ¡create ¡

the ¡layout ¡of ¡user ¡interface ¡by ¡drag ¡and ¡drop ¡ interface ¡crea,on ¡and ¡live ¡preview ¡for ¡your ¡new ¡ applica,on ¡

Android Workshop (Eddie) Introduction 16

slide-17
SLIDE 17

Editor ¡Area ¡– ¡Smart ¡Code ¡Sheet ¡

  • The ¡“smart” ¡sheet ¡includes ¡many ¡func,ons, ¡for ¡example, ¡

showing ¡hints ¡of ¡arguments ¡ ¡

Android Workshop (Eddie) Introduction 17

slide-18
SLIDE 18

Editor ¡Area ¡– ¡Graphical ¡Layout ¡Editor ¡

  • It ¡will ¡be ¡visible ¡via ¡a ¡tab ¡at ¡the ¡boOom ¡of ¡the ¡code ¡editor ¡

window ¡when ¡you ¡open ¡your ¡xml ¡file ¡(res/layout) ¡ ¡ ¡

Android Workshop (Eddie) Introduction 18

slide-19
SLIDE 19

Debug ¡Area ¡

  • Debug ¡area ¡usually ¡provides ¡informa,on ¡about ¡

warnings, ¡errors, ¡logs ¡and ¡specifica,ons ¡

  • The ¡above ¡is ¡an ¡example ¡of ¡showing ¡Java ¡errors ¡in ¡

the ¡Problems ¡tab ¡

Android Workshop (Eddie) Introduction 19

slide-20
SLIDE 20

Create ¡an ¡Android ¡AVD ¡

  • AVD ¡= ¡Android ¡Virtual ¡

Device ¡(Emulator) ¡

  • Create ¡an ¡Android ¡Virtual ¡

Device ¡(AVD): ¡In ¡Eclipse, ¡ select ¡Window-­‑> ¡Android ¡ SDK ¡and ¡AVD ¡Manager-­‑> ¡ Virtual ¡Devices, ¡and ¡click ¡ New ¡

  • Here ¡is ¡an ¡example ¡to ¡

create ¡a ¡AVD ¡

Android Workshop (Eddie) Introduction 20

slide-21
SLIDE 21

Select ¡Your ¡AVD ¡

  • AIer ¡clicking ¡“Create ¡AVD”, ¡you ¡should ¡see ¡available ¡AVD(s) ¡
  • Select ¡AVD ¡and ¡click ¡“Start ¡…” ¡

Android Workshop (Eddie) Introduction 21

slide-22
SLIDE 22

Run ¡your ¡AVD ¡

  • Run ¡the ¡AVD ¡as ¡below ¡and ¡keep ¡it ¡alive ¡during ¡the ¡lesson ¡
  • Every ¡,me, ¡you ¡may ¡need ¡around ¡a ¡minute ¡to ¡start ¡the ¡emulator ¡

Android Workshop (Eddie) Introduction 22

slide-23
SLIDE 23

Hello, ¡Android! ¡

  • Create ¡a ¡new ¡Android ¡Project. ¡In ¡Eclipse, ¡click ¡File-­‑> ¡New ¡-­‑> ¡

Android ¡project. ¡ ¡

Meaning ¡of ¡the ¡different ¡fields: ¡ – Project ¡Name ¡ ¡

  • This ¡is ¡the ¡Eclipse ¡Project ¡name ¡— ¡the ¡name ¡of ¡the ¡directory ¡that ¡will ¡

contain ¡the ¡project ¡files. ¡Use ¡“helloandroid” ¡

– Build ¡Target ¡

  • The ¡version ¡of ¡Android ¡plaworm ¡you ¡wish ¡your ¡applica,on ¡to ¡run. ¡Since ¡

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 ¡ ¡

  • This ¡is ¡the ¡human-­‑readable ¡,tle ¡for ¡your ¡applica,on ¡— ¡the ¡name ¡that ¡will ¡

appear ¡on ¡the ¡Android ¡device. ¡Use ¡Hello ¡Android. ¡

Android Workshop (Eddie) Introduction 23

slide-24
SLIDE 24

Hello, ¡Android! ¡

– Package ¡Name ¡ ¡

  • This ¡is ¡the ¡package ¡namespace ¡(following ¡the ¡same ¡rules ¡as ¡for ¡packages ¡in ¡the ¡Java ¡

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. ¡ ¡

  • Your ¡package ¡name ¡must ¡be ¡unique ¡across ¡all ¡packages ¡installed ¡on ¡the ¡Android ¡system; ¡

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 ¡ ¡

  • This ¡is ¡the ¡name ¡for ¡the ¡class ¡stub ¡that ¡will ¡be ¡generated ¡by ¡the ¡plugin. ¡This ¡will ¡be ¡a ¡

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 ¡

  • p,onal, ¡but ¡an ¡Ac,vity ¡is ¡almost ¡always ¡used ¡as ¡the ¡basis ¡for ¡an ¡applica,on. ¡Here ¡we ¡

use ¡HelloAndroid. ¡

– Min ¡SDK ¡Version ¡ ¡

  • This ¡specifies ¡the ¡minimum ¡API ¡Level ¡on ¡which ¡your ¡applica,on ¡can ¡run. ¡By ¡default ¡this ¡is ¡

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

slide-25
SLIDE 25

Hello, ¡Android! ¡

  • Now ¡your ¡Android ¡project ¡is ¡ready. ¡In ¡Package ¡Explorer, ¡click ¡

src-­‑> ¡hkust.cse.HelloAndroid. ¡Open ¡HelloAndroid.java ¡

– 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

slide-26
SLIDE 26

Hello, ¡Android! ¡

  • Construct ¡the ¡UI. ¡Use ¡following ¡code ¡to ¡replace ¡the ¡default ¡code ¡of ¡
  • nCreate(). ¡

¡ ¡ ¡public ¡void ¡onCreate(Bundle ¡savedInstanceState) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡super.onCreate(savedInstanceState); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡TextView ¡tv ¡= ¡new ¡TextView(this); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tv.setText("Hello, ¡Android"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡setContentView(tv); ¡ ¡ ¡ ¡} ¡

  • Since ¡the ¡class ¡TextView ¡is ¡not ¡accepted ¡by ¡default, ¡you ¡should ¡click ¡
  • n ¡it ¡and ¡select ¡Import ¡‘TextView’ ¡(android.widget). ¡ ¡

– 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

slide-27
SLIDE 27

Hello, ¡Android! ¡

  • An ¡Android ¡user ¡interface ¡is ¡composed ¡of ¡hierarchies ¡of ¡objects ¡called ¡Views. ¡

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 ¡

  • f ¡the ¡View ¡class ¡and ¡the ¡subclass ¡that ¡handles ¡text ¡is ¡TextView. ¡
  • In ¡this ¡change, ¡you ¡create ¡a ¡TextView ¡with ¡the ¡class ¡constructor, ¡which ¡

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 ¡

  • Context. ¡So, ¡you ¡can ¡pass ¡this ¡as ¡your ¡Context ¡reference ¡to ¡the ¡TextView. ¡
  • Next, ¡you ¡define ¡the ¡text ¡content ¡with ¡setText(). ¡
  • Finally, ¡you ¡pass ¡the ¡TextView ¡to ¡setContentView() ¡in ¡order ¡to ¡display ¡it ¡as ¡the ¡

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. ¡

  • There ¡it ¡is ¡— ¡"Hello, ¡Android!" ¡in ¡Android! ¡The ¡next ¡step, ¡of ¡course, ¡is ¡to ¡see ¡it ¡
  • running. ¡ ¡

Android Workshop (Eddie) Introduction 27

slide-28
SLIDE 28

Hello, ¡Android! ¡

  • Run ¡the ¡applica,on. ¡Click ¡Run-­‑> ¡Run, ¡and ¡select ¡Android ¡

Applica[on. ¡Eclipse ¡will ¡build ¡the ¡whole ¡project ¡and ¡deploy ¡it ¡ to ¡an ¡emulator ¡automa,cally. ¡You ¡can ¡find ¡your ¡applica,on ¡in ¡

  • Menu. ¡
  • Debug ¡your ¡project. ¡Put ¡a ¡breakpoint ¡for ¡your ¡applica,on ¡by ¡

double-­‑clicking ¡on ¡the ¡marker ¡bar ¡next ¡to ¡the ¡source ¡code ¡

  • line. ¡ ¡

– 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

slide-29
SLIDE 29

Hello, ¡Android! ¡

  • Upgrade ¡the ¡UI ¡to ¡an ¡XML ¡Layout. ¡This ¡is ¡an ¡easier ¡way ¡to ¡

apply ¡your ¡modifica,on ¡to ¡the ¡UI ¡to ¡different ¡applica,ons. ¡

– In ¡the ¡Eclipse ¡Package ¡Explorer, ¡select ¡/res/layout/main.xml ¡

  • This ¡xml ¡layout ¡file ¡can ¡be ¡used ¡by ¡the ¡applica,on ¡to ¡construct ¡user ¡

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

slide-30
SLIDE 30

Hello, ¡Android! ¡

  • Now ¡modify ¡your ¡HelloWorld.java ¡file. ¡Replace ¡the ¡content ¡of ¡
  • nCreate() ¡with ¡following ¡code: ¡

¡ ¡ ¡ ¡public ¡void ¡onCreate(Bundle ¡savedInstanceState) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡super.onCreate(savedInstanceState); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡setContentView(R.layout.main); ¡ ¡ ¡ ¡ ¡} ¡

  • Compared ¡to ¡the ¡previous ¡code, ¡the ¡impor,ng ¡of ¡

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. ¡

  • Now ¡you ¡can ¡run ¡the ¡applica,on ¡again, ¡and ¡see ¡that ¡the ¡,tle ¡of ¡the ¡

applica,on ¡and ¡the ¡text ¡has ¡been ¡changed. ¡ ¡

Android Workshop (Eddie) Introduction 30

slide-31
SLIDE 31

Hello, ¡Android! ¡

  • Open ¡/res/values/strings.xml ¡

– you ¡can ¡see ¡the ¡values ¡of ¡the ¡two ¡strings: ¡hello ¡and ¡app_name ¡defined ¡

  • there. ¡
  • Replace ¡the ¡string ¡value ¡of ¡hello ¡with ¡“Hello, ¡<Your ¡Neighbor’s ¡

name>!” ¡

  • Now ¡you ¡can ¡run ¡the ¡applica,on ¡again, ¡and ¡see ¡that ¡the ¡

applica,on ¡now ¡prints ¡the ¡new ¡hello ¡string! ¡

Android Workshop (Eddie) Introduction 31