You are on page 1of 121

htarhtarlwin.blogspot.

com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oc oqtc5oco _o _oc oootoco5o Android application oooogto5too beginner ooo g_ go o5_ocooo
Tut orial ooo5o _t ogto5to 5_oc o oo ootc o ogooo ooo5o5 reference g5oooogo5tgq _ocooo
o o o. o 5to cto o5too` _o_ooo5oo5c o_ oooq oO5o _oo o5to ootc5tc 5 o o o5 _oc ooo
Android Application ogtq_t (o)
co5qoocO5t_ot o5oooco5tooo5cgogooo
(o) j dk1.7.0 ooo5oo
ht t ps: / / dl.dropbox.com/ u/ 94729309/ j dk1.7.0.rar
_o tgc inst all o o O5to
(_) Eclipse ooo5oo
ht t p: / / eclipse.st u.edu.t w/ t echnology/ epp/ downloads/ release/ j uno/ R/ eclipse-mobile-j uno-win32.zip
eclipse-mobile-j uno-win32.zip o unzip oo_ot eclipse ooooo D drive o5oo5O5to (ooo5ooO5togoooo
D drive o5o o 5O5t_oc to5 oo5c to oo Ooc o o D drive og o o5t _o _o o oqg5o 5O5to)
(g) Android SDK o o o 5o o
ht t p: / / dl.google.com/ android/ inst aller_r20.0.1-windows.exe
installer_r20.0.1-windows.exe o run o Next ooo o5a o o5to
I nst all j ust for me o ogt_o t Next
Dest inat ion floder o 5 o o applicat ion ooo o too oqg5o Browse ooqogtooto
I nst all o click o
Next
oocooo5 Finish oao o
(g) Tools q Android 3.0 o check o o _o t I nst all 5 packages o ogto
(4.2 Ogoqo_o ocoqtc5ooo 3.0 ooto5o 3.0 oogtocto5o)
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
(_) eclipse o oOo eclipse o o _o5og5c o tooto double click o o o o o
(o) Workspace o 5 o o ogtoo application ooo oo o 5o o too o o5 ogtoot_o t OK o click o
(g) ooco Android Developer Toos (ADT) plug-in oO_gooo (ADT plug-in o download oO5t_oto offline install
oogco_tgooo)
Help> I nst all New Soft ware>
Add o click o
Ooooctogt_ot Ok ooo
Oo oo c togt_o t Next o o o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Connect ion oo`o o_ _o t o q _o5oo oo_
_oto5tgcoo5 oooqo_o5o5 android application ogto soft ware octocg5_o_oc_ococoo5t_ototo_o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Application (o) o ht arht arlwin.blogspot .com o5_o_o
Android Applicat ion ogtq_ t (_)
Hello world! o oo`oo Applicat ion oc o ogt_o_ go5c
eclipse o open o o o
o5o oo oo`o5oo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Proj ect creat e ooooo
File> New> Android Applicat ion Proj ect
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Oo oo c t _o_ c o _o t Next o click o Minimum Required SDK o 2.2 O5to gc o applicat ion o
Android version 2.2 qOo o 5 install o o a c ooo application ogto oo too sdk version ooo5 3.0
oooooooooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Browse ooc_otooooco_o_oogtooacooo ( applicat ion logo o o o o q_oc oc gc oo ooogcoo5
ooct Next o5a o o)
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Oo o o c t_o_ co _o t Next
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oOoot_oocooo5ogc Oo oo o c too`o5oo I nst all/ Upgrade o click o
( co5qoocO5tgooo ooqooto5oo5 inst all o o _o tgc _oq o o o go_o )
Accept All o ogt_o t I nst all o click o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Oo oo o c t proj ect ocoo creat e oo_ot_ocooooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
(1) oqt ( Android version 2.2 a c Oo) o5 USB debugging o check ooO5to
(2) Phone o o qo_o5q USB Cable o t_o to o o
(3) Run> Run oaoo
(4) o5oooct o o g device o ogt_o t OK ooo
(5) Phone o 5_o_ o o o Hello World! o _o tooooo
(6) oqt menu o 5_o_ o Droid1 o o applicat ion oco oo oo oo t ouch ooo
o qoo oo Hello world! o o5oooo _o5t applicat ion o5to o uninst all
ooooooaco_ o o _o_ o (Set t ings> Applicat ions> Manage applicat ions
oo5tooo)
apk file ootooo o 5g o 5o
bin o oc o Droid1.apk o o ocootoooo oo _o5to q tOO_ _o t inst all o o _o_ o
o co oc tooo o _o t_o5to ocogtO5to sof t ware ooto _o t ( ocoo_oo5go_o5o o_o5ooooo)
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
o_ oo data ooooto o o _oqg5ooo 5 ooco_o_oootacooo ococ_otococo5too
oooo5 select _ocoqo but ton ooo click gooooO5to ( oa o goo oo _to 5o toq ogtootooo )
Android Applicat ion ogtq_t (g)
Run on Emulator
o_oo_o5_ooo Android application ogtq_tooo5ooo5ocooo android device ( o q t/ t ablet / not e)
o og o5oo o o o oo ogo_ocooo oqo_o5oo5ggoo
o o o o 5oo o ( emulator) o 5 run _o_ oooo
oc oq tc5 ( _) o 5o o o o proj ect o real device o 5 o run o emulat or o 5 run _o_ go5c emulat or
oc oo o O5t_o tgc run o c to to go_o
ooo Android Virt ual Device Manager oot_ooo
eclipse ooc_ot t ool bar oo`o Android Virt ual Device Manager oo t ool o g 5_o t click o
o5ooooooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Ooooct_o_coo ( _ooq5ggc RAM 1024 o 512 or 256 or 128 O5t_o_ o)
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
HHl o ogt_o t St art o click o o o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
_otgc o5oooqtooot oogoo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
eclipse oo5t_ot oc oq tc5 ( _) o 5o o o o applicat ion o Run o
Hello world! o o c5oqtooto emulat or o 5oooo o oo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion ogtq_t (g)
o oc ooo5 ogtq_ t ( _) o 5 creat e o o o o Droid1 g coding o c to q_ tq_ to_o5c tooo o applicat ion o
run oogc Hello World! ooo`oooooo5t oocooo5 ooco_o_oc5o5too`o5o5c o o _ooo
Eclipse o oc o
oooo_ooto5go Package Explorer ooq Droid1> src> com.example.droid1> DroidAct ivit y.j ava o oc o
package com.example.droid1;
import android.os.Bundle;
import android.app.Act ivit y;
import android.view.Menu;
import android.widget .Text View; / / * * * * *
public class DroidAct ivity ext ends Activit y {
@Override
public void onCreat e(Bundle savedI nst anceSt ate) {
super.onCreat e(savedI nst anceSt at e);
Text View t v = new Text View(this) ; / / * * * * *
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
t v.set Text ( "Hello! I 'm Dr. Ht ar Ht ar Lwin" ) ; / / * * * * *
set Cont ent View(t v); / / * * * * *
}
@Override
public boolean onCreat eOpt ionsMenu( Menu menu) {
get MenuI nf later( ) .inf late(R.menu.main, menu) ;
return true;
}
}
/ / * * * * * _oO5to oqg5ooo OoO_f_ococooo
_o tgc oo5 phone q o o _o too5 o run oo5t. Emulat or q run oo5t _o _o g5q run _o_ o
Android Applicat ion ogtq_ t (_)
Discussion
oocoqtc5ooo t ut orial ooo5o_togtoo5o oooooooo5cgoooctooo_tc5tootogto ooo
oocooo5 ogo ocoqtc5ooo_oq_ot gctocto5to5_ocooo
oooooocqtoococo Android SDK ogcooctooogo5_ocooo
ooc to o o_o tgc inst all oogooo oo o 5 inst all oooooo5ogtootgooo
oo5ctoooocoooo5 D: drive o5oo5O5to5_ocooo _oo5 D: \ sdk oo sdk
oooo_t_o_og5o_ootogooo D drive oggco_t_o_og5oqg5o5O5tacooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oo5_oO5tooct St art SDK Manager o check o o O5t_o t lat est SDK t ools ooq platform ooo
download oacooo ooooc o plat form ooto5 check o o _o t I nst all * packages o click o o g oo ( *
= number of packages)
Android 4.2 o download oO5ta cgcoo5ctooo ( c o5qo o c _o to o go 5o)
o ooootot oq5cooooooo_t sdk f older (Android SDK o install o o o o oqg5) Oo SDK Manager
oot_oto ogooo
oq5oocoo eclipse o download o_o t unzip oooo
Eclipse ooto _o5t I DE oto_ tgooo ooooo oocoqtc5ooo5 eclipse o oo t_o_o5to 5o
Eclipse o 5 android applicat ion ooogta c o ADT Plugin o inst all o o ootgo_ too
ooo5totoc_ocgcoo5 cocogo_o
Proj ect ocoo creat e oooo eclipse oot_o_oo command line tools oo_tot_o_ogooo
o o 5oo5 eclipse oo5ot_o_o5to5_ocooo
oo creat e ooO5to applicat ion o run ooo5o_t o o c to q too`o 5 run acoo emulat or oo`o5o_t
run a c ooo q_toot _ ootooo5_o_oto5to_o_og5q_tootacooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion ogtq_t (o)
(oo SDK 2.2 qoo O5to5o ooo 3.0 qooo5ogc_ocgo5q_tq_ tooqo5ootoogoo
ooooooco_oooo)
oocooo5 User I nt erface ocooo_o_ go5c
(o) Tool bar oo`o creat e a new Android proj ect o o t ool o click o o o o5o oo o c t_o_ _o t Next o
click o o o
oocooo5 oo_o_og5oootocooo5oogtootooo ( oogtoc o_ tgooo ) _o tgc Next o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Ooo o5_oO5too MyApp2> res> layout> act ivity_main.xml o oc _o t o5o o code oo copy
oo_otO_ooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
< ?xml version= "1.0" encoding= "ut f-8"?>
< LinearLayout xmlns: android= "ht t p: / / schemas.android.com/ apk/ res/ android"
xmlns: t ools= "ht t p: / / schemas.android.com/ t ools"
android: layout _widt h= "match_parent "
android: layout_height= "match_parent "
android: orient ation= "horizontal">
< Edit Text android: id= "@+ id/ edit _message"
android: layout_weight = "1"
android: layout _widt h= "0dp"
android: layout_height= "wrap_cont ent "
android: hint = "@string/ edit _message" / >
< But t on
android: layout _widt h= "wrap_cont ent "
android: layout_height= "wrap_cont ent "
android: t ext = "@st ring/ but t on_send" / >
< / LinearLayout >
oq5o oc o MyApp2> values> st rings.xml o oc _o to5o o code ooO_ o o o
< ?xml version= "1.0" encoding= "ut f -8"?>
< resources>
< st ring name= "app_name"> My First App< / st ring>
< string name= "edit _message"> Enter a message< / st ring>
< string name= "but t on_send"> Send< / string>
< st ring name= "menu_set t ings"> Sett ings< / st ring>
< st ring name= "t it le_act ivity_main"> MainAct ivity< / st ring>
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
< / resources>
Run _o_ o o oc o emulat or q run O5too_oooo o5ooooct Text box ocoq But t on
ocooooooooo o q tqo_ to o _o t run acooo bin o5oo apk ocoo_oto_toqtOO__ot
inst all ooacooo ( coding octo oq5oo(c)o 5g c t_oooo )
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion ogtq_ t (g)
oc oq tc5 (o) g coding o c to q_ tco g c t_oo 5_oc ooo
oocoqtc5o t ext box ocoq but t on ocoO_o5o
gcotooO_oo text box q but t on oo layout o_ococgooo
For layout
< ?xml version= "1.0" encoding= "ut f-8"?>
< LinearLayout xmlns: android= "ht t p: / / schemas.android.com/ apk/ res/ android"
xmlns: t ools= "ht t p: / / schemas.android.com/ t ools"
android: layout _widt h= "match_parent "
android: layout_height= "match_parent "
screen o5gqcooo5o widt h and height oooooo oo5o
android: orient ation= "horizontal"
t ext box q but ton o o5to o oqg5ooo o oo o o o5o
oo Text box o o o
< Edit Text android: id= "@+ id/ edit _message"
android: layout_weight = "1"
android: layout _widt h= "0dp"
android: layout_height= "wrap_cont ent "
t extbox o5goO_oooc5gooo5oocto5_ococoo width and height oooooo5o
android: hint = "@string/ edit _message" / >
oo but t on o o o
< But t on
android: layout _widt h= "wrap_cont ent "
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
android: layout_height= "wrap_cont ent "
android: t ext = "@st ring/ but t on_send" / >
< / LinearLayout >
< ?xml version= "1.0" encoding= "ut f -8"?>
< resources>
< st ring name= "app_name"> My First App< / st ring>
Applicat ion g name o
< string name= "edit _message"> Enter a message< / st ring>
Applicat ion run o o gc t ext box oc to 5oo`o5oo st ring Ent er a message o
< string name= "but t on_send"> Send< / string>
Applicat ion run o o gc but ton o 5oo`o5oo st ring Send o
< st ring name= "menu_set t ings"> Sett ings< / st ring>
< st ring name= "t it le_act ivity_main"> MainAct ivity< / st ring>
< / resources>
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion ogtq_ t (o)
oocooo5 ocoqtc5 ( o)o5 _o_oooo proj ect oooogto5to5o
ooqto text box ocoq but t on o creat e ooo_ot oocooo5 text box Oo5c5goO__ot but t on
o a o gc o c5o display oo_ooooo oc oq tc5 (o)o 5_o_o o o o MyApp
o _oq oc _o to5o oo c too o o o5to
Open MyApp> res> layout> act ivit y_main.xml
Add android: onClick= "sendMessage" as follow:
< But t on
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: t ext = "@st ring/ but t on_send"
android: onClick= "sendMessage" / > ( oococtoOoO_o)
Open MyApp> src> MainAct ivit y.j ava
Add t hese codes.
package com.example.myfirst app;
import android.app.Act ivit y;
import android.content.I nt ent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget .Edit Text ;
public class MainAct ivity ext ends Act ivit y {
public f inal st atic St ring EXTRA_MESSAGE = "com.example.myf irst app.MESSAGE";
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
/ * * Called when t he user clicks t he Send but t on * /
public void sendMessage(View view) {
I nt ent int ent = new I nt ent (this, DisplayMessageActivity.class);
Edit Text edit Text = ( Edit Text ) f indViewByI d( R.id.edit _message) ;
St ring message = edit Text .get Text ( ) .t oSt ring( ) ;
int ent .put Ext ra( EXTRA_MESSAGE, message) ;
st art Act ivit y( int ent ) ;
}
@Override
public void onCreat e(Bundle savedI nst anceSt ate) {
super.onCreat e(savedI nst anceSt at e);
set Cont ent View( R.layout .act ivity_main) ;
}
@Override
public boolean onCreat eOpt ionsMenu( Menu menu) {
get MenuI nf later( ) .inf late(R.menu.act ivity_main, menu) ;
return true;
}
}
Tip: I n Eclipse, press Ct rl + Shif t + O t o import missing classes
Now you need t o creat e t he DisplayMessageAct ivity class in order f or t his t o work.
To creat e DisplayMessageActivit y class
1. Click New in t he t oolbar.
2. I n the window that appears, open the Android folder and select Android Activity. Click Next .
3. Select BlankActivity and click Next .
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
4. Fill in t he act ivity details:
o Proj ect : MyFirst App
o Activit y Name: DisplayMessageActivit y
o Layout Name: act ivity_display_message
o Navigat ion Type: None
o Hierarchial Parent : com.example.myf irst app.MainActivity
o Title: My Message
Click Finish.
Figure 1. The new activity wizard in Eclipse.
Open t he DisplayMessageActivit y.j ava f ile.
The class already includes an implement ation of the required onCreat e() method.
There's also an implemtation of t he onCreat eOpt ionsMenu( ) met hod, but you won't need it f or t his app so
you can remove it .
The class should look like t his:
public class DisplayMessageActivity extends Act ivity {
@Override
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
public void onCreat e( Bundle savedI nst anceSt at e) {
super.onCreat e(savedI nst anceSt at e) ;
setCont ent View( R.layout .activity_display_message);
}
}
All subclasses of Act ivit y must implement t he onCreat e() method.
The syst em calls t his when creat ing a new inst ance of t he act ivit y.
I t is where you must define the activit y layout and where you should perf orm init ial setup f or t he act ivity
component s.
Add it to the manifest
You must declare all act ivities in your manif est f ile, AndroidManif est .xml, using an < act ivit y> element .
You used t he Eclipse t ools t o creat e t he act ivit y, it creat es a def ault ent ry.
I t should look like t his:
< application ... >
...
< act ivit y
android: name= ".DisplayMessageActivity"
android: label= "@string/ t it le_act ivity_display_message" >
< met a-dat a
android: name= "android.support .PARENT_ACTI VI TY"
android: value= "com.example.myfirst app.MainAct ivit y" / >
< / act ivit y>
< / applicat ion>
Receive t he I nt ent
Every Act ivit y is invoked by an I ntent, regardless of how t he user navigat ed t here.
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
You can get t he I ntent t hat st art ed your act ivity by calling get I nt ent ( ) and ret rieve t he dat a cont ained
wit hin it .
I n t he DisplayMessageActivit y classs onCreat e() met hod, get t he int ent and ext ract t he message delivered
by MainAct ivity:
I nt ent int ent = get I nt ent ( ) ;
St ring message = int ent .get St ringExt ra( MainAct ivit y.EXTRA_MESSAGE) ;
Display the Message
To show t he message on t he screen, creat e a Text View widget and set t he t ext using set Text () .
Then add t he Text View as t he root view of t he act ivit ys layout by passing it t o set Cont ent View() .
The complet e onCreat e() met hod f or DisplayMessageActivity now looks like t his:
@Override
public void onCreat e(Bundle savedI nst anceSt at e) {
super.onCreat e(savedI nst anceSt at e) ;
/ / Get t he message f rom t he int ent
I nt ent int ent = get I nt ent ( ) ;
St ring message = int ent .get St ringExt ra( MainAct ivit y.EXTRA_MESSAGE) ;
/ / Creat e t he t ext view
Text View t ext View = new Text View( t his);
t ext View.set Text Size(40) ;
t ext View.set Text (message) ;
/ / Set t he t ext view as t he act ivit y layout
set Cont ent View(t ext View);
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
}
Finally, DisplayMessageActivit y class looks like t his:
package com.example.myfirst app;
import android.app.Act ivit y;
import android.content.I nt ent;
import android.os.Bundle;
import android.widget .Text View;
public class DisplayMessageActivity ext ends Act ivit y {
@Override
public void onCreat e( Bundle savedI nst anceSt at e) {
super.onCreat e(savedI nst anceSt at e) ;
/ / Get t he message f rom t he intent
I nt ent int ent = get I nt ent ( ) ;
St ring message = int ent .get St ringExt ra( MainAct ivit y. EXTRA_MESSAGE) ;
/ / Creat e t he t ext view
Text View t ext View = new Text View(this) ;
t ext View.set Text Size(40) ;
t ext View.set Text (message) ;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
/ / Set t he t ext view as t he act ivit y layout
set Cont ent View(t ext View);
}
}
You can now run t he app.
When it opens, t ype a message in t he t ext f ield, click Send, and t he message appears on t he second
activit y.
Figure 2. Bot h act ivities in t he f inal app, running on Android 4.0.
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion (9) o ht arht arlwin.blogspot.com o5_o_o
Android Applicat ion ogtq_ t (oo)
ooooo5ooooootocooo_o_go5c
Android device oo`o 5 current t ime o display o o _ooo application oc o ogtoo
gc o t proj ect oc o create o o o o o
Proj ect name o skelet on ooot_ot act ivit y name o Now oooto
_o tgc Now.j ava file o 5o5o o source code ooo g o O_ o
package com.example.skelet on;
import android.app.Act ivit y;
import android.os.Bundle;
import android.view.View;
import android.widget .But t on;
import j ava.util.Dat e;
public class Now ext ends Act ivity implement s View.OnClickList ener {
Butt on bt n;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
@Override
public void onCreate(Bundle icicle) {
super.onCreat e( icicle) ;
bt n = new Butt on( this) ;
bt n.set OnClickList ener( t his) ;
updateTime( ) ;
set Cont ent View(bt n) ;
}
public void onClick( View view) {
updateTime( ) ;
}
privat e void updat eTime() {
bt n.set Text( new Dat e( ) .t oString( ) ) ;
}
}
Discussion
package com.example.skelet on;
oooo5 package o declare o o o5o
import android.app.Act ivit y;
import android.os.Bundle;
import android.view.View;
import android.widget.But ton;
import j ava.ut il.Dat e;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oooo5 o o program o 5o too class ooo import o o o5o android-specific class o5tc o android
package Oo 5g ooo
public class Now extends Activit y implement s View.OnClickList ener {
Button btn;
Act ivit y Now o5 public class _oc _o t android.app.Act ivit y o o base class ooq ext end o o O5to5o
o program o5ogc Now o5 but t on (bt n) ocoo hold ooO5tooo (But t on oo5 android widget
ocoo widget oo5 applicat ion o5 oto UI element oooo_o5o5o Widget oo5toto5 View oo
base class ooq ext end o o ooo )
But t on click oo5to to o act ivit y Oo 5o t rap o o oc o5o_o5c Now o5 OnClickList ener o o_ t
implement ooO5tooo
@Override
public void onCreate( Bundle icicle) {
super.onCreate(icicle) ;
bt n = new But t on(t his);
btn.setOnClickListener(this);
updateTime( );
setCont ent View(bt n);
}
Act ivit y o c_ooo5q onCreate() oo met hod o invoke ooooo
The first thing you should do is chain upward t o t he superclass, so the st ock Android activity init ialization
can be done.
_o tgc but ton inst ance oc o o creat e o o ooo _o too5 but ton click oo5to to inst ance o o o
set OnclickList ener() o o tooo 5to t_o tgc updat eTime() met hod o oo`ooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
o oqg5o 5oo`o5ooo5 program o run o o o5q o5 but ton o oa o oc current t ime o display
o o _oo5c o _o too5 set Cont ent View() oo
public void onClick( View view) {
updateTime( );
}
But t on ocoo click oogc o but t on oo configure ooO5to onClickList ener inst ance o5 onClick()
met hod o invoke o o ooo o o updateTime met hod o call oo5 o5oo o oc ooo`o5ooo5
but ton o click o o o oo`o 5o
privat e void updat eTime() {
bt n.setText(new Date() .toString( )) ;
}
Act ivit y open ooooo5q oooo but t on o click ooooo5q but t on g label o5 current time
ooogo 5o ooogo5co set Text () ootO5tooo
_o tgc oo5 program run _o_ o o5o oo o c tooooo oo o oqg5ooto t ap o o o current t ime
o oco t ap oct ocoo_o5cto5tooo o5oo oqooo_oo_o5c toqo5ot
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
o oc o onCreat e( ) method Oo updat eTime( ) o _o_o _o t run _o_ oo5t run run oc t current t ime
oooo5oooo t t ap ooo current time oo`o5oooo co t_o_ o
Android Applicat ion ogtq_t (oo)
Widget ooo creat e o o _o t act ivit y o 5 at t ach ooo5o Java code ogt_otooogooo
(_o to o oq to 5o o o oo oo ) XML-based layout f ile oooot_o__o tooo o_tgooo
o q_ too o to5tooo XML-based layout oo5o widget oc toc tg relationship q widget q o o g
cont iner ooooo relationship goooo5ooo5_oooto5o XML-based layout ooo r esource
oo_ocooooo5o layout file ooo res f older o5o o 5O5tooo
XML element oog at t ribut e oo5o ooo oo5 propert ies ooo widget ooo5 oo o o c o _t_oc goo .
cont ainer oooooo _o_oocoooo propert ies oooo _oo5 Butt on element ocog att ribut e value
o android: t ext St yle = bold oooo5 ooo5o but t on o5oo`oo t ext o bold _ocgoooooo5o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
o layout ooqoo oo _o t aapt t ool ooq_o t R.j ava o o file o generat e o o ootooo layout ooq
layout Oo widget ooootooogc R.layout .main or R.id.but ton cgo_ocoo`otac ooo
o ooco xml code oot_ot but t on ocooo_o_go5c gcot proj ect oco create ooo
o_ o oo5 NowRedux o ooto layout name o oo5 main o ooto o o o5o o xml code ooo
ot_ot res/ layout o5oo main.xml file o5otO_ooo _otgc run _o_oo but t on ocooo`o5oo
o but t on o tap oo_o_ o5o_oco5o5oooot o5ooooogooto5ot but t on
ocooo`o5ooooooooO5toot o5oo
< Relat iveLayout xmlns: android= "http: / / schemas.android.com/ apk/ res/ android"
xmlns: tools= "htt p: / / schemas.android.com/ tools">
< Button
android: id= "@+ id/ but ton"
android: layout_width= "wrap_content "
android: layout_height = "wrap_cont ent "
android: t ext = "" / >
< / Relat iveLayout>
oooo5 XML layout file o 5 but t on oc o o creat e o o O5to5o
xmlns: android= "http: / / schemas.android.com/ apk/ res/ android"
Root element o 5 Android XML namespace o o o declare o o gooo _o5t element ooooo5 root g
children oo_oc _o t name space declarat ion o inherit o o ooo
android: id= "@+ id/ but t on"
o but ton o j ave code ooqo o to o to 5o oo5 o o ident ifier oc o ootO5tgoo android: id at tribut e
o o t_o too5 oo element o c to id ootcg5oo oo t j ava code ooqo o too`o too element ooo o5 id
ootooooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Button
But t on o o5o XML element go_ oo But t on o o5 Android-supplied widget oc o _oc oooo5 class
name ooootooo5oo ooooo5 oooc widget ocoo android.view.View g subclass _ocq
creat e o o oo o gc oo5 full package declarat ion o o goo _oo5 com.example.MyWidget o ogtgo 5oo
android: layout _width= "wrap_cont ent "
android: layout _height= "wrap_cont ent "
oooo5 but t on g width q height o oo o o o5o
android: t ext = ""
but ton o5oo`ococo t ext o oo5oo5 o5oooo`ococo space oO5too
o o gc but ton oc o o XML code qogtO5to o c _oc o main.xml o c o5 res/ layout o 5g oqo_o o layout file
o j ava code ooq access o o a c o o gc NowRedux.j ava file Oo onCreat e() call back Oo 5
setContentView(R.layout.main) ;
oo st atement ocoO_oogoo
_otoo5 but t on o otoogcoo5
f indViewByI d() ;
o_otoo`otgoo
o5oo j ava code ooo NowRedux.j ava ocOotO_ooo
package com.example.layout s;
import android.app.Act ivit y;
import android.os.Bundle;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
import android.view.View;
import android.widget.But t on;
import j ava.ut il.Dat e;
public class NowRedux ext ends Act ivit y
implement s View.OnClickList ener {
But t on bt n;
@Override
public void onCreat e(Bundle icicle) {
super.onCreat e(icicle) ;
set Cont ent View(R.layout .main) ;
bt n= ( But t on) findViewByI d(R.id.but t on) ;
bt n.set OnClickList ener(this) ;
updateTime( ) ;
}
public void onClick( View view) {
updateTime( ) ;
}
privat e void updat eTime() {
bt n.set Text (new Dat e( ).toString() ) ;
}
}
_o tgc run _o_oooo o5oooo_toogoo oocooo5 but t on o t ap
oooctoqo_o5cto5to5oooo gc program qoooo oocoooo5 but t on o XML code
qogtooo5o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion ogtq_t (o_)
Employing Basic Widget
ooco widget oooooootooo_o_ go5c Widget oo5 f iled, label, but t on cg o5oooo
Assigning Labels
Label o android o5oo5 Text View ooo`oo user o edit oooogot oooo5oooto ooo5
_oo5 t ext box oc o Oo 5 o_ogoO_ococoooooo5 user oqq o box
Oo5o5O_goooo5oo5c _ q _oooto5o label o o t ext box
qooooo5O_ootooco_ooooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Label oc o o creat e o o oc gc Text View inst ance o creat e o o goo oq5o oc q_ to XML layout file
Oo5 Text View element ocoOo_oto_t create ooacoo oo`o but t on ooooooo_too android:
t ext propert y o5oo5 oooo`ococo text oO_oo oo5go propert ies ooo_o_go5c
android: t ypeface
monospace o5t o5o5too
android: t extst yle
bold, italic or bold and it alic o5too
android: textColor
o5og5c ootqoo`oc o5ooo RGB hex f ormat qogtgoo _oo5-q og5c _oc oc gc # FF0000 oo
qo q5oc o o o _o_ go5c proj ect oc o creat e o o _o t Label o o_ ooto act ivit y name o main
o ooto _o tgc main.xml file o 5o5o o code ooo tO_ _o t run o
< Text View xmlns: android= "ht t p: / / schemas.android.com/ apk/ res/ android"
android: id= "@+ id/ Text View"
android: layout _widt h= "wrap_cont ent "
android: layout_height= "wrap_cont ent "
android: t ext = "You were expect ing somet hing prof ound" / >
oooo5 out put oo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Fields of Green. Or Other Colors.
o oc o Edit Text o_o5cto_o5ooo
Edit Text widget oo5 Text View g subclass o Edit Text g propert ies ooooo5
android: autoText
auto spelling goc gc o tgooo
android: capitalize
goO_ooo oOootc5oto capit al _oc aut o o_o5c tootoco o
android: digits
digit oooo5 oo o oc gc oo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
android:singleline
single line or mult iline o5t oo o o o o
android: numeric
numeric-only input o oo
android: password
password input o o o
android: phoneNumber
phone number ooogoO_o
Field o o proj ect oc o creat e o o o activit y name o FieldDemo o ooto
main.xml o 5 o5o o code ooo tO_ o
< ?xml version= "1.0" encoding= "ut f -8"?>
< Edit Text xmlns: android= "ht t p: / / schemas.android.com/ apk/ res/ android"
android: id= "@+ id/ field"
android: layout widt h= "f ill parent "
android: layout height = "f ill parent"
android: singleLine= "f alse"
/ >
android: singleLine o5 false _ocooo c5goO_oo ococtoo goO_acooo
FieldDemo.j ava file o 5o5o o code ooo tooo tO_ o
package com.commonsware.android.basic;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
import android.app.Act ivit y;
import android.os.Bundle;
import android.widget .Edit Text ;
public class FieldDemo ext ends Activit y {
@Override
public void onCreat e(Bundle icicle) {
super.onCreat e(icicle) ;
setCont ent View(R.layout.main) ;
Edit Text f ld= (Edit Text )findViewByI d(R.id.field) ;
f ld.setText ("Licensed under t he Apache License, Version 2.0 " +
"( t he \ "License\ ") ; you may not use t his f ile " +
"except in compliance wit h t he License. You may " +
"obt ain a copy of t he License at " +
"ht t p: / / www.apache.org/ licenses/ LI CENSE-2.0") ;
}
}
Run _o_o o5ooooooooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
ht t ps: / / dl.dropbox.com/ u/ 94729309/ how%20to%20writ e%20android%20application%20%2813%29.docx
Android Applicat ion ogtq_t (og)
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
1. t rivia o o game oc o o design o o ooo
2. applicat ion prot ot ype (qo q5) o implement o o ooo
3. game prot ot ype o run ooo
1. Designing an Android Trivia Game
o game o 5 user o o5o oootoq tooootooo
oco ogocoooog5ooto5t
a5ta_coto5t
Great whit e sharks q diving O to to5t
oo5c oo o to5t
oo_o__otooo5tooto5o o game o Been, Done That ! oo_ootgo5c
1.1. Determining High-Level Game Features
o5oo feat ures ooooo
Applicat ion name, version and developer ooo5_ooo splash sequence oco
Scores ooo _o_ oo a c gq
Game rules oo o g c t_ogq
Game set t ings oo oootO5tgq
o feature ocoooq ocooto_o5ctoo_tooo ooooq5oOo main menu ocoOoooo
oo user o application ocoot ooooo5ta coo
_o _o_o5ggc Been There, Done That ! o o applicat ion o 5 screen oo o oo
1. A st art up screen
2. A main menu screen
3. A game play screen
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
4. A set t ings screen
5. A scores screen
6. A help screen
o screen ooo5 Been There, Done That ! applicat ion g o o user int erf ace ooooo
1.2. Determining Activit y Requirement s
Screen ococoo act ivity class oc o c g goo Figure 6.1 o 5 act ivit y ooo_oO5too
Base Activity class ocooOoO_oo oo QuizAct ivity o o_ ootoo oo act ivit y
go_oco5too
1. QuizAct ivit y: android.app.act ivit y class ooq ext end o o goo applicat ion preferences oo
_o5t applicat ion-wide set t ings and f eat ures oo o o class o5 define o o goo
2. QuizSplashAct ivit y: QuizAct ivit y ooq ext end o o oo oo splash screen oo oo
3. QuizMenuAct ivit y: QuizAct ivit y ooq ext end o o oo oo main menu screen oooo
4. QuizHelpAct ivit y: QuizAct ivit y ooq ext end o o oo oo help screen oooo
5. QuizScoresAct ivit y: QuizAct ivit y ooq ext end o o oo oo scores screen oooo
6. QuizSet t ingsAct ivit y: QuizAct ivit y ooq ext end o o oo oo set t ings screen oooo
7. QuizAct ivit y ooq ext end o o oo oo game screen oooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
1.3. Determining Screen-Specific Game Features
Act ivit y ococg basic f eat ures oo o def ine ooooo
1.3.1. Def ining Splash Screen Feat ures
Splash screen o o applicat ion o o ent ry point _ocooo o g funct ionalit y (code) ooo
QuizSplashAct ivity class Oo 5ogtgoo o screen o o5o oo o cq tooo o o goo
Applicat ion name and version o _ogoo
Game oo c o oc c5tcg5oo5c too graphic or logo o _ogoo
ooo o O5to oq oc o og5o gc main menu screen o o o o_o5c to5tgoo
Figure 6.2 o5 splash screen o _oO5too
1.3.2. Def ining Main
Menu Screen Feat ures
Main menu screen oococ _o5t screen oooo5tac goo Main menu screen o splash
_o tgc oo`o5goo user o o oo screen oo5tocoooo5 ogta cgoo o g f unctionalit y ooo
QuizMenuAct ivit y class o5ogtgoo main menu screen oo oootgo5ooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Splash screen _o to5q main menu screen o o o oo`o5goo
User o Play Game, Set t ings, Scores, or Help
_o _o g5ogta c o5c o o ootO5tgoo Figure 6.3 o5 main menu screen o_oO5too
1.3.3. Def ining Help Screen Feat ures
Help screen ooo5 user o game ooo play goooo5 o_o5ooo o g f unct ionalit y ooo5
QuizHelpAct ivit y class o 5ogtgoo help screen oo5o oo o ooo o goo
User o help t ext oo _o_o t o o gc scroll oo _o too a c o5c oo ootgoo
User o Quest ion oc suggest oo a c o5c oo ootgoo
Figure 6.4 o 5 Help screen o_oO5too
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
1.3.4. Def ining Scores Screen Feat ures
o screen ooo5 user o game scores oo_o_ a c o5c o o ootgo 5o o g f unctionalit y ooo
QuizScoresAct ivit y class o 5ogtgooo o screen o 5o5o ooo oooo5c o o ootgoo
Top score oo_ogoo
ooo o user o game screen oo5o5o gc oq5o o t score o _ogoo
Figure 6.5 o5 scores screen o _oO5too
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
1.3.5. Def ining Set t ings Screen Feat ures
o screen ooo5 user o game set tings oo edit oo save o o a c o5c oo5c g o ootgoo game
set t ings ooo o5o user name and ot her import ant features ooo o_o5o5o o g f unctionalit y oo o
QuizSet t ingAct ivit y class o5ogtgoo o screen oooootgo5ooo
User o game set t ings oo O_ a c o5c
User o oc5too5oo o o oo`a c o5c
Figure 6.6 o 5 basic sett ings screen o_oO5tooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
1.3.6. Def ining Game Screen Features
o screen o t rivia quiz o display oo_ogooo o g f unct ionalit y o QuizGameAct ivity class
o 5ogtgoo o screen oo5oooooooogoo
Yes/ no quest ions oooo o o ootgooo
I nput o handle o o goo . score q st at e o o o tO5tgoo
User oc5to _o tgc scores screen oo o_o5ctootgoo
Figure 6.7 o 5 game screen o_oO5tooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
2. I mplement ing an Application Prototype
1. Android proj ect oco creat e oogoo
2. Applicat ion resources oo_oc o st rings q graphics ooO_ goo
3. Screen oc o c oo layout resource oco c creat e o o goo
4. Screen oc o c oo j ava class oco c implement oo goo (Act ivit y class ooq ext end
o o goo )
5. Act ivit y 5to to 5o t_o_o oo applicat ion-wide preferences oo creat e o o goo
2.1. Creating a New Android Proj ect
Proj ect name: BTDT
Build t arget : Android 2.3.3 + Google API s (API Level 10)
Applicat ion name: Been There, Done That !
Package name: com.android.bt dt
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Creat e act ivit y: QuizSplashAct ivit y
(o set ting oct proj ect o creat e ooacoo. oo co_o_o oooco_toooo)
2.2. Adding Proj ect Resources
Act ivit y oc o c oo layout file oc o c o oo act ivit y name oc o c oo o_ t t ext st ring
ococooo application icon oo_t ocoo5ooo_o5ctoooo
2.2.1. Adding St ring Resources
Eclipse ooq res\ values\ st rings.xml o c o oc o hello o o st ring o oo o o o st ring oc oo
creat e o o o screen oc o o st ring oc o a q tq screen o oo g o oo o5o oo c t_oc oqgoo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<r esour ces>
<st r i ng
name="app_name">Been Ther e, Done That ! </ st r i ng>
<st r i ng
name="hel p">Hel p Scr een</ st ri ng>
<st r i ng
name="menu">Mai n Menu Scr een</ st r i ng>
<st r i ng
name="spl ash">Spl ash Scr een</ st r i ng>
<st r i ng
name="set t i ngs">Set t i ngs Screen</ st ri ng>
<st r i ng
name="game">Game Scr een</ st ri ng>
<st r i ng
name="scor es">Scores Scr een</ st r i ng>
</ r esour ces>
2.2.2. Adding Drawable Resources
Applicat ion oo oc oo5 oo icon o ooo 48 x 48 pixel g o PNG t ype image file oc o creat e
o o o phot oshop o o5o o t_o too5 oo o_ o quizicon.png o ooto _o tgc drawable o o folder
OO_ o gc g a c o icon.png o o5o oo oc o o o ooo o o o o o size go _tq creat e
o o O5to5g gc o_ t / drawable-ldpi, / drawable-mdpi, / drawable-hdpi o o folder ooo 5 size
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
coc__otooO_acooo (low, medium, high resolution) o_ooo5 gooto_ oootgooo o
go g oooo o ooo ref er o o o o oc o Oo o5 ref er o o cg5o ooo android operat ing syst em o
device ooocoo5oo icon version ooo5o5 ogtootooooo (o proj ect ooooo5
ooootooo oocoogo5ocoo_ogc default icon ooto)
2.2.3. Adding Layout Resources
Act ivit y oc o c oo layout resurce file oc o c creat e o o goo main.xml o splash.xml
o o_ o_o5c to splash.xml file o _o Oo o5to (copy o o o) o_ ooo game.xml, help.xml, menu.xml,
scores.xml, set t ings.xml oooto error oooo octgoq layout Oo Text View conrol o @st ring/ hello
st ring o ref er o o O5to o o hello o o5oog oo5 o t oo`o 5oo o _o oo oo_o5c o layout file
oc o oc tc o 5_oc go 5g ooo game.xml o c o oc _o t @st ring/ hello oqg5o 5 @st ring/ game qc5tO tgoo
splash.xml ooc oq o file oo5to to 5 oo o c g5o_ ooq c5tO to @st ring/ splash,
@string/ help, @string/ menu, @st ring/ scores, @string/ set t ings cg o__oc oo
2.3. I mplement ing Application Activities
src\ com.androidbook.bt dt .hour6\ QuizSplashAct ivity.j ava file o copy o5to file name o
QuizAct ivit y.j ava o ooto coding oo5o oo c t_oc goo _o tgc save o o O5to
package com. andr oi dbook. bt dt . hour 6;
i mport andr oi d. app. Act i vi t y;
publ i c cl ass Qui zAct i vi t y extends Act i vi t y {
publ i c stati c fi nal St r i ng GAME_PREFERENCES = "GamePr ef s";
}
2.3.1. Creat ing t he Rest of t he Applicat ion Act ivit ies
oq5o Oo copy _oOo o o _o t file name ooo QuizMenuAct ivit y, QuizHelpActivit y,
QuizScoresAct ivit y, QuizSet t ingsAct ivity, QuizGameAct ivity o to tooto ( copy
oo o oc gc oq5o oc q_ to com.androidbook.bt dt .hour6 package name o right click o o _o t New Class
o ogto _o tgc class name ooto o o)
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
go5o file octo5 error goo o5o_o5co_tooo5 file octo5 main.xml ootO5too
oocooogoo5ot splash.xml oo_o5ctoo_ooo oo_o5c file ocooctco5ooo_o5ctgoo _oo5
QuizHelpAct ivit y.j ava f ile o set Cont ent View( ) method o 5
set Cont ent View(R.layout .help); o o_o5c tgoo
package com. andr oi dbook. bt dt . hour 6;
i mport andr oi d. os. Bundl e;
publ i c cl ass Qui zHel pAct i vi t y extends Qui zAct i vi t y {
/ ** Cal l ed when t he act i vi t y i s f i r st creat ed. */
@Over r i de
publ i c voi d onCr eat e( Bundl e savedI nst anceSt at e) {
super. onCr eat e( savedI nst anceSt at e) ;
set Cont ent Vi ew( R. l ayout . hel p) ;
}
}
oq o QuizScoresActivity, QuizSet t ingsAct ivit y, QuizMenuAct ivity, QuizGameAct ivit y f ile ooo5o_t
ototooo_o5ctgoo ocototo5o5oooct_ocoqgoo
package com. andr oi dbook. bt dt . hour 6;
i mport andr oi d. os. Bundl e;
publ i c cl ass Qui zScor esAct i vi t y extends Qui zAct i vi t y {
/ ** Cal l ed when t he act i vi t y i s f i r st creat ed. */
@Over r i de
publ i c voi d onCr eat e( Bundl e savedI nst anceSt at e) {
super. onCr eat e( savedI nst anceSt at e) ;
set Cont ent Vi ew( R. l ayout . scores) ;
}
}
package com. andr oi dbook. bt dt . hour 6;
i mport andr oi d. os. Bundl e;
publ i c cl ass Qui zSet t i ngsAct i vi t y extends Qui zAct i vi t y {
/ ** Cal l ed when t he act i vi t y i s f i r st creat ed. */
@Over r i de
publ i c voi d onCr eat e( Bundl e savedI nst anceSt at e) {
super. onCr eat e( savedI nst anceSt at e) ;
set Cont ent Vi ew( R. l ayout . set t i ngs) ;
}
}
package com. andr oi dbook. bt dt . hour 6;
i mport andr oi d. os. Bundl e;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
publ i c cl ass Qui zMenuAct i vi t y extends Qui zAct i vi t y {
/ ** Cal l ed when t he act i vi t y i s f i r st creat ed. */
@Over r i de
publ i c voi d onCr eat e( Bundl e savedI nst anceSt at e) {
super. onCr eat e( savedI nst anceSt at e) ;
set Cont ent Vi ew( R. l ayout . menu) ;
}
}
package com. andr oi dbook. bt dt . hour 6;
i mport andr oi d. os. Bundl e;
publ i c cl ass Qui zGameAct i vi t y extends Qui zAct i vi t y {
/ ** Cal l ed when t he act i vi t y i s f i r st creat ed. */
@Over r i de
publ i c voi d onCr eat e( Bundl e savedI nst anceSt at e) {
super. onCr eat e( savedI nst anceSt at e) ;
set Cont ent Vi ew( R. l ayout . game) ;
}
}
2.3.2. Updating t he Android Manif est File
oocooo5 manifest file o_ocgoo5oo ( o) @drawable/ quizicon o_oco ( ooooooco
O_O5toooogcoo g_oto5tooto5ogcoo5 ooot) ( _) ocooO5to act ivit y 5toto
manifest file o 5 regist er o o goo ( g) debuggable application at t ribut e o t rue O5too QuizSplashAct ivit y
o default activit y _oc O5tg o5to o5_o_ goo o act ivit y g screen _oc o Splash screen ooq c _o t
display o 5o oo
2.4. Creating Application Preferences
o application o5bast ic st at e informat ion and user dat a oo ootO5toooo o funct ionalit y
o go oo androids shared preferences _oc o android.cont ent .SharedPref erences o o t_o_a c oo
Shared preferences o application Oo oo act ivit y ooo o_ oo`_o t o t_o_a c oo oo_o5c
base class _oco QuizAct ivit y.j ava file o 5 publ i c stati c fi nal St r i ng GAME_PREFERENCES = "GamePr ef s";
ooogto o5oooo5oo oo _o5t subclasses ooo oooooo`otaco5oo (creat e ooaco shared
preference ogoo oq oo O5to5og oo t preference oo o cat egories ooooc gc preference name
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
o o t_o toa c oo _oo5 game preferences o user preferences o oo shared preference
ooo oocooooo5ooo5ooooo5oo)
Applicat ion Oo shared preference O_ooo5ooocoo octo5tgoo
1. Act ivit y class Oo SharedPreferences obj ect g inst ance o ret rieve o o o o gc
get SharedPreferences() met hod o o tgoo
2. Preference ooo modify o o o SharedPreference.Edit or o o obj ect o creat e o o goo
3. Edit or oot_ot preference o o_o5ctgoo
4. Edit or o 5 commit () met hod o o t_o t o_o5c tO5to5oo o commit o o goo
2.4.1. Saving Specif ic Shared Pref erences
Preference oo o key/ value c o o o o tgoo preference value oo o o5o o dat a type
oo_ocacoo
Boolean
Float
I nt eger
Long
St ring
oo preference o save o o oc oo o o5o t_oo _o tgc oo5 SharedPreferences obj ect g
inst ance o oogooo Ooo5o_o5ooo o edit or oot_ot o_o5cto_tooo ogoo _otgc
commit oogoo _oo5 users name q age oo preference _oo save ooocgc act ivit y class
Oo5o5oo code oO_ goo
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor =settings.edit();
prefEditor.putString(UserName, J aneDoe);
prefEditor.putInt(UserAge, 22);
prefEditor.commit();
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
preference oo 5to to clear o o oc gc shared preferences edit or o o ta c ooo clear( ) met hod
o o tgoo o _ preference ooo o5 remove o o oc o5o gc oo5 ( o o remove o o oc o preference
o_oo_ot) remove() met hod ootgoo
2.4.2. Ret rieving Shared Preferences
Edit or oooooaco5o_o5c creat e oogo5Oo ret rieve oogo5oooooo activit y class
Oo shared preference values oo o ret rieve o o o o5o o code ooo tgoo
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
if (settings.contains(UserName) ==true) {
// We have a user name
String user =Settings.getString(UserName, Default);
}
( shared preferences oooo5oo5ooooootoot oq5oocto5oo5 otgoo)
3. Running the Game Prototype
gcot debug configurat ion o creat e oogoo
3.1. Creating a Debug Configuration
New eclipse proj ect oooo debug conf igurat ion o o o o ooo AVD (emulat or) o 5 conf igure
ooO5to5o5 oo application o5ooooO5to Minimum sdk version qOo_ocgooo (oo
application g minimum sdk version oo oo5o O5to o_ to o5_oq _o_ oc gc AndroidManif est .xml file
o5_o_a cooo)
<uses- sdk
andr oi d: mi nSdkVer si on="7"
andr oi d: t ar get SdkVer si on="10"></ uses- sdk>
ooo5o o applicat ion o android API level 7 acOo configure ooO5to emulat or q real android
device ooo5otacoooo o applicat ion oogtooo5oo5 API level 10
oot_otogtO5tooooooo5o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
AVD oooogoototogcoo5 Eclipse t oolbar o Android Virt ual Device Manager
ooqo5tacooo o tog ooc oq tc5o AVD ooq_to_o5_ot_ocooo applicat ion oo run oc o 5 Eclipse
t oolbar oo`o Android Virt ual Device Manager o click _o t ooooO5to AVD o select o o . _o tgc st art
but t on o click gooo
3.2. Launching the Prototype in the Emulator
emulat or oc o5_o gc AVD manager window o _oq o o _o t (emulat or oooocto5ooootoq5)
eclipse o Run but t on o click gooo otoo5c ( bug) o ooto click o o o_ tgoo
o5oooo_toooo
ooo emulat or q run o5ooo5 Lunch a new Andorid Virt ual Device opt ion o select o o _o t OK
o o o Ooo 5_oO5tooo5oo5 conf igure ooO5to AVD ogo o5ooo`ooqo5o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oocoo5ooO5tgc ocoo_ocgooooo ocoOoooooO5tacooo ocoOo ogcoo5 oo AVD
otoooo5 og_oto OK o click o
emulat or o 5 run go5 go toa tooo _oc a c gc real device qo run ococooo Figure 6.8
o 5_oO5to o o _tooooo (emulat or o5ocoo_t inst all oo_o t run _oooo)
run _o__otoo oocoqtc5Ooo5o5oOtOt_o5t_o5t_ocgo5 oo o ootoo t
Figure 6.8
oooooo_ot Applicat ion window o5_o_ooogcooooO5to icon ootq Been There, Done That !
o o o_ o applicat ion oooooooo _o5to to toqo application ooo o oc_o_ooo
Ooooo_t_oqooooo Been There, Done That ! o o o_ o applicat ion o click o o o o o o
def ault _oc O5to o QuizSplashAct ivit y oc_otooooooo o act ivit y o 5 Text View ocoo5oooo
oo_o5c t it le o 5 Been There, Done That ! oo5oo_ot Splash Screen o o c5o oo5 _oootoqo Text View
ocoo5oogo5o oOog5oooogcoo5o5toctogooacoo_o applicat ion f ramework
ooo5oo5ctc 5o_oo5o_ot_ocooo coding ooOoO_oo_oct_oc game applicat ion
ocoo5c_oc c5ogto5t_ot_oco5tooooo oq5oocoqtc5ooo5ooogto5tooo
( o applicat ion o oo o gc uninst all ooogooo)
o o oo qo q5 proj ect 5t o5oo link o 5o o
ht t ps: / / dl.dropbox.com/ u/ 94729309/ Hour6.rar
google doc o 5o_ t Hour6 oootO5tooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion ogtq_ t (og)
(og) o 5o o o o5o oo o o o5too
I mplementing an Animated Splash Screen
1. Splash screen o o design o o oo
2. Splash screen layout o _oc oo
3. Animat ion oo O_ oo
1. Designing the Splash Screen
main menu screen _o to o q_ tq_ t_o5gc o screen o o o _ogoo
o screen o5 t it le and version informat ion ooo_ooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
2. I mplementing the Splash Screen Layout
o screen o 5 Text View and I mageview cont rol _ootO5too Text View o t ext st ring o_ooq
I mageView o graphic o_oo o view ooo screen o 5 ooooqg5ooo_too5c_tc5too ooo
Layout cont rol ootgoo layout cont rol o _to _tg oo
Layout cont rol q View cont rol ooo 5 at tribut e oooo o o ootgoo oo o o g behavior o
cont rol ooacoo cont rol oog widt h and height oooooo android: layout_width and
android: layout _heught oo at t ribut e ooootgoo o at tribut e value ooo dimension
ooo_t_ocacoo _oo5 number of pixels qoo o o o5o _too
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
_o5tq_tooqo_tooooogooo _oo5 f ill_parent or wrap_content o oo
_o5tq_ too tooo_ tg oootoo
o oo5 splash screen o 5 I mageView cont rol o display ooooo TableLayout cont rol
oot_ogo5c splash screen o5 parent layout ooo5 LinearLayout O5too oOo5 Text View,
TableLayout ( o Oo 5 TableRow cont rol _oooo TableRow oc o c o 5 I mageView _ocooo ), oq5oOo
Text View _oooo
2.1. Adding New Project Resources
o layout Oo5ot_o_oo st ring, color and dimension o o resources oo creat e o o goo
splash1.png, splash2.png, splash3.png, splash4.png ooogoo resolut ion go _tc q creat e ooO5t_ot
res/ drawable-ldpi, res/ drawable-mdpi, res/ drawable-hdpi ototo5O_O5too
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
/ res/ vlues/ strings.xml f ile Oo 5 St ring go O_ oo ocoo t op t it le (Been There) oo. oc o o
bot t om t it le ( Done That ! ) oo. oq5oocoo version inf ormation o o ( mult iple line O5tootgoo)
oc oq tc5 13 o 5o o o o splash oo string ooooooo5 ooqt ootcoooogooto
oO__oO5to5 st rings.xml f ile o5o5o oo c t_oc oqgoo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<r esour ces>
<st r i ng
name="app_name">Been Ther e, Done That ! </ st r i ng>
<st r i ng
name="hel p">Hel p Scr een</ st ri ng>
<st r i ng
name="menu">Mai n Menu Scr een</ st r i ng>
<st r i ng
name="set t i ngs">Set t i ngs Screen</ st ri ng>
<st r i ng
name="game">Game Scr een</ st ri ng>
<st r i ng
name="scor es">Scores Scr een</ st r i ng>
<st r i ng
name="app_l ogo_t op">BEEN THERE</ st r i ng>
<st r i ng
name="app_l ogo_bot t om">DONE THAT! </ st r i ng>
<st r i ng
name="app_ver si on_i nf o">Ver si on 2. 0. 0\ nCopyr i ght 2011 Maml ambo\ nAl l Ri ght s
Reser ved.
</ st r i ng>
</ r esour ces>
/ res/ vaules/ o 5 color.xml o o f ile ocoOoooo color resources googoo oc o o t it le t ext
o o ( og oog5c ). oc o o version t ext o o ( grayish whit e). oq5oocoo version t ext background
oo ( deep blue) color.xml f ile o5o5oooct_ocgoo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<r esour ces>
<col or
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
name="l ogo_col or ">#FFFF0F</ col or >
<col or
name="ver si on_col or ">#f 0f 0f 0</ col or >
<col or
name="ver si on_bkgrd">#1a1a48</ col or >
</ r esour ces>
/ res/ vlues/ dimens.xml file oc o Oo O_ oo dimension value go O_ goo t it le font size oo
(48dp), version t ext font size oo (15dp), version t ext ococtqococt line spacing oo (3dp)
dp oo unit ooto5o oo ooo device resolut ion oooo oco_ooo dimens.xml file
o 5o5o o code oog goo _o tgc save o
2.2. Updating the Splash Screen Layout
splash.xml file Oo gcooO5to cont rol ooooooco
_o tgc oo5 o5o ooc ooo c too5c go o
1. LinearLayout O_ oo Orient at ion o vert ical O5too background color at t ribut e o
oog5c O5too oq o5ooo_ tO_ oo
<Li near Layout
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: or i ent at i on="ver t i cal "
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_parent "
andr oi d: backgr ound="@andr oi d: col or / bl ack">
2. Text View (Text ViewTopTit le) O_ oo
<Text Vi ew
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Text Vi ewTopTi t l e"
andr oi d: t ext ="@st r i ng/ app_l ogo_t op"
andr oi d: t ext Col or ="@col or / l ogo_col or"
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: gr avi t y="t op| cent er "
andr oi d: t ext Si ze="@di men/ l ogo_si ze"></ Text Vi ew>
3. TableLayout (TableLayout 01) O_ oo
<Tabl eLayout
andr oi d: i d="@+i d/ Tabl eLayout 01"
andr oi d: st r et chCol umns="*"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _wi dt h="mat ch_par ent ">
4. TableLayout Oo 5 TableRow O_ oo TableRow Oo 5 I mageView _o O_ oo
<Tabl eRow
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
andr oi d: i d="@+i d/ Tabl eRow01"
andr oi d: l ayout _hei ght ="wr ap_cont ent " andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al ">
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew2_Lef t "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash1"></ I mageVi ew>
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew2_Ri ght "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash2"></ I mageVi ew>
</ Tabl eRow>
5. TableLayout O o5 oq5o Oo TableRow oc oOo O_ oo TableRow O o5 I mageView
_oO_oo
<Tabl eRow
andr oi d: i d="@+i d/ Tabl eRow02" androi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al ">
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew3_Lef t "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash3"></ I mageVi ew>
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew3_Ri ght "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash4"></ I mageVi ew>
</ Tabl eRow>
6. LinearLayout Oo 5 Text View (Text ViewBot t omTit le) oc o O_ oo
<Text Vi ew
andr oi d: l ayout _wi dt h="mat ch_par ent " androi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Text Vi ewBot t omTi t l e" andr oi d: t ext ="@st ri ng/ app_l ogo_bot t om"
andr oi d: t ext Col or ="@col or / l ogo_col or "
andr oi d: gravi t y="cent er "
andr oi d: t ext Si ze="@di men/ l ogo_si ze"></ Text Vi ew>
7. LinearLayout Oo 5oq5o Oo Text View (Text ViewBot t omVersion) oc o Oo O_ oo
<Text Vi ew
andr oi d: i d="@+i d/ Text Vi ewBot t omVer si on"
andr oi d: t ext ="@st ri ng/ app_ver si on_i nf o"
andr oi d: t ext Si ze="@di men/ versi on_si ze"
andr oi d: t ext Col or ="@col or / ver si on_col or "
andr oi d: backgr ound="@col or / ver si on_bkgr d"
andr oi d: l ayout _hei ght ="mat ch_par ent "
andr oi d: l i neSpaci ngExt r a="@di men/ versi on_spaci ng"
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: gr avi t y="cent er "></ Text Vi ew>
o_o tgc oo5 splash.xml f ile o 5o5o o code oo o c tg oqgoo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<Li near Layout
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: or i ent at i on="ver t i cal "
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_parent "
andr oi d: backgr ound="@andr oi d: col or / bl ack">
<Text Vi ew
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Text Vi ewTopTi t l e"
andr oi d: t ext ="@st ri ng/ app_l ogo_t op"
andr oi d: t ext Col or ="@col or / l ogo_col or"
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: gr avi t y="t op| cent er "
andr oi d: t ext Si ze="@di men/ l ogo_si ze"></ Text Vi ew>
<Tabl eLayout
andr oi d: i d="@+i d/ Tabl eLayout 01"
andr oi d: st r et chCol umns="*"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _wi dt h="mat ch_par ent ">
<Tabl eRow
andr oi d: i d="@+i d/ Tabl eRow01"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al ">
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew2_Lef t "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash1"></ I mageVi ew>
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew2_Ri ght "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash2"></ I mageVi ew>
</ Tabl eRow>
<Tabl eRow
andr oi d: i d="@+i d/ Tabl eRow02"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al ">
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew3_Lef t "
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash3"></ I mageVi ew>
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew3_Ri ght "
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: src="@dr awabl e/ spl ash4"></ I mageVi ew>
</ Tabl eRow>
</ Tabl eLayout >
<Text Vi ew
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Text Vi ewBot t omTi t l e"
andr oi d: t ext ="@st ri ng/ app_l ogo_bot t om"
andr oi d: t ext Col or ="@col or / l ogo_col or"
andr oi d: gr avi t y="cent er "
andr oi d: t ext Si ze="@di men/ l ogo_si ze"></ Text Vi ew>
<Text Vi ew
andr oi d: i d="@+i d/ Text Vi ewBot t omVer si on"
andr oi d: t ext ="@st ri ng/ app_ver si on_i nf o"
andr oi d: t ext Si ze="@di men/ versi on_si ze"
andr oi d: t ext Col or ="@col or / ver si on_col or "
andr oi d: backgr ound="@col or / ver si on_bkgr d"
andr oi d: l ayout _hei ght ="mat ch_par ent "
andr oi d: l i neSpaci ngExt r a="@di men/ versi on_spaci ng"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _gr avi t y="cent er _ver t i cal | cent er _hor i zont al "
andr oi d: gr avi t y="cent er "></ Text Vi ew>
</ Li near Layout >
_o tgc oo5 save _o t run o o5ooooctooooo
3. Working with Animation
o oc o splash screen o5 animat ion O_go5c Android platf orm o graphics animation
go _too5o o support oooota coo
Animat ed GI F images
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Frame-by-frame animat ion
Tweened animat ion
OpenGL ES
o applicat ion o 5oo5 Tweened animat ion o o too alpha (t ransparency), rot at ion, scaling,
t ranslat ing (moving) animat ions ooooacoo
3.1. Adding Animations Resources
Animat ion go o creat e o o _o t fade_in.xml, fade_in2.xml, cust om_anim.xml o o file oo_oc
/ res/ anim o 5o o too fade_in.xml file ooo5 2.5 seconds _o5o c t alpha value 0 ( t ransparent ) ooq 1
(opaque) _oc o_o5c too fade_in.xml file o 5o5o o code ooo c t_oc oo
<?xml ver si on="1. 0" encodi ng="ut f - 8" ?>
<set
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: shar eI nt er pol at or ="f al se">
<al pha
andr oi d: f r omAl pha="0. 0"
andr oi d: t oAl pha="1. 0"
andr oi d: dur at i on="2500">
</ al pha>
</ set >
o animat ion o t op Text View cont rol o5oto5o
oq5o oc o o fade_in2.xml animation file o o qqo o oo o5o5o st rat Of fset at tribut e o 2500
milliseconds O5to5o o o o5o animat ion _oc oo`o o5oo c t_o5to c c ooc t 5 seconds _oc ooo 2.5
seconds oc5c _o t 2.5 seconds f ade ( o q g5ooo5o o5o5c) o o oo 5 seconds o gc splash screen
ocooto display oo_ooooo5oooo fade_in2.xml file o5o5oooct_ocooo
<?xml ver si on="1. 0" encodi ng="ut f - 8" ?>
<set
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: shar eI nt er pol at or ="f al se">
<al pha
andr oi d: f r omAl pha="0. 0"
andr oi d: t oAl pha="1. 0"
andr oi d: dur at i on="2500"
andr oi d: st ar t Of f set ="2500">
</ al pha>
</ set >
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oq5o o toc o o TableLayout graphics o o animation sequence oc o o o goo ogo 5ooo
rot at ion, scaling, alpha t ransit ion ooo cust om_anim.xml file o5o5o oo c t_oc gooo
<?xml ver si on="1. 0" encodi ng="ut f - 8" ?>
<set
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: shar eI nt er pol at or ="f al se">
<r ot at e
andr oi d: f r omDegr ees="0"
andr oi d: t oDegr ees="360"
andr oi d: pi vot X="50%"
andr oi d: pi vot Y="50%"
andr oi d: dur at i on="2000" / >
<al pha
andr oi d: f r omAl pha="0. 0"
andr oi d: t oAl pha="1. 0"
andr oi d: dur at i on="2000">
</ al pha>
<scal e
andr oi d: pi vot X="50%"
andr oi d: pi vot Y="50%"
andr oi d: f r omXScal e=". 1"
andr oi d: f r omYScal e=". 1"
andr oi d: t oXScal e="1. 0"
andr oi d: t oYScal e="1. 0"
andr oi d: dur at i on="2000" / >
</ set >
0 ooq 360 degree o rot at e o o o 2 seconds O5too alpha value 0 ooq 1 o_o5c to5 2
seconds O5too 10% ooq 100% scale o_o5c to5 2 seconds O5too animat ion oc o o t 2 seconds
_o5oo parallel o o o 5o o _o tgc oo5 file o save _o t o animat ion o oo o c g5 View ooo 5o tooo
3.2. Animating Specific Views
Animat ion sequence ooo act ivit y class file (QuizSplashAcivit y class) o 5 coding ogt_o t o o too .
manage o o goo applicat ion o pause o o O5to o q o _to 5 animat ion ooo go O5tgoo application o
resume _oq o o gc animat ion o o_ t resume _oq o o goo
o fade_in animat ion o t it le Text View (Text Vi ewTopTit le) o 5 apply o o go5c o o go 5ooo5
QuizSplashAcit ivt y class o 5 Text View cont rol g inst ance o creat e o o oo animat ion resource
(Text ViewTopTitle) o Animation obj ect (logo1) O load oooo _otgc st rat Animat ion() met hod o
oo`oo o5o oo c too
Text Vi ew l ogo1 = ( Text Vi ew) f i ndVi ewByI d( R. i d. Text Vi ewTopTi t l e) ;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Ani mat i on f ade1 =Ani mat i onUt i l s. l oadAni mat i on( thi s, R. ani m. f ade_i n) ;
l ogo1. st ar t Ani mat i on( f ade1) ;
onPause() callback o oo`gc animat ion o st op o o goo clearAnimat ion() met hod o oo`go 5oo
o5o o 5 onPause() met hod g code o _oO5too
@Over r i de
protected voi d onPause( ) {
super. onPause( ) ;
/ / St op t he ani mat i on
Text Vi ew l ogo1 = ( Text Vi ew) f i ndVi ewByI d( R. i d. Text Vi ewTopTi t l e) ;
l ogo1. cl ear Ani mat i on( ) ;
Text Vi ew l ogo2 = ( Text Vi ew) f i ndVi ewByI d( R. i d. Text Vi ewBot t omTi t l e) ;
l ogo2. cl ear Ani mat i on( ) ;
/ / st op ot her ani mat i ons
}
3.3. Animating All View in a Layout
Animat ion ooo View oc o oc tc o 5 apply o o oo parent cont rol o5o child View cont rol
oo5totooo_t apply ooogoo Oto_oc TableLayout o5oo Layout AnimationCont roller
obj ect oot_otoogoo
o o o c qo o oo o gc
Animat ion o load o o goo
Layout Animat ionCont roller g inst ance o creat e o o goo
oo configure o o goo
_o tgc layout g set Layout Animat ion() met hod o pass o o ootgoo
_oo5- o5oo code oo o
cust om_animat ion o load o o oo
Layout Animat ionCont roller o creat e o o oo
_otgc TableLayout O o TableRow oco oc tc o 5 apply oo oo
Ani mat i on spi ni n = Ani mat i onUt i l s. l oadAni mat i on( thi s, R. ani m. cust om_ani m) ;
Layout Ani mat i onCont r ol l er cont r ol l er = newLayout Ani mat i onCont r ol l er ( spi ni n) ;
Tabl eLayout t abl e = ( Tabl eLayout ) f i ndVi ewByI d( R. i d. Tabl eLayout 01) ;
for ( i nt i = 0; i < t abl e. get Chi l dCount ( ) ; i ++) {
Tabl eRow r ow =( Tabl eRow) t abl e. get Chi l dAt ( i ) ;
r ow. set Layout Ani mat i on( cont r ol l er ) ;
}
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
o oqg5o 5 st art Aniat ion() met hod o call cg5oo o t o5o_o5c o Layout Animat ionCont roller
oooooto5too Layout AnimationCont roller animat ion o st op oooooo5 clearAnimat ion() met hod
ootgoo onPause() met hod o5o5oo code ooOoO_ooo5ooo
Tabl eLayout t abl e = ( Tabl eLayout ) f i ndVi ewByI d( R. i d. Tabl eLayout 01) ;
for ( i nt i = 0; i < t abl e. get Chi l dCount ( ) ; i ++) {
Tabl eRow r ow =( Tabl eRow) t abl e. get Chi l dAt ( i ) ;
r ow. cl ear Ani mat i on( ) ;
}
3.4. Handling Animation Life Cycle Events
o o gc splash screen o 5 animat ion ooO_ _oto5t_o oo o o go 5o animat ion _o to5to o
QuizSplshAct ivit y ooq QuizMenuAct ivit y o o_o5c to oo QuizMenuAct ivit y o oc oo I nt ent oc o o
creat e o o gooo _o tgc o I nt ent o st art Act ivit y() met hod o pass o o gooo _o tgc QuizSplashAct ivit y
g finish() met hod o oo`gooo ( o o o o gc back but t on o o t_o t splash screen o _oq oo5ta c oo5 oo t
o5o_o5co QuizSplashAct ivit y o act ivit y st ack o5oO5to ocoO finish ooocoooo) f ade_in2
animation o5 5 seconds _o5oo Animat ionList ener obj ect o creat e o o oo o o 5 ooo callback
ooooo5 st art , end, repeat o o o oo5 onAnimat ionEnd() met hod o5 implement o o o o ooo
Animat ionList ener o ooo creat e oooo. onAnimat ionEnd() callback o ooo implement
oooooo5 o5oo code o5_oO5tooo
Ani mat i on f ade2 = Ani mat i onUt i l s. l oadAni mat i on( thi s, R. ani m. f ade_i n2) ;
f ade2. set Ani mat i onLi st ener ( newAni mat i onLi st ener ( ) {
publ i c voi d onAni mat i onEnd( Ani mat i on ani mat i on) {
st ar t Act i vi t y( newI nt ent ( Qui zSpl ashAct i vi t y. thi s, Qui zMenuAct i vi t y. cl ass) ) ;
Qui zSpl ashAct i vi t y. thi s. f i ni sh( ) ;
}
}) ;
_o tgc oo5 emulat or or real device ( handset ) o 5 run _o_ o splash screen o 5 animat ion ooooo _o tgc
main menu o o_o5c to5too o oo o main menu o 5oo`o5oo o5ooo oo5
oq5oocoqtc5o5ooo5_ogo5c
Example proj ect code oo_tO_ootO5tooo hour7 o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Android Applicat ion ogtq_ t (o_)
(og) o 5o o o o5o oo o o o5too
I mplementing the Main Menu Screen
4. Main menu screen o design o o oo
5. Main menu screen layout o implement oooo
6. List Views cont rols oo q oo o o oo
7. _o5t menu t ypes oo qoooooo
1. Designing the Menu Screen
o screen ooc oc application g _o5t screen ooo o5tgo 5o user o 5ogtcg5 option go g ooo
oooooo5 play t he game, review t he help, conf igure t he set t ings, view t he high scores o oo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
ooaco q_ too tooooo5 o _to _tg o5oo opt ion ococoo but t on ococO5to_tgoo
_o tgc ooocg5 but t on o click ooooo list en oooo _otgcooocg5 screen o display oo_ooooo
_oo5 play t he game o o but t on o click o o gc game play oo screen o o_o5c tootgo 5oo ooooo option
ooo5to5o5qo but ton ooo_to5t_otO_goo oo_o5c but t on ooto ListView cont rol
ootoo List View Oo 5 option ooo5t_o tO_ O5ta c oo option ooo5to screen ooqgco5 scroll
oo_oto5ta coooo
Menu o background image O_acoo List View cont rol o 5 cust om select ion graphic
o_tO_acoo
User o list opt ion oc o o o click oooooo o qoo o c g5 act ivit y (screen) o display
oo_ooo_tooo
1.1. Determining Main Menu Screen Layout Requirements
QuizMenuAct ivit y oot_o_oo / res/ layout / menu.xml layout f ile o 5 o o o5ooo o goo header,
List View cont rol, I mageView cont rol ooooo
1.2. Designing the Screen Header with Relat iveLayout
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Header o 5 screen t it le oo Text View cont rol o display o o goo Text View cont rol
gootocooco5 oootooogco_o_oo5cto5oo Relat iveLayout oot_ot c_o_go5c
Relat iveLayout ooo5 oOo5oo child view ooo parent layout or ot her child view cont rol
ooqoo_o5c oqg5oootacoooo oo_o5c header o Relat iveLayout cont rol qoooo oo5
child layout go ooo
I mageView cont rol (parent cont rol g t op left q _ oo)
Text View cont rol (parent cont rol g t op cent er q_ oo )
I mageView cont rol (parent cont rol g t op right q_ oo )
<Rel at i veLayout
andr oi d: i d="@+i d/ Rel at i veLayout 01"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnPar ent Top="t r ue">
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew_MenuHeader "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _al i gnPar ent Lef t ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
<Text Vi ew
andr oi d: i d="@+i d/ Text Vi ew01"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: t ext ="@st r i ng/ menu"
andr oi d: t ext Si ze="@di men/ scr een_t i t l e_si ze"
andr oi d: shadowDx="0"
andr oi d: shadowDy="0"
andr oi d: shadowRadi us="10"
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _gr avi t y="f i l l _hori zont al | cent er "
andr oi d: l ayout _al i gnPar ent Top="t r ue"
andr oi d: l ayout _cent er Hori zont al ="t r ue"
andr oi d: shadowCol or ="@andr oi d: col or / whi t e"
andr oi d: t ext Col or ="@col or / t i t l e_col or "></ Text Vi ew>
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew_MenuHeader 2"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _gr avi t y="r i ght | cent er _ver t i cal "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _al i gnPar ent Ri ght ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
</ Rel at i veLayout >
1.3. Designing the List View Control
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
List View cont rol o5 View obj ect o5t_otO_acoo cust om divider q selector
oooO__oto _o_oo5cto5cooacoo
<Li st Vi ew
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Li st Vi ew_Menu"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: di vi der ="@dr awabl e/ di vi der "
andr oi d: l i st Sel ect or ="@dr awabl e/ sel ect or "
andr oi d: l ayout _cent er Hor i zont al ="t r ue"
andr oi d: l ayout _bel ow="@+i d/ Rel at i veLayout 01"></ Li st Vi ew>
1.4. Finishing Touches f or t he Main Menu Layout Design
List View cont rol _o tgc I mageView O_ oo
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew_MenuBot t om"
andr oi d: sr c="@dr awabl e/ hal f "
andr oi d: l ayout _al i gnPar ent Bot t om="t rue"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: adj ust Vi ewBounds="t rue"
andr oi d: scal eType="cent er I nsi de"></ I mageVi ew>
main menu screen glayout design o o5o o 5_o_ o
Relat iveLayout
o Relat iveLayout
I mageView
Text View (Tit le)
I mageView
o List View
I t em(0): Text View( Play Game )
I t em(1): Text View( View Scores )
I t em(2): Text View( Set t ings )
I t em( 3) : Text View( Help )
o I mageView
5to t_o tgc menu.xml file o5o5o oo c t_oc oqoo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<Rel at i veLayout
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_parent "
andr oi d: backgr ound="@dr awabl e/ bkgr nd">
<Rel at i veLayout
andr oi d: i d="@+i d/ Rel at i veLayout 01"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnPar ent Top="t r ue">
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew_MenuHeader "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _al i gnPar ent Lef t ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
<Text Vi ew
andr oi d: i d="@+i d/ Text Vi ew01"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: t ext ="@st r i ng/ menu"
andr oi d: t ext Si ze="@di men/ scr een_t i t l e_si ze"
andr oi d: shadowDx="0"
andr oi d: shadowDy="0"
andr oi d: shadowRadi us="10"
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _gr avi t y="f i l l _hori zont al | cent er "
andr oi d: l ayout _al i gnPar ent Top="t r ue"
andr oi d: l ayout _cent er Hori zont al ="t r ue"
andr oi d: shadowCol or ="@andr oi d: col or / whi t e"
andr oi d: t ext Col or ="@col or / t i t l e_col or "></ Text Vi ew>
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew_MenuHeader 2"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _gr avi t y="r i ght | cent er _ver t i cal "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _al i gnPar ent Ri ght ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
</ Rel at i veLayout >
<Li st Vi ew
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Li st Vi ew_Menu"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _cent er Hor i zont al ="t r ue"
andr oi d: l ayout _bel ow="@+i d/ Rel at i veLayout 01"></ Li st Vi ew>
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew_MenuBot t om"
andr oi d: sr c="@dr awabl e/ hal f "
andr oi d: l ayout _al i gnPar ent Bot t om="t rue"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: adj ust Vi ewBounds="t rue"
andr oi d: scal eType="cent er I nsi de"></ I mageVi ew>
</ Rel at i veLayout >
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
2. I mplementing the Main Menu Screen Layout
Proj ect Oo resurce ooOoO_goo
2.1. Adding New Project Resources
Layout Oo 5 drawable, st ring, color, dimension oo resource ooO_goo O_ go 5ooo
/ res/ drawable Oo 5 bkgrnd.png, divider.png, half .png, slect or.png oo bkgrnd.png o background image
_oc o too (Relat iveLayout oo to 5) divider.png and select or.png o cust om divider and select or
_oc o too ( ListView cont rol oo to 5) half .png o screen g o5oo_oo5otoo (I mageView oo to 5)
/ res/ values/ string.xml o 5 st ring ooO_ oo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<r esour ces>
<st r i ng
name="app_name">Been Ther e, Done That ! </ st r i ng>
<st r i ng
name="hel p">Hel p Scr een</ st ri ng>
<st r i ng
name="set t i ngs">Set t i ngs Screen</ st ri ng>
<st r i ng
name="game">Game Scr een</ st ri ng>
<st r i ng
name="scor es">Scores Scr een</ st r i ng>
<st r i ng
name="app_l ogo_t op">BEEN THERE</ st r i ng>
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
<st r i ng
name="app_l ogo_bot t om">DONE THAT! </ st r i ng>
<st r i ng
name="app_ver si on_i nf o">Ver si on 2. 0. 0\ nCopyr i ght 2011 Maml ambo\ nAl l Ri ght s
Reser ved.
</ st r i ng>
<st r i ng
name="menu">MAI N MENU</ st r i ng>
<st r i ng
name="menu_i t em_set t i ngs">Set t i ngs</ st r i ng>
<st r i ng
name="menu_i t em_pl ay">Pl ay Game</ st ri ng>
<st r i ng
name="menu_i t em_scor es">Vi ew Scor es</ st r i ng>
<st r i ng
name="menu_i t em_hel p">Hel p</ st r i ng>
</ r esour ces>
/ res/ values/ colors.xml o5 Color resource ooOoO_oo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<r esour ces>
<col or
name="l ogo_col or ">#FFFF0F</ col or >
<col or
name="ver si on_col or ">#f 0f 0f 0</ col or >
<col or
name="ver si on_bkgrd">#1a1a48</ col or >
<col or
name="t i t l e_col or ">#f 0f 0f 0</ col or >
<col or
name="t i t l e_gl ow">#F00</ col or >
<col or
name="menu_col or ">#FFFF0F</ col or >
<col or
name="menu_gl ow">#F00</ col or>
</ r esour ces>
/ res/ values/ dimens.xml o 5 dimension ooO_ oo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<r esour ces>
<di men
name="l ogo_si ze">48dp</ di men>
<di men
name="ver si on_si ze">15dp</ di men>
<di men
name="ver si on_spaci ng">3dp</ di men>
<di men
name="scr een_t i t l e_si ze">40dp</ di men>
<di men
name="menu_i t em_si ze">34dp</ di men>
</ r esour ces>
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
2.2. Updat ing the Main Menu Screen Layout Files
Mast er layout file _oc o menu.xml ooq screen oc o o tg layout o oo o o O5too List View
cont rol ootoo layout file oco create oooo_ toootoo
2.2.1. Adding the List View Template Layout
List View cont rol o 5 it em ooo5t_o toa c oo it em oc o c oo layout t emplat e oc o c g goo
oo / res/ layout / menu_it em.xml f ile o 5O_ goo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<Text Vi ew
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: t ext Si ze="@di men/ menu_i t em_si ze"
andr oi d: l ayout _gr avi t y="cent er _hor i zont al "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: shadowRadi us="5"
andr oi d: gr avi t y="cent er "
andr oi d: t ext Col or ="@col or / menu_col or "
andr oi d: shadowCol or ="@col or / menu_gl ow"
andr oi d: shadowDy="3"
andr oi d: shadowDx="3" / >
3. Working with the List View Cont rol
ooco QuizMenuAct ivit y.j ava file o_o_go5c gcot List View cont rol o5
ooo5ooO_ooo _otgc List View cont rol Oo oo it em o click oooooo5 list en ooooo
user click o o o it em qo c o screen o display o o _oooo _oo5 Play Game it em o click gc Game play
a c oo screen o display o o _ogooo
3.1. Filling a ListView Control
List View Oo 5 it em go O_ o oo QuizMenuAct ivit y class g onCreat e() met hod o 5 code
ogtgoo
findViewByI d() met hod oot_ot List View oootoo
Li st Vi ew menuLi st = ( Li st Vi ew) f i ndVi ewByI d( R. i d. Li st Vi ew_Menu) ;
_o tgc resource st ring go o load o o oo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
St r i ng[ ] i t ems = { get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_pl ay) ,
get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_scor es) ,
get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_set t i ngs) ,
get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_hel p) };
oo o gc List View cont rol o_ tg_o o qooo data o_ tg_o data q layout templat e
(menu_it em.xml) o map o o ooto oq oo map o o ootoo adapter o ogtgoo oooo5 dat a t ype
oo`oo_o5oo o application oooo5 ArrayAdapt er o otooo
Ar r ayAdapt er <St r i ng> adapt = newAr rayAdapt er <St r i ng>( thi s, R. l ayout . menu_i t em, i t ems) ;
menuLi st . set Adapt er ( adapt ) ;
set Adapter() met hod o call o o gc List View control oqq data adapt er o o ta c o_o
menuLi st . set Adapt er ( adapt ) ;
o O o gc QuizMenuAcivit y.j ava file o5o5o oo c t_oc ooo
package com.androidbook.bt dt .hour8;
import android.cont ent.I nt ent;
import android.os.Bundle;
import android.view.View;
import android.widget .Adapt erView;
import android.widget .ArrayAdapt er;
import android.widget.List View;
import android.widget .Text View;
public class QuizMenuAct ivity extends QuizAct ivity {
/ * * Called when t he act ivit y is first creat ed. * /
@Override
public void onCreat e( Bundle savedI nst anceSt at e) {
super.onCreat e(savedI nst anceSt at e) ;
setContentView( R.layout .menu) ;
List View menuList = ( ListView) findViewByI d( R.id.List View_Menu) ;
St ring[ ] it ems = { get Resources() .get St ring(R.st ring.menu_it em_play),
get Resources( ) .get St ring(R.st ring.menu_it em_scores),
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
get Resources( ) .get St ring(R.st ring.menu_it em_set t ings),
get Resources( ) .get St ring(R.st ring.menu_it em_help) } ;
ArrayAdapt er< St ring> adapt = new ArrayAdapt er< St ring> (t his, R.layout .menu_it em, it ems);
menuList .set Adapt er( adapt ) ;
}
}
ooc_otgc f ile o save _o t run _o_ o splash screen _o tgc main menu screen
o5o5o oo c too`o5oo o oo
ooooo menu it em ooo click oogco5oo_o5cto5to5oooootot o_o5c to5to5c
o5oo5ooogtoo
3.2. List ening for List View Events
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
List View Oo oo it em o click oooooo5 list en oo_ot respond _oq ooooo list en ooo
set OnI t emClickList ener() method ootgoo respond _oqoooo5 onI t emClick() method o implement
o o gooo oo it em o click o o oo o o5 cc oot_o t oo o c g5 application activit y (screen) o launch
o o ootgo 5o
menuLi st . set OnI t emCl i ckLi st ener ( newAdapt er Vi ew. OnI t emCl i ckLi st ener ( ) {
publ i c voi d onI t emCl i ck( Adapt er Vi ew<?> par ent , Vi ew i t emCl i cked, i nt posi t i on, l ong
i d) {
/ / Not e: i f t he l i st was bui l t "by hand" t he i d coul d be used.
/ / As- i s, t hough, each i t emhas t he same i d
Text Vi ew t ext Vi ew =( Text Vi ew) i t emCl i cked;
St r i ng st rText = t ext Vi ew. get Text ( ) . t oSt r i ng( ) ;
i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_pl ay) ) ) {
/ / Launch t he Game Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s, Qui zGameAct i vi t y. cl ass) ) ;
} el se i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_hel p) ) ) {
/ / Launch t he Hel p Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s, Qui zHel pAct i vi t y. cl ass) ) ;
} el se i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_set t i ngs) ) ) {
/ / Launch t he Set t i ngs Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s,
Qui zSet t i ngsAct i vi t y. cl ass) ) ;
} el se i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_scor es) ) ) {
/ / Launch t he Scor es Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s, Qui zScor esAct i vi t y. cl ass) ) ;
}
}
}) ;
OnI t emClick() met hod o oo it em o click oooooo5oooooo inf ormat ion oo5toto pass
ooootooo QuizMenuAct ivit y.j ava file oo5oooct_ocoo
menuLi st . set OnI t emCl i ckLi st ener ( newAdapt er Vi ew. OnI t emCl i ckLi st ener ( ) {
publ i c voi d onI t emCl i ck( Adapt er Vi ew<?> par ent , Vi ew i t emCl i cked, i nt posi t i on, l ong
i d) {
/ / Not e: i f t he l i st was bui l t "by hand" t he i d coul d be used.
/ / As- i s, t hough, each i t emhas t he same i d
Text Vi ew t ext Vi ew =( Text Vi ew) i t emCl i cked;
St r i ng st rText = t ext Vi ew. get Text ( ) . t oSt r i ng( ) ;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_pl ay) ) ) {
/ / Launch t he Game Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s, Qui zGameAct i vi t y. cl ass) ) ;
} el se i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_hel p) ) ) {
/ / Launch t he Hel p Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s, Qui zHel pAct i vi t y. cl ass) ) ;
} el se i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_set t i ngs) ) ) {
/ / Launch t he Set t i ngs Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s,
Qui zSet t i ngsAct i vi t y. cl ass) ) ;
} el se i f
( st r Text . equal sI gnor eCase( get Resour ces( ) . get St r i ng( R. st r i ng. menu_i t em_scor es) ) ) {
/ / Launch t he Scor es Act i vi t y
st ar t Act i vi t y( newI nt ent ( Qui zMenuAct i vi t y. thi s, Qui zScor esAct i vi t y. cl ass) ) ;
}
}
}) ;
_o tgc run _o_ o menu it em oc o o click o o gc oo o c g5 screen o _oc gooo
3.3. Customizing List View Control Characteristics
List View o cust om divider and select ion pict ure ooqoocoo
3.3.1. Adding a Cust om Divider
Divider o List View it em oc o qoc o _o5to 5O5too divider at tribut e o color or drawable graphic
resource _oca coo color oooo oooogc it em ocoqoco_o5to5 horizont al line ooo
o c tO o oo o o o goo drawable graphic resource o o too o gc it em oo_o5to 5 graphic
oo`o5oo O tc ooo5 oo`o t it em gOo q o5o o t it em go5o o 5 divider oo`o 5oo o o t
List View cont rol Oo divider O_oo menu.xml file ooc_ot List View cont rol g divider at tribut e
o 5 @drawable/ divider o O_ o o o
<Li st Vi ew
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Li st Vi ew_Menu"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: di vi der ="@dr awabl e/ di vi der "
andr oi d: l ayout _cent er Hor i zont al ="t r ue"
andr oi d: l ayout _bel ow="@+i d/ Rel at i veLayout 01"></ Li st Vi ew>
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
3.3.2. Adding a Cust om Select or
List Oo 5g o it em ooqo oo it em o select ooO5toooo5o_oo ListView selector
ootgoo list Select or at t ribut e ootgoo def ault ooo5 ooo5og5coo5ooo5oootoo
Select or oO_o menu.xml f ile o oc _o t ListView control g listSelect or at t ribut e o
@drawable/ select or oooooo
<Li st Vi ew
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i d="@+i d/ Li st Vi ew_Menu"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: di vi der ="@dr awabl e/ di vi der "
andr oi d: l i st Sel ect or ="@dr awabl e/ sel ect or "
andr oi d: l ayout _cent er Hor i zont al ="t r ue"
andr oi d: l ayout _bel ow="@+i d/ Rel at i veLayout 01"></ Li st Vi ew>
_o tgc save _o t run _o_ o divider oo selector ooooooo
4. Working wit h Ot her Menu Types
Android o 5 menu type o _to _tg oo
Cont ext menus
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
View obj ect oco ooo`o5oqoo5_o5oO5tgc oo`o5ooo menu o cont ext menu
o oo`oo
Opt ions menus
Handset oo`o menu but t on o click oogcoo`o5o menu o Opt ions menu ooo`oo
4.1. Adding an Options Menu t o the Game Screen
Opt ion menu o o too o gc menu resources oogc O_ goo _o tgc QuizGameAct ivtiy class o 5
coding ogtgoo
4.1.1. Adding Menu Resources
/ res/ menu o5o o 5 gameopt ions.xml file o 5 menu resource ooO_ oo < menu> q < it em>
o o t ag _o ooo it em g at tribut e ooooo5
id
oo o c g5 opt ion menu it em o oo oo identif y o o a c o
t ilt e
option menu it em o 5oo5 _ooo st ring
icon
menu it em o o oo5 _ooo o
o applicat ion g opt ion menu o 5 option _o O_ oo Set tings and Help o oo_o5c
gameopt ions.xml file o5o5o oo c t_oc oo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<menu
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d">
<i t em
andr oi d: i d="@+i d/ set t i ngs_menu_i t em"
andr oi d: t i t l e="@st r i ng/ menu_i t em_set t i ngs"
andr oi d: i con="@andr oi d: dr awabl e/ i c_menu_pr ef er ences"></ i t em>
<i t em
andr oi d: i d="@+i d/ hel p_menu_i t em"
andr oi d: t i t l e="@st r i ng/ menu_i t em_hel p"
andr oi d: i con="@andr oi d: dr awabl e/ i c_menu_hel p"></ i t em>
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
</ menu>
4.1.2. Adding an Options Menu to an Activity
Opt ion menu o o _o tgc QuizGameAct ivit y class OO_ oo QuizGameAct ivit y class Oo 5
onCreat eOpt ionsMenu() met hod o implement oogoo
@Over r i de
publ i c bool ean onCr eat eOpt i onsMenu( Menu menu) {
super. onCr eat eOpt i onsMenu( menu) ;
get MenuI nf l at er ( ) . i nf l at e( R. menu. gameopt i ons, menu) ;
menu. f i ndI t em( R. i d. hel p_menu_i t em) . set I nt ent (
newI nt ent ( thi s, Qui zHel pAct i vi t y. cl ass) ) ;
menu. f i ndI t em( R. i d. set t i ngs_menu_i t em) . set I nt ent (
newI nt ent ( thi s, Qui zSet t i ngsAct i vi t y. cl ass) ) ;
return true;
}
4.1.3. Handling Options Menu Selections
User o oo opt ion menu it em oogtoooooo5ccooo onOpt ionI t emSelected() met hod
o o tgoo user select o o o o o menu it em Oooq int ent o o _o t oo o c g5 act ivit y ( screen) o lunch
(display) o o goo
@Over r i de
publ i c bool ean onOpt i onsI t emSel ect ed( MenuI t emi t em) {
super. onOpt i onsI t emSel ect ed( i t em) ;
st ar t Act i vi t y( i t em. get I nt ent ( ) ) ;
return true;
}
QuizGameActivit y.j ava file g complet e code o o5o oo c t_oc oo
package com.androidbook.bt dt .hour8;
import android.cont ent.I nt ent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuI tem;
public class QuizGameAct ivit y extends QuizAct ivit y {
/ * * Called when t he act ivit y is first creat ed. * /
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
@Override
public void onCreat e( Bundle savedI nst anceSt at e) {
super.onCreat e(savedI nst anceSt at e) ;
set Cont ent View( R.layout .game) ;
}
@Override
public boolean onCreat eOpt ionsMenu( Menu menu) {
super.onCreat eOpt ionsMenu( menu) ;
get MenuI nf lat er( ) .inf late(R.menu.gameopt ions, menu) ;
menu.findI t em(R.id.help_menu_it em) .setI ntent(
new I nt ent ( t his, QuizHelpActivit y.class));
menu.findI t em(R.id.sett ings_menu_item) .setI ntent(
new I nt ent ( t his, QuizSet t ingsAct ivit y.class)) ;
ret urn t rue;
}
@Override
public boolean onOpt ionsI t emSelect ed( MenuI t em it em) {
super.onOpt ionsI t emSelect ed(it em) ;
st art Act ivit y(it em.get I nt ent ( ));
ret urn t rue;
}
}
_o tgc oo5 save _o t run o o5o oo o _toooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Example proj ect code oo_tO_ ootO5tooo Hour8 o
Andorid Applicat ion ogtq_t ( oo)
ogo5_oto o5oogoooq5 oocooo5 Help q Scores Screens oo o o ooo
1. Designing and implement ing t he help screen
2. Working wit h f iles
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
3. Designing and implement ing t he scores screen
4. Designing screens wit h t abs
5. Working wit h XML
1. Designing and I mplement ing the Help Screen
1.1. Designing t he Help Screen
o screen o 5 t ext ooo display o o _oo 5o scroll o o _o t_o_ a c o5c o_ to o ootgoo
o_ooto o5oo5_o_o
Lay out design o o5o o 5_o_ o
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
1.2. I mplement ing t he Help Screen Layout
Proj ect Oo new resources ooOo O_ goo help.xml f ile o o_ t _oc goo
1.2.1. Adding New Proj ect Resources
Help screen layout o 5o t_o_oo st ring, color, dimension resources ooo5oo t ext file
oc o o_ to oo help screen o main Text View cont rol o5 display o o _ooo t ext ooo
quizhelp.t xt file o 5ogtO5to _oo5
Have you been t her e?
Have you done t hat ?
BEEN THERE, DONE THAT! i s a t r i vi a qui z dedi cat ed t o t hose who l ove t ravel and advent ur e. By
answer i ng a seri es of Yes or No quest i ons, you gai n poi nt s t owar d bei ng t he most wel l - t ravel ed
and f i el d- t est ed per son on t he pl anet . Shar e your exper i ences and compet e wi t h your f r i ends.
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
GAME SETTI NGS
<TBD>
SUPPORT I NFORMATI ON
"Been Ther e, Done That ! " was devel oped by Maml ambo.
Phone: 1- 800- 555- 1212
Emai l : suppor t @maml ambo. com
Websi t e: ht t p: / / www. maml ambo. com
Addr ess: 1060 West Addi son St r eet , Chi cago, I L 60613 ( Par don t he Bl ues Br ot her s r ef er ence)
COPYRI GHT ( c) 2011, Maml ambo.
Al l r i ght s r eser ved.
Oo oo c togtO5to _o tgc quizhelp.txt file o5 res/ raw/ o5o o 5g oqgooo
1.2.2. Updat ing t he Help Screen Layout
res/ layout o5o o help.xml f ile o5 Help screen g layout o oq o tootoo file o help.xml file o 5
o5o oo c t xml code ooogtgooo
LinearLayout ocoO__ot og background att ribut e ooo5 @dr awabl e/ bkgr nd
o O5tooo
oo5oo5 Relat iveLayout ocoOoO_ ooo Relat ive layout o5 I mageView cont rol
_o q Text View oc o oooo Text View cont rol g t ext at t ribut e o 5 @st ring/ help
o O5tooo
Relat iveLayout cont rol g_oc o 5 Text View cont rol oc o Oo O_ oo o g id o
Text View_HelpText o O5to linksClickable at t ribut e o t rue O5t_o t aut oLink at t ribut e o
all O5t_oc t_oc phone numbers, web addt esses, email addresses and post al addresses
oo o aut o link ooootac ooo t ext St yle at t ribut e o ot_o t bold or it alic
ooacoootoo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<Li nearLayout
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: or i ent at i on="ver t i cal "
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_parent "
androi d: background="@dr awabl e/ bkgr nd">
<Rel ati veLayout
andr oi d: i d="@+i d/ Rel at i veLayout 01"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent ">
<I mageVi ew
andr oi d: i d="@+i d/ I mageVi ew_Header "
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _al i gnPar ent Lef t ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
<TextVi ew
andr oi d: i d="@+i d/ Text Vi ew01"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
androi d: text="@st r i ng/ hel p"
andr oi d: t ext Si ze="@di men/ scr een_t i t l e_si ze"
andr oi d: shadowDx="0"
andr oi d: shadowDy="0"
andr oi d: shadowRadi us="10"
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _gr avi t y="f i l l _hori zont al | cent er "
andr oi d: l ayout _al i gnPar ent Top="t r ue"
andr oi d: l ayout _cent er Hori zont al ="t r ue"
andr oi d: shadowCol or ="@andr oi d: col or / whi t e"
andr oi d: t ext Col or ="@col or / t i t l e_col or "></ Text Vi ew>
<I mageVi ew
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ res/ andr oi d"
andr oi d: i d="@+i d/ I mageVi ew_Header 2"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _gr avi t y="r i ght | cent er _ver t i cal "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _al i gnPar ent Ri ght ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
</ Rel at i veLayout >
<TextVi ew
androi d: i d="@+i d/ Text Vi ew_Hel pText "
andr oi d: t ext Col or ="@col or / t i t l e_col or "
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_par ent "
androi d: autoLi nk="al l "
andr oi d: i sScr ol l Cont ai ner ="t r ue"
androi d: textStyl e="i t al i c"
andr oi d: dr awabl ePaddi ng="@di men/ hel p_t ext _dr awabl e_paddi ng"
andr oi d: t ext Col or Li nk="@col or / l ogo_col or "
androi d: l i nksCl i ckabl e="t r ue"
andr oi d: f adi ngEdgeLengt h="@di men/ hel p_t ext _f adi ng"
andr oi d: f adi ngEdge="ver t i cal "
andr oi d: scr ol l bar s="ver t i cal "
andr oi d: paddi ng="@di men/ hel p_t ext _paddi ng"
andr oi d: t ext Si ze="@di men/ hel p_t ext _si ze"
andr oi d: scr ol l bar St yl e="out si deOver l ay"
andr oi d: buf f er Type="spannabl e"></ Text Vi ew>
</ Li near Layout >
2. Working wit h Files
o o oc ooo5 quizhelp.t xt file o ooo QuizHelpAct ivit y.j ava file o _oc ooo quizhelp.t xt
file ooo_ot Text View_HelpText oo Text View cont rol o5 display oo_ogo5o
2.1. Accessing Raw File Resources
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
quizhelp.t xt file o 5ogtO5to dat a o oo o openRawResource() met hod o o tooo
I nput St r eami Fi l e = get Resour ces( ) . openRawResour ce( R. r aw. qui zhel p) ;
o o I nput St ream obj ect oc o go_o o c o lieny-by-line or byt e-by-byt e oo _o t st ring o
creat e ooa co_o o5ooq_tootoctooooo
Text Vi ew hel pText = ( Text Vi ew) f i ndVi ewByI d( R. i d. Text Vi ew_Hel pText ) ;
St ri ng st r Fi l e = i nput St r eamToSt r i ng( i Fi l e) ;
hel pText . set Text ( st r Fi l e) ;
QuizHelpAct ivit y.j ava file g code _o_ c oo5o oo c to
package com.androidbook.bt dt .hour9;
import j ava.io.Dat aI nput St ream;
import j ava.io.I OException;
import j ava.io.I nput St ream;
import android.os.Bundle;
import android.ut il.Log;
import android.widget .Text View;
public class QuizHelpAct ivity extends QuizAct ivity {
/ * * Called when t he act ivit y is first creat ed. * /
@Override
public void onCreat e( Bundle savedI nst anceSt at e) {
super.onCreat e(savedI nst anceSt at e) ;
set Cont ent View( R.layout .help) ;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
/ / Read raw f ile int o st ring and populat e Text View
I nput St ream iFile = get Resources() .openRawResource(R.raw.quizhelp) ;
t ry {
Text View helpText = (Text View) f indViewByI d( R.id.Text View_HelpText );
St ring st rFile = input St reamToString( iFile);
helpText .set Text ( st rFile) ;
} cat ch ( Except ion e) {
Log.e( DEBUG_TAG, "I nput StreamToSt ring failure", e) ;
}
}
/ * *
* Convert s an input st ream t o a st ring
*
* @param is
* The { @code I nput St ream} obj ect t o read f rom
* @ret urn A { @code St ring} obj ect represent ing t he st ring for of t he input
* @t hrows I OExcept ion
* Thrown on read f ailure f rom t he input
* /
public String inputSt reamToSt ring( I nput Stream is) t hrows I OExcept ion {
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
St ringBuf f er sBuffer = new St ringBuffer() ;
Dat aI nput St ream dat aI O = new Dat aI nput St ream(is) ;
St ring st rLine = null;
while ( (st rLine = dat aI O.readLine( ) ) ! = null) {
sBuf f er.append(st rLine + "\ n") ;
}
dat aI O.close( ) ;
is.close( ) ;
ret urn sBuf f er.t oSt ring();
}
}
_o tgc oo5 save o o _o t run _o_ o splash screen oo o5_o toq5o main menu ooq help screen
option o ogto o o help screen c5o5oooctoo`o5ooooo quizhelp.txt f ile
o5ogtO5tooctoogooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Scores screen _o_o o q_ to oo ogtooto5tooo
3. Designing and I mplement ing the Scores Screen
3.1. Designing t he Scores Screen
o screen o5oo5 user o scores ooO o _oo 5o score _o_tgooo all-t ime-high scores
and users f riends scores _oc ooo screen o5 2o _to t_ogo5co users name, score and overall
ranking ooo_o_ooo
Score informat ion ooo _oo Text View cont rol or List View cont rol _o _o g5o ta coo
o5o oo o c t_oc o5c o o _o_ oo _oo too
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
3.1.1. Det ermining Scores Screen Layout Requirement s
/ res/ layout / scores.xml layout file o _oc goo screen gOoo t it le bar oo Relat ive
layout ootoo _otgc TabHost cont rol o5oo oo5 t ab _ogoo t ab ocoo all scores
oo _oc _o t oq oc oooo5 friends scores oo o too
3.1.2. Adding the TabHost Cont rol
o5o oo o c t_oc goo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
3.2. I mplement ing t he Scores Screen Layout
Oo o 5o o o o design o implement o o o oo resources ooOo O_ oo _o tgc
scores.xml file o _oc oo
3.2.1. Adding New Proj ect Resources
oq5o o c to 5oo5 scores ooo remot e server ooqo _ooo o oo5oo5oo oo5 server
oo tooto xml file o 5o ogtO5t_ too
/ res/ xml/ o5o o 5 allscores.xml and friendscores.xml file oog goo allscores.xml o 5
o5o oo c t scores ooO_ O5to
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<! - -
Thi s i s a mock scor e XML chunk
- - >
<scor es>
<scor e
user name="LED"
scor e="12345"
r ank="1" / >
<scor e
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
user name="SAC"
scor e="12344"
r ank="2" / >
<scor e
user name="NAD"
scor e="12339"
r ank="3" / >
<scor e
user name="ASH"
scor e="12335"
r ank="4" / >
<scor e
user name="KPM"
scor e="12245"
r ank="5" / >
<scor e
user name="SDB"
scor e="12222"
r ank="6" / >
<scor e
user name="LOL"
scor e="11667"
r ank="7" / >
<scor e
user name="AFK"
scor e="11345"
r ank="8" / >
<scor e
user name="BRB"
scor e="11234"
r ank="9" / >
<scor e
user name="BUG"
scor e="10000"
r ank="10" / >
<scor e
user name="LED"
scor e="1234"
r ank="11" / >
<scor e
user name="SAC"
scor e="123"
r ank="12" / >
<scor e
user name="NAD"
scor e="122"
r ank="13" / >
<scor e
user name="ASH"
scor e="121"
r ank="14" / >
<scor e
user name="KPM"
scor e="21"
r ank="15" / >
<scor e
user name="SDB"
scor e="12"
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
r ank="16" / >
<scor e
user name="LOL"
scor e="11"
r ank="17" / >
<scor e
user name="AFK"
scor e="5"
r ank="18" / >
<scor e
user name="BRB"
scor e="3"
r ank="19" / >
<scor e
user name="BUG"
scor e="1"
r ank="20" / >
</ scor es>
friendscores.xml file o 5oo5 o5o oo c tO5to
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<! - -
Thi s i s a mock scor e XML chunk
- - >
<scor es>
<scor e
user name="BAG"
scor e="12345"
r ank="101" / >
<scor e
user name="BET"
scor e="12344"
r ank="201" / >
<scor e
user name="SI M"
scor e="12339"
r ank="322" / >
<scor e
user name="TUG"
scor e="12335"
r ank="444" / >
<scor e
user name="RUG"
scor e="12245"
r ank="555" / >
<scor e
user name="YUK"
scor e="12222"
r ank="666" / >
<scor e
user name="YAK"
scor e="11667"
r ank="777" / >
<scor e
user name="WAH"
scor e="11345"
r ank="888" / >
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
<scor e
user name="FOG"
scor e="11234"
r ank="999" / >
<scor e
user name="LUG"
scor e="10000"
r ank="1001" / >
</ scor es>
< scores> element oc o c o 5 child < score> element ootoog oo element oc o c o 5 at t ribut e
go c g oo username, score and rank oo
3.2.2. Updat ing the Scores Screen Layout
Scores screen user int erface o scores.xml layout file o 5oo o o ooo
(ooqg5o5ooo5tgo5o TabHost cont rols ooo design mode o5_o_ooco_oo5ooooot
oo_o5c XML layout mode o5o_occg5go5_ocgooo t ab ooo_o_oo emulat or o device
o o 5 run _o_ o _oc go 5_oc ooo )
Layout file o updat e oooogc
qo g _o to5t cont rol ooo oo oc o
LinearLayout cont rol ocoO_o android: background at tribut e o @drawable/ bkgrnd
o O5to o LinearLayout cont rol o5o o 5oq o cont rol ooo Oo O_ ooo
Relat iveLayout cont rol oO_ooo oo5 I mageView _oq Text View ocooooo Text View
cont rol g t ext at t ribut e o @st ring/ scores oO5tooto
Relat iveLayout cont rol g_oco5 TabHost cont rol ocoO_ooo o_ooo5 TabHost 1
oo og layout _widt h and layout _height at t ribut e oooooooootgooo
TabHost cont rol goc to 5 LinearLayout cont rol oc o O_ ooo _o tgc o g orient at ion
at t ribut e o vert ical O5tooo layout _widt h and layout _height o o_ too o o ooo
ot LinearLayout cont rol Oo5 TabWidget cont rol ocoO_ooo o cont rol g id at t ribute o
@android: id/ t abs oO5tooo
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oq5o Oo FrameLayout cont rol oc o Oo O_ ooo o g id o oo5 @android: id/ t abcont ent
o oo o o ooo _o tgc layout _widt h and layout _height ooo oo o o ooo
FrameLayout cont rol o5 TableLayout cont rol _oOoO_ooo Tab ocooo TableLayout
cont rol ocoaqto Tab o _oooo5 TableLayout cont rol _oO_go5o o TableLayout
cont rol ooo scores ooo display o o _oo o to5o oOo TableLayout cont rol o
TableLayout _AllScores o o_ oot_o t o o o oo5c o oo5 TableLayout _FriendScores
o ooto _o tgc layout _widt h and layout _height ooo oo o o o oo5 _ooo scores
informat ion q_to5too`oo__ot t able g column o resize ooaco5c st ret chColumns
at t ribut e o * oO5tooto
res/ layout/ xml/ scores.xml file g content oo5o oo c t_oc gooo
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<Li nearLayout
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
andr oi d: or i ent at i on="ver t i cal "
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_parent "
androi d: background="@dr awabl e/ bkgr nd">
<Rel ati veLayout
andr oi d: i d="@+i d/ Rel at i veLayout 01"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent ">
<I mageVi ew
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ res/ andr oi d"
andr oi d: i d="@+i d/ I mageVi ew_Header "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _al i gnPar ent Lef t ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
<TextVi ew
andr oi d: i d="@+i d/ Text Vi ew01"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
androi d: text="@st r i ng/ scor es"
andr oi d: t ext Si ze="@di men/ scr een_t i t l e_si ze"
andr oi d: shadowDx="0"
andr oi d: shadowDy="0"
andr oi d: shadowRadi us="10"
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _gr avi t y="f i l l _hori zont al | cent er "
andr oi d: l ayout _al i gnPar ent Top="t r ue"
andr oi d: l ayout _cent er Hori zont al ="t r ue"
andr oi d: shadowCol or ="@andr oi d: col or / whi t e"
andr oi d: t ext Col or ="@col or / t i t l e_col or "></ Text Vi ew>
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
<I mageVi ew
xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ res/ andr oi d"
andr oi d: i d="@+i d/ I mageVi ew_Header 2"
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: sr c="@dr awabl e/ i con"
andr oi d: l ayout _gr avi t y="r i ght | cent er _ver t i cal "
andr oi d: l ayout _wi dt h="wrap_cont ent "
andr oi d: l ayout _al i gnPar ent Ri ght ="t r ue"
andr oi d: l ayout _al i gnPar ent Top="t r ue"></ I mageVi ew>
</ Rel at i veLayout >
<TabHost
androi d: i d="@+i d/ TabHost 1"
androi d: l ayout_wi dth="mat ch_par ent "
androi d: l ayout_hei ght="mat ch_par ent ">
<Li nearLayout
androi d: ori entati on="vert i cal "
androi d: l ayout_wi dth="mat ch_par ent "
androi d: l ayout_hei ght="mat ch_parent ">
<TabWi dget
androi d: i d="@andr oi d: i d/ t abs"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent " / >
<FrameLayout
androi d: i d="@andr oi d: i d/ t abcont ent "
androi d: l ayout_wi dth="mat ch_par ent "
androi d: l ayout_hei ght="mat ch_par ent ">
<Scr ol l Vi ew
andr oi d: i d="@+i d/ Scr ol l Vi ewAl l Scor es"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_par ent "
andr oi d: scr ol l bar s="ver t i cal ">
<Tabl eLayout
androi d: i d="@+i d/ Tabl eLayout _Al l Scor es"
androi d: l ayout_wi dth="mat ch_par ent "
androi d: l ayout_hei ght="mat ch_par ent "
androi d: stretchCol umns="*">
</ Tabl eLayout >
</ Scr ol l Vi ew>
<Scr ol l Vi ew
andr oi d: i d="@+i d/ Scr ol l Vi ewFr i endScor es"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_par ent "
andr oi d: scr ol l bar s="ver t i cal ">
<Tabl eLayout
androi d: i d="@+i d/ Tabl eLayout _Fr i endScor es"
androi d: l ayout_wi dth="mat ch_par ent "
androi d: l ayout_hei ght="mat ch_par ent "
androi d: stretchCol umns="*"></ Tabl eLayout >
</ Scr ol l Vi ew>
</ Fr ameLayout >
</ Li near Layout >
</ TabHost >
</ Li near Layout >
4. Designing Screens wit h Tabs
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
oo oc ooo5 QuizScoresAcit ivit y.j ava file o 5 code ogtgo5c gc o t Tabhost cont rol o
init ialize o o gooo _o tgc t ab a c o q_o_ goo All Scores t ab o default t ab _oc O5too
4.1. Configuring t he TablHost Cont rol
TabHost cont rol o findViewByI d() met hod qret irve o o ooo _o tgc TabHost cont rol g
set up() met hod o call ooo
/ / Set up t he t abs
TabHost host = ( TabHost ) f i ndVi ewByI d( R. i d. TabHost 1) ;
host . set up( ) ;
4.2. Adding Tabs t o the TabHost Control
TabHost cont rol o ret rieve o o _o t init ialize o o _o t_o o oo5 t ab oc o oc tc o configure
oooo5oo addTab() met hod qoooo All scores t ab o creat e oooo5oo code
oo ogtgooo
/ / Al l Scor es t ab
TabSpec al l Scor esTab = host . newTabSpec( "al l Tab") ;
al l ScoresTab. set I ndi cat or ( get Resour ces( ) . get St r i ng( R. st r i ng. al l _scor es) ,
get Resour ces( ) . get Dr awabl e(
andr oi d. R. dr awabl e. st ar _on) ) ;
al l ScoresTab. set Cont ent ( R. i d. Scr ol l Vi ewAl l Scor es) ;
host . addTab( al l Scor esTab) ;
Friends scores t ab ooooo5o5oo code oooo
/ / Fr i ends Scores t ab
TabSpec f r i endScoresTab = host . newTabSpec( "f r i endsTab") ;
f r i endScor esTab. set I ndi cat or( get Resour ces( ) . get St r i ng( R. st r i ng. f r i ends_scor es) ,
get Resour ces( ) . get Dr awabl e(
andr oi d. R. dr awabl e. st ar _on) ) ;
f r i endScor esTab. set Cont ent ( R. i d. Scr ol l Vi ewFr i endScor es) ;
host . addTab( f r i endScor esTab) ;
4.3. Set t ing t he Default Tab
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Default oqq oo t ab o O5too_ t set CurrentTabByTag() met hod o call goo _o tgc t ab g
t ag name o 5 pass o o ootgoo
/ / Set t he def aul t t ab
host . set Cur r ent TabByTag( "al l Tab") ;
_o tgc oo5 save o o _o t run o scores screen o 5 t ab _o oooo ooooo display o o _oo scores
data go5oo5oooootot
5. Working wit h XML
Android plat form o5 XML dat a ooq ooooo mechanism ooo5t_otg ooo _oo5
SAX (Simple API f or XML)
XML Pull Parser
Limit ed DOM Level 2 core support
oo XML t echnology ootoooo5ooo5 ooocg5 proj ect oo`oo_ooo o t ut orial o5oo5
simple XML file ooq oo o oo
5.1. Ret rieving XML Resources
Proj ect resurces o5 save ooO5to XML dat a o gcot oogooo
(/ res/ xml/ allscores.xml) get XML() met hod o o t_o t XmlResourceParser g inst ance o init ialize
ooacooo
Xml Resour cePar ser mockAl l Scor es = get Resour ces( ) . get Xml ( R. xml . al l scor es) ;
5.2. Parsing XML Files wit h XmlResourceParser
< score> t ag oc o c o g 5_o t o g username, rank and score at t ribut es ooo ext ract
o o o 5o next () met hod o o t_o t while () loop qoo ooo
i nt event Type =- 1;
bool ean bFoundScores = fal se;
/ / Fi nd Scor e r ecor ds f r omXML
whi l e ( event Type ! = Xml Resour cePar ser . END_DOCUMENT) {
i f ( event Type == Xml Resour cePar ser . START_TAG) {
/ / Get t he name of t he t ag ( eg scor es or scor e)
St r i ng st rName = scor es. get Name( ) ;
i f ( st r Name. equal s( "scor e") ) {
bFoundScor es = true;
St r i ng scor eVal ue = scores. get At t r i but eVal ue( nul l , "scor e") ;
St r i ng scor eRank = scor es. get At t r i but eVal ue( nul l , "r ank") ;
St r i ng scor eUserName = scor es. get At t r i but eVal ue( nul l , "user name") ;
i nser t Scor eRow( scor eTabl e, scor eVal ue, scor eRank, scor eUser Name) ;
}
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
}
event Type = scor es. next ( ) ;
}
Loop Oo 5 START_TAG event o _o_ oo t ag name o5 < score> tag q mat ch _oc gc score dat a
goooo5oacoo _otgc get At t ribut eValue() met hod oot_ot dat a o ext ract ooooo ooo score
ocooctcoo TableLayout cont rol Oo5 TableRow cont rol oOoO_oo
QuizScoresAcitivit y.j ava file g code _o_ c oo5o oo c t_oc oo
package com.androidbook.bt dt .hour9;
import j ava.io.I OException;
import org.xmlpull.v1.XmlPullParserExcept ion;
import android.content .res.XmlResourceParser;
import android.os.Bundle;
import android.ut il.Log;
import android.widget .TabHost ;
import android.widget .TabHost .TabSpec;
import android.widget .TableLayout ;
import android.widget .TableRow;
import android.widget .Text View;
public class QuizScoresAct ivity extends QuizAct ivity {
/ * * Called when t he act ivit y is first creat ed. * /
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
@Override
public void onCreat e( Bundle savedI nst anceSt at e) {
super.onCreat e(savedI nst anceSt at e) ;
set Cont ent View( R.layout .scores) ;
/ / Set up t he t abs
TabHost host = (TabHost ) f indViewByI d(R.id.TabHost 1) ;
host .set up( ) ;
/ / All Scores t ab
TabSpec allScoresTab = host .newTabSpec( "allTab") ;
allScoresTab.set I ndicat or(get Resources().get St ring(R.st ring.all_scores), getResources().get Drawable(
android.R.drawable.st ar_on));
allScoresTab.set Cont ent (R.id.ScrollViewAllScores) ;
host .addTab( allScoresTab) ;
/ / Friends Scores t ab
TabSpec f riendScoresTab = host .newTabSpec( "f riendsTab") ;
f riendScoresTab.setI ndicator( getResources().getSt ring(R.string.f riends_scores) ,
get Resources( ).get Drawable(
android.R.drawable.st ar_on));
friendScoresTab.set Cont ent (R.id.ScrollViewFriendScores) ;
host .addTab( f riendScoresTab) ;
/ / Set t he def ault t ab
host .set Current TabByTag( "allTab") ;
/ / Ret rieve t he TableLayout ref erences
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
TableLayout allScoresTable = ( TableLayout) f indViewByI d(R.id.TableLayout_AllScores) ;
TableLayout f riendScoresTable = ( TableLayout ) f indViewByI d( R.id.TableLayout _FriendScores) ;
/ / Give each TableLayout a yellow header row wit h t he column names
init ializeHeaderRow( allScoresTable) ;
init ializeHeaderRow(friendScoresTable) ;
XmlResourceParser mockAllScores = get Resources( ).get Xml(R.xml.allscores) ;
XmlResourceParser mockFriendScores = get Resources() .get Xml(R.xml.friendscores) ;
t ry {
processScores(allScoresTable, mockAllScores);
processScores(friendScoresTable, mockFriendScores);
} cat ch ( Except ion e) {
Log.e( DEBUG_TAG, "Failed t o load scores", e) ;
}
}
/ * *
* Add a header { @code TableRow} t o t he { @code TableLayout } ( st yled)
*
* @param scoreTable
* t he { @code TableLayout } t hat t he header row will be added t o
* /
private void initializeHeaderRow( TableLayout scoreTable) {
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
/ / Creat e t he Table header row
TableRow headerRow = new TableRow( this);
int t ext Color = get Resources( ).get Color(R.color.logo_color) ;
f loat t ext Size = get Resources() .get Dimension( R.dimen.help_t ext _size);
addText ToRowWit hValues(headerRow, get Resources().get St ring(R.st ring.username), t ext Color,
t ext Size) ;
addText ToRowWit hValues( headerRow, get Resources().get St ring(R.st ring.score) , t ext Color, t ext Size);
addText ToRowWit hValues( headerRow, get Resources().get St ring(R.st ring.rank), t ext Color, t ext Size);
scoreTable.addView( headerRow);
}
/ * *
* Churn t hrough an XML score inf ormat ion and populat e a { @code TableLayout }
*
* @param scoreTable
* The { @code TableLayout } t o populat e
* @param scores
* A st andard { @code XmlResourceParser} cont aining t he scores
* @t hrows XmlPullParserExcept ion
* Thrown on XML errors
* @t hrows I OExcept ion
* Thrown on I O errors reading t he XML
* /
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
privat e void processScores(f inal TableLayout scoreTable, XmlResourceParser scores) t hrows
XmlPullParserExcept ion,
I OExcept ion {
int event Type = -1;
boolean bFoundScores = false;
/ / Find Score records from XML
while (event Type ! = XmlResourceParser.END_DOCUMENT) {
if ( event Type = = XmlResourceParser.START_TAG) {
/ / Get t he name of t he t ag (eg scores or score)
St ring st rName = scores.get Name();
if ( st rName.equals( "score") ) {
bFoundScores = t rue;
St ring scoreValue = scores.get At t ribut eValue( null, "score") ;
St ring scoreRank = scores.get At t ribut eValue( null, "rank") ;
St ring scoreUserName = scores.get At t ribut eValue(null, "username");
insert ScoreRow(scoreTable, scoreValue, scoreRank, scoreUserName) ;
}
}
event Type = scores.next( ) ;
}
/ / Handle no scores available
if ( bFoundScores = = false) {
f inal TableRow newRow = new TableRow(t his) ;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Text View noResult s = new Text View( t his);
noResult s.set Text ( get Resources() .get St ring(R.st ring.no_scores));
newRow.addView( noResult s) ;
scoreTable.addView( newRow);
}
}
/ * *
* { @code processScores()} helper met hod -- I nserts a new score { @code
* TableRow} in t he { @code TableLayout }
*
* @param scoreTable
* The { @code TableLayout } t o add t he score t o
* @param scoreValue
* The value of t he score
* @param scoreRank
* The ranking of t he score
* @param scoreUserName
* The user who made t he score
* /
privat e void insert ScoreRow(f inal TableLayout scoreTable, St ring scoreValue, St ring scoreRank, St ring
scoreUserName) {
f inal TableRow newRow = new TableRow( t his) ;
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
int text Color = getResources( ).getColor(R.color.t itle_color) ;
f loat t ext Size = get Resources() .get Dimension( R.dimen.help_t ext _size);
addText ToRowWit hValues(newRow, scoreUserName, t ext Color, t ext Size) ;
addText ToRowWit hValues(newRow, scoreValue, t ext Color, t ext Size);
addText ToRowWit hValues(newRow, scoreRank, t ext Color, t ext Size);
scoreTable.addView( newRow);
}
/ * *
* { @code insert ScoreRow() } helper met hod -- Populat e a { @code TableRow} wit h
* t hree columns of { @code Text View} dat a ( st yled)
*
* @param t ableRow
* The { @code TableRow} t he t ext is being added t o
* @param t ext
* The t ext t o add
* @param t ext Color
* The color t o make t he t ext
* @param t ext Size
* The size t o make t he t ext
* /
privat e void addText ToRowWit hValues(final TableRow t ableRow, St ring t ext , int t ext Color, float t ext Size)
{
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintun
www.facebook.com/groups/htarhtarlwin
Text View t ext View = new Text View( t his) ;
t ext View.set Text Size(t ext Size);
t ext View.set Text Color( t ext Color) ;
t ext View.set Text ( t ext ) ;
t ableRow.addView( t ext View) ;
}
}
5.3. Applying Finishing Touches t o t he Scores Screen
Run _o_ o scores screen oo5tgc o5oooct t ab _o o scores dat a
oo qoo oo go 5_oc ooo oo o_ too
Complet e source code o oo5oo
ht t p: / / www.mediafire.com/ ?lm1cbc6s6d53uay

You might also like