mad master manual (1)
TRANSCRIPT
-
8/10/2019 Mad Master Manual (1)
1/68
Department
Of
Computer Science and Engineering
MOBILE APPLICATION DEVELOPMENT
LABORATORY MANAL
AVANTHI INSTITUTE OF ENGINEERING & TECHNOLOGY
Cherukupalli(V), Near Tagarapuvalasa Bridge, Bhogapuram(M),Vizianagaram(Dist).
0!""#$$%&"!, '''.avanthi.edu.in, '''.avanthiportal.om,
mail*hodse.avanthi+gmail.om
DEPARTMENT O! COMPTER SCIENCE AND EN"INEERIN"
http://www.avanthi.edu.in/http://www.avanthiportal.com/http://www.avanthi.edu.in/http://www.avanthiportal.com/ -
8/10/2019 Mad Master Manual (1)
2/68
# Cour$e Tit%e& MAD
' C%a$$& B(Tec) IV Year I Sem
* Branc)& CSE
+ Regu%ation& R#,
- No( of .our$/0ee1& *
CORSE OB2ECTIVES&
To impart fundamental concepts in the area of mobile computing.
To provide a computer systems perspective on the converging areas of wireless
networking, embedded systems, and software, and to introduce selected topics of currentresearch interest in the field.
To impart knowledge in wireless and mobile computing and in their applications.
CORSE OTCOME&
Understanding the systems, protocols and mechanisms to support mobility for mobile
internet user.
Develop networked applications for mobile devices.
Understanding the hardware necessary to support mobility and computation
Understanding the possible applications of mobility to computation Design and implement a novel mobile computing application
E3IPMENT/SO!T0ARE RE3IRED&
1. JDK and Java ireless Toolkit must be installed on your system
!. "ntel based desktop #$ with minimum of 1%%&'(
). *o. of +ystems reuired %- per sess
Prepared 45& Mr( 2(V(6IRAN .(O(D
INDE7
-
8/10/2019 Mad Master Manual (1)
3/68
S%(No Name of t)e Program Page(No
1 rite a J!& application that shows how to change font si/e and color. 11
!
rite a J!& program which creates following kind of menu
o $ut
o $opyo #ast
o Delete
o +elect all
o Unselect all
10
)
rite J!& program which creates following kind of
menuvent 'andling2
o $ut can be on3off
o $opy can be on3off
o #ast can be on3off
o Delete can be on3off
o +elect all #ut all four 4ptions on
o Unselect all #ut all four 4ptions off
1%
0$reate &"D# application which draws a bar graph to the displaydata values can be given at int2array. 5ou can enter four data
integers values to the input te6t field
17
8
$reate an &"D# application which e6amine that a phone number
which a user has entered is in the given format9"nput $hecking: ;rea code should be one of the following
-0-,-01,-8-,-0--,-00
There should % area code:
!!
% rite a sample program to show how to make a socket connectionfrom J!& phone
!8
?;uthentication with a eb +erver@ogin to 'TT# +erver from a J!& #rogram
)1
=
$reate a &"D# application, which show to the user 8
-
8/10/2019 Mad Master Manual (1)
4/68
10rite an android application that shows how to use intents in mobile
application development%0
-
8/10/2019 Mad Master Manual (1)
5/68
Mobile Application Development Laboratory
Manual
Introduction to 2'ME
In$ta%%ation of 2a8a 0ire%e$$ Too%1it 92'ME:
1: "f the Java Development Kit 9JDK: is not thereor only having the Java Buntime
nvironment 9JB: installed, install the latest JDK fromhttpC33ava.sun.com3avase3downloads3inde6.sp.
.
!: *e6t, download the 2a8a 0ire%e$$ Too%1it 9formerly called J!& ireless Toolkit:fromC httpC33ava.sun.com3products3swtoolkit3download.html.
): Bun the installer 9for e6ample, for indows it isC sunEavaEwirelessEtoolkit< !E8E! 1-Hg.setAont9f!:H
33 g.draw@ine9-, f1.get'eight9: > y#os < 1, width, f1.get'eight9: > y#os < 1:Hg.draw+tring9&D"U& A4*T, -, y#os, Mraphics.@AT O Mraphics.T4#:H
g.set$olor9F@;$K:H
33g.draw@ine9-, f!.get'eight9: > y#os < 1, width, f!.get'eight9: > y#os < 1:H
y#os y#os > f1.get'eight9: > 1-H
g.setAont9f):H
g.draw+tring9+&;@@ A4*T, -, y#os, Mraphics.@AT O Mraphics.T4#:Hy#os y#os > f1.get'eight9: > 1-H
g.draw@ine9-, f).get'eight9: > y#os < 1, width, f).get'eight9: > y#os < 1:H
painting falseH
L
L
L
Output&
$
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
17/68
Mobile Application Development Laboratory
Manual
E?erci$e '&
0rite a 2'ME program ;)ic) create$ fo%%o;ing 1ind of menu
o Cut
o Cop5
o Pa$to De%ete
o Se%ect a%%
o n$e%ect a%%
importava6.microedition.midlet.GH
importava6.microedition.lcdui.GH
pu4%icc%a$$&enuvents e?tend$&"Dlet I
pu4%ic$hoiceMroup chH
pu4%ic$hoiceMroup ch1H
pu4%icAorm formH
pu4%icAorm form1Hpu4%icDisplay displayH
pu4%ic$ommand PiewH
pu4%ic$ommand 6itH
pu4%ic$ommand FackH
pu4%ic+tring"tem optionsH
"
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
18/68
Mobile Application Development Laboratory
Manual
pu4%ic"tem itemH
pu4%ic&enuvents9:
IdisplayDisplay.getDisplay9t)i$:H
formne;Aorm9:H
form1ne;Aorm9+elcted 4ptions are:Hchne;$hoiceMroup9#references,$hoice.MULTIPLE:H
ch.append9cut,nu%%:H
ch.append9copy,nu%%:Hch.append9paste,nu%%:H
ch.append9delete,nu%%:H
ch.set+elected"nde691, true:H
form.append9ch:Hch1ne;$hoiceMroup9,$hoice.EXCLUSIVE:H
ch1.append9select all,nu%%:H
ch1.append9unselect all,nu%%:H
ch1.set+elected"nde691, true:Hform.append9ch1:H
Piewne;$ommand9Piew,$ommand.OK,1:H6it ne;$ommand96it,$ommand.EXIT,1:H
Fackne;$ommand9Fack,$ommand.BACK,1:H
form.add$ommand9Piew:Hform.add$ommand96it:H
form1.add$ommand9Fack:H
L
pu4%ic8oidstart;pp9:I
display.set$urrent9form:H
L
pu4%ic8oidpause;pp9:
I
L
pu4%ic8oiddestroy;pp94oo%eanunconditional:
I
L
L
4utputC
9
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
19/68
Mobile Application Development Laboratory
Manual
E?erci$e *&
0rite 2'ME program ;)ic) create$ fo%%o;ing 1ind of menuE8ent .and%ing
o Cut can 4e on/off
o Cop5 can 4e on/off
o Pa$t can 4e on/off
o De%ete can 4e on/off
o Se%ect a%% Put a%% four Option$ on
o n$e%ect a%% Put a%% four Option$ off
importava6.microedition.midlet.GH
importava6.microedition.lcdui.GH
pu4%icc%a$$&enuvents! e?tend$&"Dlet imp%ement$$ommand@istener,"tem+tate@istener I
:
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
20/68
-
8/10/2019 Mad Master Manual (1)
21/68
Mobile Application Development Laboratory
Manual
I
L
pu4%ic8oidcommand;ction9$ommand command,Displayable displayable:I
if9displayableform:
I if9commandPiew:
I
4oo%eanopt2ne;4oo%eanch.si/e9:2Hoptionsne;+tring"tem9,:H
+tring valuesH
ch.get+electedAlags9opt:H
options.setTe6t9:H
for9inti-HiQopt.lengthHi>>:
I
if9opti2:I
values>ch.get+tring9i:>RnHL
L
options.setTe6t9values:Hform1.append9options:H
display.set$urrent9form1:H
L
e%$eif9command6it:I
destroy;pp9true:H
notifyDestroyed9:HL
L
e%$eif9displayableform1:I
if9commandFack:
I
display.set$urrent9form:Hoptions.setTe6t9:H
L
LL
pu4%ic8oiditem+tate$hanged9"tem item:
I
if9itemch1:
I
inti-H&
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
22/68
Mobile Application Development Laboratory
Manual
intsi/ech.si/e9:H
;)i%e9iQsi/e:
I
if9ch1.get+elected"nde69:-:
ch.set+elected"nde69i, true:H
e%$ech.set+elected"nde69i, fa%$e:H
i>>H
LL
L
L
Output&
E?erci$e +&
Create MIDP app%ication ;)ic) dra;$ a 4ar grap) to t)e di$p%a5 data 8a%ue$ can 4e
gi8en at intarra5( You can enter four data integer$ 8a%ue$ to t)e input te?t fie%d(
importava6.microedition.midlet.GH
importava6.microedition.lcdui.GH
pu4%icc%a$$FarMraph&"Dlet e?tend$&"Dlet imp%ement$$ommand@istener
I
pu4%icAorm formH
pu4%ic$ommand e6it$ommandH
pu4%ic$ommand 4k$ommandH
pu4%ic$ommand back$ommandH
pu4%icDisplayable dH
pu4%icDisplay displayH
pu4%icTe6tAield te6tfield1H
pu4%icTe6tAield te6tfield!H
pu4%icTe6tAield te6tfield)H
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
23/68
Mobile Application Development Laboratory
Manual
pu4%icTe6tAield te6tfield0H
pu4%icFarMraph&"Dlet9:I
displayDisplay.getDisplay9t)i$:H
formne;Aorm9FarMraph:Hte6tfield1ne;Te6tAield9Palue1C
-
8/10/2019 Mad Master Manual (1)
24/68
Mobile Application Development Laboratory
Manual
d.set$ommand@istener9t)i$:H
display.set$urrent9d:H
L
e%$eif9commande6it$ommand:
notifyDestroyed9:H
L e%$eif9displayabled:
I
if9commandback$ommand:display.set$urrent9form:H
L
L
L
c%a$$Far$anvas e?tend$$anvasI
int2 dataH
pu4%icint6H
pu4%icintyHpu4%icinty1H
pu4%icinthH
pu4%icFar$anvas9int2 data:
I
t)i$.datadataH61-H
L
pu4%ic8oidpaint9Mraphics g:
Ig.set$olor9!88,!88,!88:H
g.fillBect9-, -, t)i$.getidth9:, t)i$.get'eight9::H
g.set$olor9!88, 1!8, 1--:H
inti-H
y1data-2H
h!--H
;)i%e9iQdata.length:
I
ydatai2H
h!-->y1)-H
i>>HL
LL
4utputC$0
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
25/68
-
8/10/2019 Mad Master Manual (1)
26/68
Mobile Application Development Laboratory
Manual
displayDisplay.getDisplay9this:H
form1new Aorm9"nsert the #hone number:H
e6it$ommandnew $ommand96it,$ommand.S"T,1:Hok$ommandnew $ommand94k,$ommand.4K,1:H
stnew +tring"tem9#hone *umber is ,:H
te6tfield1new Te6tAield9#honeH,,)-,Te6tAield.;*5:Hform1.append9te6tfield1:H
form1.add$ommand9ok$ommand:H
form1.add$ommand9e6it$ommand:Hform1.set$ommand@istener9this:H
L
public void start;pp9:
Idisplay.set$urrent9form1:H
L
public void pause;pp9:
I L
public void destroy;pp9boolean unconditional:I
L
public void command;ction9$ommand cmd,Displayable displayable:I
if9cmde6it$ommand:
notifyDestroyed9:H
else if9cmdok$ommand:I
+tring ste6tfield1.get+tring9:H
ss.replace9 , .:H
int lens.length9:H
int i-Hint c-H
+tring s1H
while9iQlen:I
if9s.char;t9i:.:
I
if9c-:
I
$$
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
27/68
Mobile Application Development Laboratory
Manual
if9s1.euals9-0-: OO s1.euals9-01: OO s1.euals9-8-: OO s1.euals9-0--: OO s1.euals9-00::
I
c>>H
s1H
LL
if9c1:
Iif9s1.length9:>H
s1HL
L
L
s1s1>s.char;t9i:Hi>>H
Lif9s1.length9:
-
8/10/2019 Mad Master Manual (1)
28/68
-
8/10/2019 Mad Master Manual (1)
29/68
-
8/10/2019 Mad Master Manual (1)
30/68
Mobile Application Development Laboratory
Manual
public Aorm form1H
public Aorm form!H
public $ommand start$ommandH public $ommand refresh$ommandH
public $ommand e6it$ommandH
public Display displayH public +tring"tem stH
public Datagram+erver9:
I displayDisplay.getDisplay9this:H
start$ommandnew $ommand9+tart,$ommand.4K,1:H
refresh$ommandnew $ommand9Befresh,$ommand.4K,1:H
e6it$ommandnew $ommand96it,$ommand.S"T,1:H stnew +tring"tem9 , :H
form1 new Aorm9DataMramserver:H
form!new Aorm9Beady to receive &essages:H
form1.add$ommand9start$ommand:H form1.set$ommand@istener9this:H
form!.add$ommand9refresh$ommand:H form!.add$ommand9e6it$ommand:H
form!.set$ommand@istener9this:H
L public void start;pp9:
I
display.set$urrent9form1:H
L
public void pause;pp9:
I L
public void destroy;pp9boolean unconditional:I
L
public void command;ction9$ommand cmd,Displayable displayable: I
if9displayableform1:
I if9cmdstart$ommand:
I
try I Datagram$onnection dgc 9Datagram$onnection:
$onnector.open9datagramC33C7--1:H
try I$%
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
31/68
Mobile Application Development Laboratory
Manual
int si/e 1--H
Datagram datagram dgc.newDatagram9si/e:H
dgc.receive9datagram:H
form!.append9datagram.getData9:.to+tring9::H
L finally I dgc.close9:H
L
L catch 96ception 6:I
6.print+tackTrace9:H
L
display.set$urrent9form!:H
L L
else if9displayableform!: I
if9cmde6it$ommand:
I notifyDestroyed9:H
L
else if9cmdrefresh$ommand:
I st.setTe6t9 :H
L
L L
L
Creating t)e DatagramC%ient pro
-
8/10/2019 Mad Master Manual (1)
32/68
-
8/10/2019 Mad Master Manual (1)
33/68
Mobile Application Development Laboratory
Manual
%: Try various features of the phone simulator including the different look
-
8/10/2019 Mad Master Manual (1)
34/68
Mobile Application Development Laboratory
Manual
byte2 payload te6tfield.get+tring9:.getFytes9:H
Datagram datagram dgc.newDatagram9payload, payload.length:H
dgc.send9datagram:HL
L finally
I dgc.close9:H
L
L catch 96ception 6:I
6.print+tackTrace9:H
L
L
L
L
Output&
E?erci$e H&
Aut)entication ;it) a 0e4 Ser8er
Login to .TTP Ser8er from a 2'ME Program
This J!& sample program shows how to display a simple @4M"* +$B* on the J!&phone and how to authenticate to a 'TT# server.
+ource $odeC
import ava6.microedition.midlet.GH
import ava6.microedition.lcdui.GH
import ava6.microedition.io.GHimport ava.io.GH
public class login e6tends &"Dlet implements $ommand@istenerI
public Aorm form1H
"0
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
35/68
Mobile Application Development Laboratory
Manual
public $ommand ok$ommandH
public Display displayH
public 'ttp$onnection htnullHpublic "nput+tream istnullH
public +tring"tem stHpublic Te6tAield t1H
public Te6tAield t!H
public ;lert alertHpublic Aorm form!H
public login9:
I
displayDisplay.getDisplay9this:H
stnew +tring"tem9 , elcome:H
alert new ;lert9 ,rong User*ame or #assword,null,;lertType."*A4:H t1new
Te6tAield9User*ame, ,)-,Te6tAield.;*5:Ht!new Te6tAield9#assword, ,)-,Te6tAield.#;++4BD:H
form1new Aorm9@ogin 'ere:Hform!new Aorm9elcome:H
ok$ommandnew $ommand9@ogin,$ommand.4K,1:H
form1.add$ommand9ok$ommand:Hform1.set$ommand@istener9this:H
form1.append9t1:H
form1.append9t!:Hform!.append9st:H
L
public void start;pp9:I
display.set$urrent9form1:H
L
public void pause;pp9:
I
L
public void destroy;pp9boolean unconditional:
InotifyDestroyed9:H
Lpublic void command;ction9$ommand cmd,Displayable d:
I
if9cmdok$ommand:"
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
36/68
Mobile Application Development Laboratory
Manual
I
try
I+tring
urlhttpC3317!.1%=.8.17C=-=-3eb;pplication?3inde6.spWt1>t1.get+tring9:.trim9:
>Xt!>t!.get+tring9:.trim9:Hht9'ttp$onnection:$onnector.open9url:H
istht.open"nput+tream9:H
byte2 bnew byte7--2Hist.read9b:H
+tring snew +tring9b:H
ss.trim9:H
if9s.euals9ok:: Deptdisplay.set$urrent9form!:H
else
I
alert.setTimeout9;lert.A4BPB:Hdisplay.set$urrent9alert:H
LL
catch96ception e6:
Iform1.append9e6.to+tring9::H
LLLL
E?erci$e
Create a MIDP app%ication= ;)ic) $)o; to t)e u$er ->#, ui@ ue$tion$( A%% ue$tion$ )a8e
+ po$$i4%e option$ and one rig)t option e?act%5( App%ication count$ and $)o;$ to t)e u$er
)o; man5 rig)t an$;er$ ;ere rig)t and $)o;$ t)em to u$er
+ource $odeC
import ava6.microedition.midlet.GH
import ava6.microedition.lcdui.GH
import ava6.microedition.rms.GHimport ava.io.GH
public class Yui/ e6tends &"Dlet implements $ommand@istener
Ipublic Aorm form1H
public Aorm form!H
public Aorm form)H
public Aorm form0H
public Aorm form8H"$
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
37/68
-
8/10/2019 Mad Master Manual (1)
38/68
Mobile Application Development Laboratory
Manual
form1.add$ommand9ne6t$ommand:H
form1.set$ommand@istener9this:H
ch!new $hoiceMroup9,$hoice.S$@U+"P:H
ch!.append9b>a, null:Hch!.append9bGa, null:H
ch!.append9b3a, null:H
ch!.append9b
-
8/10/2019 Mad Master Manual (1)
39/68
Mobile Application Development Laboratory
Manual
form8.set$ommand@istener9this:H
form%.add$ommand96it$ommand:H
Lpublic void start;pp9:
I
display.set$urrent9form1:HL
public void pause;pp9: I
L
public void destroy;pp9boolean unconditional:
IL
public void command;ction9$ommand cmd,Displayable displayable:
I
if9displayableform1:
Iif9cmdne6t$ommand:
display.set$urrent9form!:H
Lelse if9displayableform!:
I
if9cmdne6t$ommand:
display.set$urrent9form):H
else if9cmdback$ommand:
display.set$urrent9form1:HL
else if9displayableform):
Iif9cmdne6t$ommand:
display.set$urrent9form0:H
else if9cmdback$ommand:display.set$urrent9form!:H
L
else if9displayableform0:I
if9cmdne6t$ommand:
display.set$urrent9form8:Helse if9cmdback$ommand:
display.set$urrent9form):H
L":
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
40/68
Mobile Application Development Laboratory
Manual
else if9displayableform8:
I
if9cmdne6t$ommand:I
if9ch1.get+elected"nde69:!:count>>H
if9ch!.get+elected"nde69:-:
count>>Hif9ch).get+elected"nde69:):
count>>H
if9ch0.get+elected"nde69:):
count>>Hif9ch8.get+elected"nde69:):
count>>H
st.setTe6t9+tring.value4f9count::Hform%.append9st:H
form%.append9te6tfield:Hdisplay.set$urrent9form%:H
L
L
L
L
4utputC
"%
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
41/68
Mobile Application Development Laboratory
Manual
ANDROID
#( Introduction
;ndroid is an Operating Syste! "or !o#ile de$i%esdeveloped by Moogle, which is built upon
@inu6 kernel. ;ndroid competes with ;pples i4+ 9for i#hone3i#ad:, B"&s Flackberry,
&icrosofts indows #hone 9previously called indows &obile:, +ambian 4+, and many otherproprietary mobile 4+es.
;ndroid is based on @inu6 with a set of native core $3$>> libraries. ;ndroid applications arewritten in Java. 'owever, they run on ;ndroids own Java Pirtual &achine, called Dalvik Pirtual
&achine 9DP&:, which is optimi/ed to operate on the mobile devices.
'( .o; to In$ta%% Android SD6
Fefore installing ;ndroid +DK, you need to installC
1. Java Development Kit 9JDK:C .
!. clipseC
Step #& Do;n%oad t)e Android SD6
Download the ;ndroid +DK from httpC33developer.android.com3sdk3inde6.html. Aor novices,
choose the installer version by clicking the button Download the +DK for indows
9installerEr!-
-
8/10/2019 Mad Master Manual (1)
42/68
Mobile Application Development Laboratory
Manual
Step '& In$ta%% Android SD6
Bun the downloaded installer into a directory of your choice, e.g., dCRbinRandroid
-
8/10/2019 Mad Master Manual (1)
43/68
Mobile Application Development Laboratory
Manual
to launch. 5ou can change the orientation 9between portrait and landscape: of the the emulator
via $trl
-
8/10/2019 Mad Master Manual (1)
44/68
Mobile Application Development Laboratory
Manual
Trying launching the app from '4& ;... ;@ook for the icon 'ello.;lso try '4& ;... ;&*U ;&anage ;pps ;+elect 'ello;ndroid ;Uninstall.
90
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
45/68
Mobile Application Development Laboratory
Manual
+ource codeC
6pand the src node. 6pand the com.e6ample.helloandroid package node.
4pen the 'ello;ctivity.ava, and replace it with the following codesC
package com.e6ample.helloandroidH
import android.app.;ctivityH
import android.os.FundleH
import android.widget.Te6tPiewH
public class 'ello;ctivity e6tends ;ctivityI
3GG $alled when the activity is first created. G3
[4verride
public void on$reate9Fundle saved"nstance+tate:
I
super.on$reate9saved"nstance+tate:H
Te6tPiew te6tPiew new Te6tPiew9this:H
33 $onstruct a Te6tPiew U" component
te6tPiew.setTe6t9'ello,world:H
33 +et the te6t message for Te6tPiew
set$ontentPiew9te6tPiew:H
33 this ;ctivity sets its content to the Te6tPiew
L
L
9
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
46/68
Mobile Application Development Laboratory
Manual
E?erci$e #,&
0rite an android app%ication program t)at di$p%a5$ )e%%o ;or%d u$ing ec%ip$e
Launc) Ec%ip$e
1. Arom Aile menu ;*ew ;#roect.. ;;ndroid ;pplication #roect ;*e6t.!. The *ew ;ndroid #roect dialog appearsC
1. "n ;pplication *ame, enter 'ello ;ndroid 9this is the ;ndroid appliation
name that shows up on the real device:.
!. "n #roect *ame, enter 'ello;ndroid 9this is the clipses proect name:.
). "n #ackage *ame, enter com.e6ample.helloandroid.
0. "n Fuild +DK, select the latest version 9e.g., ;ndroid 0.1 9;#" 1%::.
8. "n &inimum Beuired +DK, select ;#" = ;ndroid !.! 9Aroyo: < almost all of
the ;ndroid devices meet this minimum reuirement ;*e6t.
). The $onfigure @auncher "con dialog appears, which allows you to set the applications
icon to be displayed on the devices ;*e6t.
0. The $reate ;ctivitiy dialog appears. $heck $reate ;ctivity Fo6 ;+elect
Flank;ctivity ;*e6t.
8. The *ew Flank ;ctivity dialog appears.
1. "n ;ctivity *ame, enter 'ello;ctivity.
!. "n @ayout *ame, enter activityEhello 9default:.
). "n Title, enter 'ello 9this title will appear as the screen title: ;Ainish.
clipse ;DT creates a default 'ello
-
8/10/2019 Mad Master Manual (1)
47/68
Mobile Application Development Laboratory
Manual
Trying launching the app from '4& ;... ;@ook for the icon 'ello.;lso try '4& ;... ;&*U ;&anage ;pps ;+elect 'ello;ndroid ;Uninstall.
9"
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
48/68
Mobile Application Development Laboratory
Manual
Source code&
6pand the src node. 6pand the com.e6ample.helloandroid package node.
4pen the 'ello;ctivity.ava, and replace it with the following codesC
package com.e6ample.helloandroidH
import android.app.;ctivityH
import android.os.FundleH
import android.widget.Te6tPiewH
public class 'ello;ctivity e6tends ;ctivityI
3GG $alled when the activity is first created. G3
[4verride
public void on$reate9Fundle saved"nstance+tate:
I
super.on$reate9saved"nstance+tate:H
Te6tPiew te6tPiew new Te6tPiew9this:H
33 $onstruct a Te6tPiew U" component
te6tPiew.setTe6t9'ello,world:H
33 +et the te6t message for Te6tPiew
set$ontentPiew9te6tPiew:H
33 this ;ctivity sets its content to the Te6tPiew
L
L
99
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
49/68
Mobile Application Development Laboratory
Manual
E?erci$e ##&
0rite an android app%ication program t)at accept$ input from t)e u$er and di$p%a5$ t)e
)e%%o name to t)e u$er in re$pon$e a$ output u$ing ec%ip$e
package com.avapapers.android.formH
import android.os.FundleHimport android.app.;ctivityH
import android.view.PiewH
import android.widget.FuttonHimport android.widget.ditTe6tH
import android.widget.Te6tPiewH
public class Arm;ctivity e6tends ;ctivityI
Futton mFuttonHditTe6t mditH
Te6tPiew mTe6tH
[4verride
public void on$reate9Fundle saved"nstance+tate:
I
super.on$reate9saved"nstance+tate:Hset$ontentPiew9B.layout.activityEfrm:H
mFutton 9Futton:findPiewFy"d9B.id.button1:H
mFutton.set4n$lick@istener9new Piew.4n$lick@istener9:
I
public void on$lick9Piew view: Imdit 9ditTe6t:findPiewFy"d9B.id.editTe6t1:H
mTe6t 9Te6tPiew:findPiewFy"d9B.id.te6tPiew1:H
mTe6t.setTe6t9elcome >mdit.getTe6t9:.to+tring9:>Z:HL
L
L
L
9:
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
50/68
-
8/10/2019 Mad Master Manual (1)
51/68
Mobile Application Development Laboratory
Manual
!igure #$reating a new ;ndroid proect using clipse
.LinearLayout
The @inear@ayout arranges views in a single column or single row. $hild views can either be
arranged vertically or hori/ontally.
?m%version1.- encodingutf
-
8/10/2019 Mad Master Manual (1)
52/68
-
8/10/2019 Mad Master Manual (1)
53/68
Mobile Application Development Laboratory
Manual
QTe6tPiew
androidClayoutEwidth1-8p6
androidClayoutEheightwrapEcontent
androidCte6t[string3hello
3N
"n this case, the width is set to 1-8 pi6els wide. modify the main.6ml file by adding a QFuttonN
view as shown belowC
?m%version1.- encodingutf
-
8/10/2019 Mad Master Manual (1)
54/68
Mobile Application Development Laboratory
Manual
!igure *The views laid out in @inear@ayout
The default orientation of @inear@ayout is set to hori/ontal. "f you want to change its orientation
to vertical, set the orientation attribute to vertical, like thisC
LinearLa5out
androidClayoutEwidthfillEparent
androidClayoutEheightfillEparent
androidCorientationvertical
6mlnsCandroidhttpC33schemas.android.com3apk3res3android
Aigure 0 shows the effect of changing the orientation to vertical.
:0
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
55/68
-
8/10/2019 Mad Master Manual (1)
56/68
Mobile Application Development Laboratory
Manual
//LinearLa5out
Aigure 8 shows that the button is aligned to the right of its parent 9which is the @inear@ayout:using the layoutEgravity attribute. ;t the same time, you use the layoutEweight attribute to
specify the ratio in which the Futton and ditTe6t views occupy the remaining space on the
screen. The total value for the layoutEweight attribute must be eual to 1.
!igure -;pplying the layoutEweight and layoutEgravity attributes
AbsoluteLayout
The ;bsolute@ayout lets you specify the e6act location of its children. $onsider the following U"defined in main.6mlC
?m%version1.- encodingutf
-
8/10/2019 Mad Master Manual (1)
57/68
Mobile Application Development Laboratory
Manual
androidClayoutEy)%1p6
/
/A4$o%uteLa5out
Aigure % shows the two Futton views located at their specified positions using the
androidElayoutE6 and androidElayoutEy attributes.
!igure Piews laid out using ;bsolute@ayout;uthors *ote. 5ou should ideally use ;bsolute@ayout when you need to reposition your views
when there is a change in the screen rotation.TableLayout
The Table@ayout groups views into rows and columns. 5ou use the QTableBowN element to
designate a row in the table. ach row can contain one or more views. ach view you place
within a row forms a cell. The width for each column is determined by the largest width of eachcell in that column.
#opulate main.6ml with the following elements and observe the U" as shown in Aigure ?.
?m%version1.- encodingutf
-
8/10/2019 Mad Master Manual (1)
58/68
Mobile Application Development Laboratory
Manual
/ EditTe?t
androidCid[>id3t6tUser*ameandroidCwidth!--p6 /
/Ta4%eRo;
Ta4%eRo; Te?tVie;
androidCte6t#asswordC
/ EditTe?t
androidCid[>id3t6t#assword
androidCpasswordtrue
/
/Ta4%eRo; Ta4%eRo;
Te?tVie;/
C)ec1Bo?androidCid[>id3chkBemember#assword androidClayoutEwidthfillEparent
androidClayoutEheightwrapEcontent androidCte6tBemember #assword
/
/Ta4%eRo;
Ta4%eRo; Button
androidCid[>id3button+ign"n
androidCte6t@og "n / /Ta4%eRo;/Ta4%eLa5out
:9
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
59/68
Mobile Application Development Laboratory
Manual
!igure HUsing the Table@ayout
*ote that in the above e6ample, there are two columns and four rows in the Table@ayout. The
cell directly under the #assword Te6tPiew is populated with an empty element. "f you dont dothis, the Bemember #assword checkbo6 will then appear under the #assword Te6tPiew, like that
shown in Aigure =.
!igure *ote the change in the position of the Bemember #assword view
Re%ati8eLa5out
The Belative@ayout lets you specify how child views are positioned relative to each other.$onsider the following main.6ml fileC
?m%version1.- encodingutfid3B@ayout androidClayoutEwidthfillEparent
androidClayoutEheightfillEparent
6mlnsCandroidhttpC33schemas.android.com3apk3res3android Te?tVie; androidCid[>id3lbl$omments androidClayoutEwidthwrapEcontent
androidClayoutEheightwrapEcontent
androidCte6t$omments androidClayoutEalign#arentToptrue
::
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
60/68
Mobile Application Development Laboratory
Manual
androidClayoutEalign#arent@efttrue
/
EditTe?t androidCid[>id3t6t$omments
androidClayoutEwidthfillEparent
androidClayoutEheight1?-p6 androidCte6t+i/e1=sp
androidClayoutEalign@eft[>id3lbl$omments
androidClayoutEbelow[>id3lbl$omments androidClayoutEcenter'ori/ontaltrue
/ Button
androidCid[>id3btn+ave androidClayoutEwidth1!8p6
androidClayoutEheightwrapEcontent
androidCte6t+ave
androidClayoutEbelow[>id3t6t$omments androidClayoutEalignBight[>id3t6t$omments
/ Button androidCid[>id3btn$ancel
androidClayoutEwidth1!0p6 androidClayoutEheightwrapEcontent
androidCte6t$ancel
androidClayoutEbelow[>id3t6t$omments
androidClayoutEalign@eft[>id3t6t$omments //Re%ati8eLa5out
*otice that each view embedded within the Belative@ayout have attributes that allow them toalign with another view. These attributes areC
layoutEalign#arentTop
layoutEalign#arent@eft
layoutEalign@eft
layoutEalignBight
layoutEbelow
layoutEcenter'ori/ontal
The value for each of these attributes is the "D for the view that you are referencing. The aboveS&@ U" creates the screen shown in Aigure 7.
:%
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
61/68
-
8/10/2019 Mad Master Manual (1)
62/68
Mobile Application Development Laboratory
Manual
!igure #,Using Arame@ayout"f you add another view 9such as a Futton view: within the Arame@ayout, the view will overlap
the previous view 9see also Aigure 11:C
?m%version1.- encodingutfid3widget%=
androidClayoutEwidthfillEparent
androidClayoutEheightfillEparent 6mlnsCandroidhttpC33schemas.android.com3apk3res3android
!rameLa5out androidClayoutEwidthwrapEcontent
androidClayoutEheightwrapEcontent
androidClayoutE60-p6 androidClayoutEy)8p6
ImageVie;
androidCsrc [drawable3androidlogo androidClayoutEwidthwrapEcontent
androidClayoutEheightwrapEcontent
/
Button
androidClayoutEwidth1!0p6
androidClayoutEheightwrapEcontent androidCte6t#rint #icture
/
/!rameLa5out
/A4$o%uteLa5out
:
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
63/68
Mobile Application Development Laboratory
Manual
!igure ##4verlapping views
5ou can add multiple views to a Arame@ayout, but each will stack on top of the previous one.
E?erci$e #*&
0rite an android app%ication program t)at con8ert$ t)e temperature from Ce%$iu$ to
!a)ren)eit(
package com.e6ample.converttemperaturee6ampleH
import android.app.;ctivityH
import android.os.FundleHimport android.view.PiewH
import android.widget.ditTe6tH
import android.widget.BadioFuttonHimport android.widget.ToastH
public class $onvertTemperture6ample e6tends ;ctivityI
private ditTe6t te6tH
[4verride public void on$reate9Fundle saved"nstance+tate:
I
super.on$reate9saved"nstance+tate:H set$ontentPiew9B.layout.main:H
te6t 9ditTe6t: findPiewFy"d9B.id.editTe6t1:H
L
33 This method is called at button click because we assigned the name to the
33 4n $lick property of the button public void my$lick'andler9Piew view:
I
:!
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
64/68
Mobile Application Development Laboratory
Manual
switch 9view.get"d9::
I
case B.id.button1C BadioFutton celsiusFutton 9BadioFutton: findPiewFy"d9B.id.radio-:H
BadioFutton fahrenheitFutton 9BadioFutton: findPiewFy"d9B.id.radio1:H
if 9te6t.getTe6t9:.length9: -:I
Toast.makeTe6t9this, #lease enter a valid number,
Toast.@*MT'E@4*M:.show9:H returnH
L
float inputPalue Aloat.parseAloat9te6t.getTe6t9:.to+tring9::H if 9celsiusFutton.is$hecked9::
I
te6t.setTe6t9+tring
.value4f9convertAahrenheitTo$elsius9inputPalue:::H celsiusFutton.set$hecked9false:H
fahrenheitFutton.set$hecked9true:H L else
I
te6t.setTe6t9+tring .value4f9convert$elsiusToAahrenheit9inputPalue:::H
fahrenheitFutton.set$hecked9false:H
celsiusFutton.set$hecked9true:H
L breakH
L
L
33 $onverts to celsius
private float convertAahrenheitTo$elsius9float fahrenheit:I
return 99fahrenheit < )!: G 8 3 7:H
L
33 $onverts to fahrenheit
private float convert$elsiusToAahrenheit9float celsius:
I return 99celsius G 7: 3 8: > )!H
L
L$ompile and build the proect.
4utputC%0
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
-
8/10/2019 Mad Master Manual (1)
65/68
Mobile Application Development Laboratory
Manual
%
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
http://www.edumobile.org/android/wp-content/uploads/2012/08/converttemperatureexample4.pnghttp://www.edumobile.org/android/wp-content/uploads/2012/08/converttemperatureexample3.pnghttp://www.edumobile.org/android/wp-content/uploads/2012/08/converttemperatureexample2.png -
8/10/2019 Mad Master Manual (1)
66/68
Mobile Application Development Laboratory
Manual
E?erci$e #+&
0rite an android app%ication t)at $)o;$ )o; to u$e intent$ in mo4i%e app%ication
de8e%opment
"ntents are a powerful concept as they allow the creation of loosely coupled applications. "ntents
can be used to communicate between any installed application components on the device.
$reate a proect "ntentFrowser
#ut the following code in layout3main.6mlC
QW6ml version1.- encodingutfid3Futton-1 androidClayoutEwidthwrapEcontent
androidClayoutEheightwrapEcontent androidCte6tFrowser
androidCon$lickopenFrowserNQ3FuttonN
Q3@inear@ayoutN
e will start a new "ntent with the method C
%$
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
http://schemas.android.com/apk/res/androidhttp://www.edumobile.org/android/wp-content/uploads/2012/08/converttemperatureexample5.pnghttp://schemas.android.com/apk/res/android -
8/10/2019 Mad Master Manual (1)
67/68
Mobile Application Development Laboratory
Manual
"ntent i new "ntent9android.intent.action.P",
Uri.parse9httpC33www.google.com::H;s your activity gets called with intent you can get the data from the intent and display it in your
application.
"n this e6ample click event of the button will open Frowser.
Bun the application.
Step$ to Create&
4pen clipse. Use the *ew #roect i/ard and select ;ndroid #roect Mive the respective
proect name i.e. "ntentFrowser. nter following informationC
#roect nameC "ntentFrowser
Fuild TargetC ;ndroid !.)
;pplication nameC "ntentFrowser#ackage nameC com.sample."ntentFrowser
$reate ;ctivityC "ntentFrowser
4n $licking Ainish "ntentFrowser code structure is generated with the necessary ;ndroid
#ackages being imported along with "ntentFrowser.ava. "ntentFrowser class will look likefollowingC
+ource $odeC
package com.sample."ntentFrowserHimport android.app.;ctivityH
import android.content."ntentH
%"
-V-NT/ /NT1T 23 N4/N5/N4 -ND TCN26247
Department o8 Computer iene and ngineering
http://www.google.com/http://www.edumobile.org/android/wp-content/uploads/2011/04/1.jpghttp://www.google.com/ -
8/10/2019 Mad Master Manual (1)
68/68
Mobile Application Development Laboratory
Manual
import android.net.UriH
import android.os.FundleH
import android.view.PiewH
public class "ntentFrowser e6tends ;ctivity
I 3GG $alled when the activity is first created. G3
[4verride
public void on$reate9Fundle saved"nstance+tate:I
super.on$reate9saved"nstance+tate:H
set$ontentPiew9B.layout.main:H
L public void openFrowser9Piew view:
I
"ntent i new "ntent9android.intent.action.P",
Uri.parse9httpC33www.google.com::H start;ctivity9i:H
LL
4utputC
http://www.google.com/http://www.edumobile.org/android/wp-content/uploads/2011/04/3.jpghttp://www.edumobile.org/android/wp-content/uploads/2011/04/2.jpghttp://www.google.com/