Minicurso Android

1,410 views
1,311 views

Published on

Android Mini Curso Hands-on.

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,410
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
34
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Minicurso Android

  1. 1. Android - 4h 100% Hands-on
  2. 2. GOOGLE ANdroid Mario Jorge Pereira
  3. 3. 14 20 13 20 12 20 11 20 10 20 09 20 08 20 07 20 06 20 05 20 04 20 03 20 02 20
  4. 4. Agenda Setup (Ambiente e AVD’s) Development Alo Mundo (Hello World) Lista Tarefas (ToDo List) (Listar, Inserir, Excluir, Preferencias …)
  5. 5. Desenvolvimento
  6. 6. http://developer.android.com Por onde começar?
  7. 7. ADT Bundle: • Eclipse + ADT plugin • Android SDK Tools • Android Platform-tools Pré requisito: JDK instalada
  8. 8. Eclipse + ADT
  9. 9. Android Virtual Device Manager
  10. 10. Android Virtual Device AVD Emulador
  11. 11. Alô Mundo File > New > Android Application Project
  12. 12. Run As.. Android application
  13. 13. src/ 
 (código fonte: .java)" gen/
 (código gerado: R.java )" bin/
 (bytecodes: .dex)" libs/ 
 (bibliotecas)" Es tr ut ur a res/ 
 (recursos: layouts, strigs, icones …)" assets/ 
 (outros recursos: arquivos em geral)
  14. 14. " rsion="1.0 <?xml ve esources> <r "utf-8"?> encoding= /string> Alo Mundo< > s</string> "app_name" s "> S e t t i n g ring name= g <st ion_settin ct </string> a llo world! ing name=" e <str o _ w o r l d "> H name="hell <string ! ! /resources < > strings.xml
  15. 15. /* AUT ED FILE. O-GENERAT D I F Y .* / DO NOT MO class R { al { public fin class attr al static fin public 0; =0x7f04000 imen { } s d rgin final clas izontal_ma tic 01; _hor public sta n= 0 x 7 f 0 4 0 0 t activity argi l in vertical_m tatic fina ty_ public s int activi inal c static f publi ; } s layout { 0x7f030000 al clas fin a i n= activity_m lic static pub int tic final public sta } } ... R.java
  16. 16. MainActivity.java ! ! } public cla ss MainAct ivity exte ! nds Activi ty { @Override protected void onCre ate(Bundle super.onCr savedInsta eate(saved nceState) InstanceSt { setContent ate); View(R.lay o u t .a c t i v i } t y _ m a i n) ; @Override public boo lean onCre ateOptions getMenuInf Menu(Menu later().in menu) { flate(R.me return tru n u .m a i n , m e; enu); }
  17. 17. AndroidManifest.xml utfencoding=“ sion="1.0" 8"?> <?xml ver . . .> <manifest ... . > ication .. i t y " <appl .MainActiv mundo xample.alo <activity com.e : n a m e=" b r . " > android g/app_name strin d : l a b e l= " @ " /> androi ction.MAIN t e r> /> tent.a intent-fil LAUNCHER" android.in < m e= " gory. android:na ntent.cate roid.i <action : n a m e= " a n d droid ategory an <c l t e r> /intent-fi < > </activity i o n> </applicat > </manifest
  18. 18. ToDo br.mobileday.todo File > New > Android Application Project
  19. 19. Log.[n NSAGEM”); e l ] (“ T A G ”, “ M E iv ! (Verbose) Log.v (Debug) Log.d (Info) Log.i (Warning) Log.w e (Erro) Log. ( .wtf ...) Log Log
  20. 20. yout ... > elativeLa <R ... <Button d / b o t a o " i d : i d= " @ + i andro _ c o n t e n t " i d t h= " w r a p d:layout_w androi p_content" e i g h t= " w r a d:layout_h t V i e w 1 " roi x and w= " @ + i d / t e ayout_belo oid:l l= " t r u e " andr rHorizonta yout_cente android:la "Botao" r o i d : t e x t= and etodo" /> o n C l i c k= " m android: ... L a y o u t> </Relative activity_main.xml
  21. 21. MainActivity.java public cla ss MainAct ivi ! ty extends Activity { @Override protected void onCre ate(Bundle super.onCr savedInsta eate(saved nceState) InstanceSt { setContent ate); View(R.lay o u t .a c t i v i } t y _ m a i n) ; ... public voi d metodo(V iew v){ Log.i(“T AG”,"TEXTO "); } }
  22. 22. Run As.. Android application
  23. 23. TOAST .show a s t .L E N G T H _ L O N G) , To x t (t h i s , " T E X T O " t.makeTe Toas ! ! G Toast.LENGTH_LON ! st.LENGTH_SHORT Toa ! ();
  24. 24. MainActivity.java public cla ss MainAct ivity exte ! nds Activi ty { @Override protected void onCre ate(Bundle super.onCr savedInsta eate(saved nceState) InstanceSt { setContent ate); View(R.lay o u t .a c t i v i } t y _ m a i n) ; ... public voi d metodo(V iew v){ Toast.ma keText(thi s, "ToDo!" } , Toast.LE N G T H _ L O N G) } .show();
  25. 25. Run As.. Android application
  26. 26. findbyid y I d ( R . i d .[ i d ] ) ; B [Tipo]) findView [Tipo] var = ( ! v a r . m e t o d o ( ) ; !
  27. 27. MainActivity.java public cla ss MainAct ivity exte ! nds Activi ty { @Override protected void onCre ate(Bundle super.onCr savedInsta eate(saved nceState) InstanceSt { setContent ate); View(R.lay o u t .a c t i v i } t y _ m a i n) ; ... public voi d metodo(V iew v){ TextVie w tv = (Te xtView) fi tv.setTex ndViewById t( "ToDO!") (R.id.text ; } View1); }
  28. 28. Run As.. Android application
  29. 29. NovaActivity.java public cla ss NovaAct ivity ! extends Ac tivity { @Overrid e protecte d void onC reate(Bund super. le savedIn onCreate(s stanceStat avedInstan e) { setCon ceState); tentView(R .layout.ac } tivity_nov a) ; ! @Overrid e public b oolean onC reateOptio getMen nsMenu(Men uInflater( u menu) { ).inflate( return R.menu.nov true; a, menu); } ! }
  30. 30. AndroidManifest.xml “utf-8"?> encoding= sion="1.0" <?xml ver f e s t . . .> <mani ... tion ... > <applica ... A c t i v i t y " va tivity ay.todo.No ed <ac ="br.mobil me ty_nova" > vi android:na title_acti / l= " @ s t r i n g be android:la > </activity > pplication </a /manifest> <
  31. 31. MainActivity.java public cla ss MainAct ivity exte ! nds Activi ty { @Override protected void onCre ate(Bundle super.onCr savedInsta eate(saved nceState) InstanceSt { setContent ate); View(R.lay o u t .a c t i v i } t y _ m a i n) ; ... public voi d metodo(V iew v){ ntent in I tent = new Intent(thi ntent.pu i s,NovaActi tExtra("ch v i t y .c l a s s ave", "val ); tartActi s or"); vity(inten t); } }
  32. 32. yout ... > elativeLa <R ... <Button d / b o t a o " i d : i d= " @ + i andro _ c o n t e n t " i d t h= " w r a p d:layout_w androi p_content" e i g h t= " w r a d:layout_h t V i e w 1 " roi x and w= " @ + i d / t e ayout_belo oid:l l= " t r u e " andr rHorizonta yout_cente android:la "Voltar" r o i d : t e x t= and oltar" /> o n C l i c k= " v android: ... L a y o u t> </Relative activity_nova.xml.java
  33. 33. NovaActivity.java public cla ss NovaAct ivity exte ! nds Activi ty { @Override protected void onCre ate(Bundle super.onCr savedInsta eate(saved nceState) set InstanceSt { ContentVie ate); w(R.layout Bun . activity_ dle extas n o v a) ; = getInten Tex t().getExt tView tv = ras(); (TextView) tv. findViewBy setText(ex Id(R.id.te tas.getStr xtView1); } ing("chave ")); ... public voi d voltar(V iew v){ fin ish(); } }
  34. 34. Run As.. Android application
  35. 35. > t 1 " ayout ... lativeL id/editTex <Re "@+ n d r o i d : i d= t " a fill_paren <EditText t h= " t " layout_wid android: rap_conten t= " w yout_heigh e" android:la t L e f t= " t r u ren ut_alignPa " o i g h t= " t r u e ndroid:lay a entR t_alignPar you " android:la t T o p= " t r u e aren out_alignP y android:la f a " "Nome Tare n t= android:hi xt" u t T y p e= " t e p android:in > d : e m s= " 1 0 " androi /> uestFocus <req > " </EditText id/button1 ="@+ t " android:id rap_conten Button < h= " w ayout_widt t " android:l rap_conten ="w out_height " y /editText1 android:la @+id u t _ b e l o w= " t a l= " t r u e " droid:layo an izon _centerHor yout android:la r" i c k= " s a l v a Cl android:on Salvar" /> x t=" android:te > tiveLayout </Rela activity_nova.xml
  36. 36. public cla ss NovaAct ivity ! ! private Ed itText tex t; NovaActivity.java extends Ac tivity { @Overri de protect ed void on Create(Bun supe dle savedI r.onCreate nstanceSta (savedInst te) { setC anceState) ontentView ; (R.layout. text activity_n = (EditTex o v a) ; t) findVie } wById(R.id .e d i t T e x t 1 ); ! @Overri de public boolean on CreateOpti getM onsMenu(Me enuInflate nu menu) { r().inflat retu e(R.menu.n rn true; ova, menu) ; } ! } public voi d salvar(V iew v){ Intent d ados = new Intent(); dados.putE xtra("nome ", text.ge setResul tText().to t( RESULT_O String()); K, d a d o s ) ; finish() ; }
  37. 37. MainActivity.java public cla ss MainAct ivity exte nds Activi ty { ... ! ! } public voi d metodo(V iew v){ Intent i ntent = ne w Intent(t startAct his,NovaAc ivityForRe t i v i t y .c l a sult(inten ss); } t,9999); @Override protected void onAct ivityResul if(reque t (i n t r e q u stCode == estCode, i 9999 && re nt resultC oast.make T sultCode = ode, Inten Text(this, = RESULT_O t data) { data.getSt K){ } ringExtra( "nome"), T } oast.LENGT H _ L O N G) . s h ow();
  38. 38. Run As.. Android application
  39. 39. ut ... > ativeLayo <Rel ... <ListView s t V i e w 1 " i i d=" @ + i d / l ndroid: h _ p a r e n t " a c w i d t h= " m a t id:layout_ ro c o n t e n t " _ and i g h t= " w r a p :layout_he oid t o m= " t r u e " andr nParentBot ayout_alig oid:l t= " t r u e " f andr gnParentLe layout_ali id: t= " t r u e " andro ParentRigh yout_align id:la "true" > = andro nParentTop ayout_alig android:l ListView> </ ... L a y o u t> </Relative activity_main.xml
  40. 40. tp://schem d r o i d= " h t as com/apk/re .android. s/ n nu xmlns:a <me android" > ! " <item ction_nova /a d:id="@+id androi o r y= " 1 0 0 " eg orderInCat android: n= " n e v e r " io :showAsAct > android a T a r e f a "/ v : t i t l e= " N o android ! </menu> main.xml
  41. 41. MainActivity.java ! public cla ss MainAct ivity exte private nds Activi ListView l ty { istView; @Override protected void onCre ate(Bundle super.onCr savedInsta eate(saved nceState) InstanceSt { setContent ate); View(R.lay o u t .a c t i v i listView = t y _ m a i n) ; (ListView) findViewBy String[] v I d ( R . i d .l i alues = ne s t V i e w 1) ; w String[] ArrayAdapt {"Acorda", er<String> "MobileDay adapter = andro ", " H a p p y H new ArrayA o u r "} ; id.R.layou dapter<Str t.simple_l listView.s i n g > (t h i s , ist_item_1 etAdapter( ,values); adapter); } public boo lean onOpt ionsItemSe switch lected(Men (item.getI uItem item temId()) { ) { case R.id. action_nov a: Int ent intent = new Inte sta nt(this, N rtActivity ovaActivit ForResult( ret y .c l a s s ) ; intent, 99 urn true; 99); default: return sup er.onOptio } nsItemSele cted(item) } ; ... }
  42. 42. Run As.. Android application
  43. 43. MainActivity.java public cla ss MainAct ivity exte private nds Activi ListView l ty { istView; private static Arr ayList<Str private ing> value ArrayAdapt s = new Ar er<String> ! rayList<St adapter; ring>(); @Overri de protect ed void on Create(Bun supe dle savedI r.onCreate nstanceSta (savedInst te) { setC anceState) ontentView ; (R.layout. list activity_m View = (Li a i n) ; stView) fi adap ndViewById ter = new ( R . i d .l i s t ArrayAdapt View1); er<String> (t h i s , andr list oid.R.layo View.setAd ut.simple_ a p t e r (a d a p } list_item_ ter); 1, v a l u e s ) ; ! } @Override protected void onRes ume() { super.o nResume(); adapter .notifyDat aSetChange } d(); ...
  44. 44. MainActivity.java public cla ss MainAct ivity exte ... nds Activi t y { @Overrid e protecte d void onA ctivityRes resultCode u l t (i n t r e , Intent d questCode, ata) { if (re int questCode == 9999 && Str resultCode ing tarefa == RESULT_ = data.get val OK) { StringExtr ues.add(ta a("nome"); refa); } } ! }
  45. 45. Run As.. Android application
  46. 46. MainActivity.java public cla ss MainAct ivity exte ... nds Activi t y { private SharedPref erences se @Overri ttings; de protect ed void on Create(Bun ... dle savedI nstanceSta te) { sett ings = Pr eferenceMa Edit nager.getD or editor efaultShar = settings edPreferen edit .edit(); ces(this); or.putStri ng("ultimo edit ", ""); or.commit( ); } ... @Overri de protect ed void on ActivityRe if ( s u l t (i n t r requestCod equestCode e == 9999 , int resu && resultC ltCode, In String tar ode == RES tent data) efa = data ULT_OK) { { .getString values.add Extra("nom (tarefa); e"); Editor edi t o r = sett ings.edit( editor.put ); String("ul timo", tar editor.com efa); mit(); } } }
  47. 47. NovaActivity.java public cla ss NovaAct ivity ! ! private Ed itText tex t; extends Ac tivity { protect ed void on Create(Bun super dle savedI .onCreate( nstanceSta savedInsta te) { setCo nceState); ntentView( R.layout.a text ctivity_no = (EditTex v a) ; t) findVie Share wById(R.id dPreferenc .e d i t T e x t 1 es setting ); s = Prefe renceManag Strin er.getDefa g valor = ultSharedP settings.g references text. etString(" (this); setText(va ultimo", " lor); Tarefa"); } ... }
  48. 48. Run As.. Android application
  49. 49. mlns:andr <menu x ndroid" > res/a ! ://schem o i d= " h t t p d.com/apk/ as.androi <item / e x i b i r " d : i d= " @ + i d androi g o r y= " 1 0 0 " rderInCate android:o ="never" owAsAction android:sh Exibir"/> i d : t i t l e= " andro ! </menu> context_menu.xml
  50. 50. MainActivity.java public cla ss MainAct ivity exte ... nds Activi t y { @Overrid e protecte d void onC reate(Bund ... le savedIn stanceStat e ) { reg isterForCo ntextMenu( } listView); ! @Overrid e public v oid onCrea teContextM ContextMen enu(Contex uInfo menu tMenu menu Info) { , View v, super. onCreateCo ntextMenu( MenuIn menu, v, m flater inf enuInfo); later = ge inflat tMenuInfla er.inflate ter(); (R.menu.co } ntext_menu , menu); ... }
  51. 51. MainActivity.java public cla ss MainAct ivity exte ... nds Activi t y { public b oolean onC ontextItem AdapterCon Selected(M textMenuIn enuItem it fo info = item.getMe em) { (AdapterCo nuInfo(); ntextMenuI switch (it nfo) em.getItem Id()) { case R.id. action_set t i n g s: String val or = (Stri listView.g ng) etItemAtPo sition(inf o .p o s i t i o n Toast.make ); Text(this, valor, Toa return tru s t .L E N G T H _ e; L O N G) . s h o w default: (); return sup er.onConte xtItemSele } cted(item) ; } ... }
  52. 52. Run As.. Android application
  53. 53. xmlns:andr <menu ndroid" > a ! ://schemas o i d= " h t t p .a m/apk/res/ ndroid.co <item id/exibir" @+ droid:id=" an g o r y= " 1 0 0 " te :orderInCa android o n= " n e v e r " i :showAsAct android “Exibir"/> = roid:title and <item / e x c l u i r " id oid:id="@+ andr r y= " 1 0 0 " go rderInCate " android:o i o n= " n e v e r ct id:showAsA andro xcluir"/> E i d : t i t l e= " andro ! </menu> context_menu.xml
  54. 54. MainActivity.java public cla ss MainAct ivity exte ... nds Activi t y { public boolean on ContextIte mSelected( AdapterCon MenuItem i textMenuIn tem) { item.getMe fo info = nuInfo(); (AdapterCo ntextMenuI nfo) switch (it em.getItem Id()) { case R.i d.exibir: String valor = (S tring) lis tView.getI Toast.make temAtPosit Text(this, i o n ( i n f o .p valor, Toa return tru osition); s t .L E N G T H _ e; L O N G) . s h o w case R.id. (); excluir: v a l u e s . r e move(info. position); a d a p t e r . n otifyDataS etChanged( return tru ); e; default: return sup er.onConte } xtItemSele cted(item) ... ; }
  55. 55. Run As.. Android application
  56. 56. r o i d= " h t t p xmlns:and s.android. ://schema com android" > /apk/res/ <menu ! <item tion_nova" /ac d : i d=" @ + i d androi g o r y= " 1 0 0 " ate d:orderInC androi n= " n e v e r " tio d:showAsAc androi T a r e f a "/ > ova d : t i t l e= " N androi <item @+id/site" =" android:id g o r y= " 1 0 0 " ate d:orderInC androi o n= " n e v e r " ti d:showAsAc androi e= " S i t e "/ > tl android:ti </menu> menu.xml
  57. 57. MainActivity.java public cla ss MainAct ivity exte ... nds Activi t y { public boolean on OptionsIte switc mSelected( h (item.ge MenuItem i tItemId()) tem) { case { R.id.actio n _ n o v a: no va(); re turn true; case R.id. site: Intent i ntent = new Inte nt(Intent. "http://ww ACTION_VIE w.google.c W,Uri.pars o m ") ) ; e ( startAct ivity(inte nt); return t rue; defau lt: re turn super .onOptions } ItemSelect ed(item); } ... }
  58. 58. Run As.. Android application
  59. 59. Esta obra está licenciada sob a licença Creative Commons Atribuição-CompartilhaIgual 3.0 Não Adaptada. Para ver uma cópia desta licença, visite http://creativecommons.org/licenses/by-sa/3.0/.
  60. 60. ANDROID Mario Jorge Pereira Como me encontrar? http://www.mariojp.com.br twitter.com/@mariojp mariojp@gmail.com

×