Вы находитесь на странице: 1из 89

Patel Group of Institutions

SL-MC( 650017)
Exercise : 1
Create Hello World application. That will display Hello World in the middle of the
screen in the red color with white background.
Screen Shot:
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor" android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" android:layout_gravity="center_ertical"
android:gravity="center_horizontal" android:textolor="@color/fgcolor"!>
<!LinearLayout>
PGI Page 1
Patel Group of Institutions
SL-MC( 650017)
JAVA :
FirstActivity.java :
package "gi#$irst%
import android#a""#&ctivity%
import android#os#'undle%
public class (irst&ctivity extends &ctivity
)
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
/
/
Exercise : 2
To understand Activity !ntent
a. Create sample application with login module."Check username and password#
b. $n successful login go to ne%t screen. And on failing login alert user using
Toast.
c. Also pass username to ne%t screen
Screen Shot:
PGI Page 2
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<.elativeLayout android:layout_width="match_parent"
android:id="@!id/relatie"a#out1" android:layout_height="match_parent"
android:background="@color/bgcolor">
<TextView android:id="@!id/te$t%iew1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text&""earance="&android:attr/te$t'ppearance"arge"
android:layout_align0arentTo"="true" android:layout_align0arentLe$t="true"
android:layout_marginTo"="()dp" android:text="*nter ,ser
-ame :"><!TextView>
<1ditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:in"utTy"e="te$t.erson-ame"
android:layout_below="@!id/te$t%iew1" android:layout_align0arentLe$t="true"
android:layout_align0arent.ight="true" android:id="@!id/t$tusername"
android:hint="*nter ,ser-ame">
<re2uest(ocus><!re2uest(ocus>
<!1ditText>
<TextView android:id="@!id/te$t%iew/"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text&""earance="&android:attr/te$t'ppearance"arge"
android:layout_below="@!id/t$tusername"
PGI Page 3
Patel Group of Institutions
SL-MC( 650017)
android:layout_align0arentLe$t="true" android:layout_marginTo"="/0dp"
android:text="*nter .assword :"><!TextView>
<1ditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:in"utTy"e="te$t.assword"
android:layout_below="@!id/te$t%iew/" android:layout_align0arentLe$t="true"
android:layout_align0arent.ight="true" android:id="@!id/t$tpassword"
android:hint="*nter .assword"><!1ditText>
<'utton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTo"="@!id/btnlogin"
android:layout_to.ight3$="@!id/te$t%iew1" android:text="0lear"
android:id="@!id/btnclear"><!'utton>
<'utton android:id="@!id/btnlogin"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""ogin" android:layout_below="@!id/t$tpassword"
android:layout_align.ight="@!id/te$t%iew/"
android:layout_margin.ight="11dp" android:layout_marginTo"="/1dp"><!'utton>
<!.elativeLayout>
<!LinearLayout>
JAVA :
SecondActivity.java :
package "gi#,econd%
import android#a""#&ctivity%
import android#content#+ntent%
import android#os#'undle%
import android#view#View%
import android#view#View#3nlickListener%
import android#widget#'utton%
import android#widget#1ditText%
import android#widget#Toast%
public class ,econd&ctivity extends &ctivity implements 3nlickListener
)
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
'utton login = *'utton-$indView'y+d*.#id#btnlogin-%
'utton clear = *'utton-$indView'y+d*.#id#btnclear-%
login#set3nlickListener*this-%
clear#set3nlickListener*this-%
/
public void onlick*View v-
)
1ditText username = *1ditText-$indView'y+d*.#id#t$tusername-%
1ditText "assword =*1ditText-$indView'y+d*.#id#t$tpassword-%
if*v#get+d*-==.#id#btnlogin-
)
if*username#getText*-#to,tring*-#e2uals*4"gi4- 55
"assword#getText*-#to,tring*-#e2uals*467894--
)
!!Toast#makeText*this: 4ok4: Toast#L1;<T=_L3;<-#show*-%
+ntent i = new +ntent*this:,7#class-%
i#"ut1xtra*4username4: username#getText*-#to,tring*--%
start&ctivity*i-%
/
PGI Page 4
Patel Group of Institutions
SL-MC( 650017)
else
)
Toast#make2e$t*this: 41rror4: Toast#"*-324_"5-3-#show*-%
/
/
if*v#get+d*-==.#id#btnclear-
)
username#setText*44-%
"assword#setText*44-%
/
/
/
S2.java :
package "gi#,econd%
import android#a""#&ctivity%
import android#content#+ntent%
import android#gra"hics#olor%
import android#os#'undle%
import android#view#View%
import android#view#View<rou"#Layout0arams%
import android#widget#'utton%
import android#widget#LinearLayout%
import android#widget#TextView%
public class ,7 extends &ctivity
)
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
+ntent i = get+ntent*-%
!!Toast#makeText*this: 4>elcome4:Toast#L1;<T=_L3;<-#show*-%
,tring username=i#get,tring1xtra*4username4-%
TextView tv=new TextView*this-%
'utton btn'ack = new 'utton*this-%
btn'ack#setText*4'ack4-%
tv#setText*4>elcome 4?username?4 @4-%
tv#setTextolor*olor#rgb*7AA: 7AA: 6BB--%
tv#setText,iCe*7A-%
LinearLayout ll = new LinearLayout*this-%
ll#setLayout0arams*new Layout0arams*Layout0arams#67""_.'8*-2:
Layout0arams#67""_.'8*-2--%
ll#set3rientation*LinearLayout#%*8270'"-%
ll#addView*tv-%
ll#addView*btn'ack-%
setontentView*ll-%

btn'ack#set3nlickListener*new 'utton#3nlickListener*-
)
public void onlick*View v-
)
setontentView*.#layout#main-%
/
/-%
/
/
PGI Page 5
Patel Group of Institutions
SL-MC( 650017)
Exercise :
Create login application where you will have to validate &mail!'"(ser)ame#. Till
the username and password is not validated login button should remain disabled.
Screen Shot:
PGI Page 6
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<.elativeLayout android:background="@color/bgcolor"
android:layout_width="match_parent" android:id="@!id/relatie"a#out1"
android:layout_height="match_parent">
<TextView android:layout_align0arentTo"="true"
android:layout_align0arentLe$t="true" android:text=",ser -ame :"
android:layout_marginTo"="1)dp" android:text&""earance="&
android:attr/te$t'ppearance"arge" android:id="@!id/te$t%iew1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"><!TextView>
<1ditText android:layout_align0arentLe$t="true"
android:in"utTy"e="te$t*mail'ddress" android:id="@!id/t$temail"
android:layout_below="@!id/te$t%iew1"
android:layout_align0arent.ight="true" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:hint="*nter ,ser
-ame"><!1ditText>
<TextView android:layout_align0arentLe$t="true"
android:text=".assword :" android:layout_marginTo"="1)dp"
android:text&""earance="&android:attr/te$t'ppearance"arge"
android:layout_below="@!id/t$temail" android:id="@!id/te$t%iew/"
android:layout_height="wrap_content"
android:layout_width="wrap_content"><!TextView>
PGI Page 7
Patel Group of Institutions
SL-MC( 650017)
<1ditText android:in"utTy"e="te$t.assword"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="@!id/te$t%iew/" android:layout_align0arentLe$t="true"
android:layout_align0arent.ight="true" android:id="@!id/t$tpassword"
android:hint="*nter .assword">
<re2uest(ocus><!re2uest(ocus>
<!1ditText>
<'utton android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_centerVertical="true"
android:layout_to.ight3$="@!id/te$t%iew1" android:id="@!id/btnlogin"
android:text=""ogin" android:text,iCe="/0p$"
android:enabled="false"><!'utton>
<!.elativeLayout>

<!LinearLayout>
JAVA :
!hirdActivity.java :
package "gi#Third%
import android#a""#&ctivity%
import android#content#+ntent%
import android#os#'undle%
import android#text#1ditable%
import android#text#Text>atcher%
import android#view#View%
import android#view#View#3nlickListener%
import android#widget#'utton%
import android#widget#1ditText%
import android#widget#Toast%
public class Third&ctivity extends &ctivity implements
3nlickListener:Text>atcher
)
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
1ditText txtemail = *1ditText-$indView'y+d*.#id#t$temail-%
1ditText txt"assword = *1ditText-$indView'y+d*.#id#t$tpassword-%
txtemail#addTexthangedListener*this-%
txt"assword#addTexthangedListener*this-%
'utton btn = *'utton-$indView'y+d*.#id#btnlogin-%
btn#set3nlickListener*this-%
/
public void onlick*View v-
)
1ditText txte = *1ditText-$indView'y+d*.#id#t$temail-%
1ditText txt" = *1ditText-$indView'y+d*.#id#t$tpassword-%

if*txte#getText*-#to,tring*-#e2uals*4"gi4- 55
txt"#getText*-#to,tring*-#e2uals*467894--
)
+ntent i = new +ntent*this:,7#class-%
i#"ut1xtra*4txte4: txte#getText*-#to,tring*--%
start&ctivity*i-%

PGI Page 8
Patel Group of Institutions
SL-MC( 650017)
!!Toast#makeText*this: 4Login###4:Toast#L1;<T=_L3;<-#show*-%
/
else
)
Toast#make2e$t*this: 4+nvalid4:Toast#"*-324_"5-3-#show*-%
/
/
public void a$terTexthanged*1ditable s-
)
1ditText txte = *1ditText-$indView'y+d*.#id#t$temail-%
1ditText txt" = *1ditText-$indView'y+d*.#id#t$tpassword-%
'utton btn = *'utton-$indView'y+d*.#id#btnlogin-%
if*txte#getText*-#to,tring*-#e2uals*4"gi4- 55
txt"#getText*-#to,tring*-#e2uals*467894--
)
btn#set1nabled*true-%
/
else
)
btn#set1nabled*false-%
/
/
public void be$oreTexthanged*har,e2uence argB: int arg6: int arg7:
int arg8- )
!! TODO &utoDgenerated method stub
/
public void onTexthanged*har,e2uence argB: int arg6: int arg7: int
arg8- )
!! TODO &utoDgenerated method stub
/
/
S2.java :
package "gi#Third%
import android#a""#&ctivity%
import android#content#+ntent%
import android#gra"hics#olor%
import android#os#'undle%
import android#view#View%
import android#view#View<rou"#Layout0arams%
import android#widget#'utton%
import android#widget#LinearLayout%
import android#widget#TextView%
public class ,7 extends &ctivity
)
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
+ntent i= get+ntent*-%
!!Toast#makeText*this:4>elcome4:Toast#L1;<T=_L3;<-#show*-%
,tring txte=i#get,tring1xtra*4txte4-%
TextView tv=new TextView*this-%
'utton btn'ack = new 'utton*this-%
btn'ack#setText*4'ack4-%
tv#setText*4>elcome 4?txte?4 @4-%
PGI Page 9
Patel Group of Institutions
SL-MC( 650017)
tv#setTextolor*olor#rgb*7AA: 7AA: 6BB--%
tv#setText,iCe*7A-%
LinearLayout ll = new LinearLayout*this-%
ll#setLayout0arams*new Layout0arams*Layout0arams#67""_.'8*-2:
Layout0arams#67""_.'8*-2--%
ll#set3rientation*LinearLayout#%*8270'"-%
ll#addView*tv-%
ll#addView*btn'ack-%
setontentView*ll-%

btn'ack#set3nlickListener*new 'utton#3nlickListener*-
)
public void onlick*View v-
)
!!setontentView*.#layout#main-%
$inish*-%
/
/-%
/
/
Exercise : "
Create and *ogin application as above . $n successful login open browser with
any (+*.
Screen Shot:
PGI Page 10
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<.elativeLayout android:layout_width="match_parent"
android:id="@!id/relatie"a#out1" android:layout_height="match_parent"
android:background="@color/bgcolor">
<TextView android:layout_height="wrap_content"
android:id="@!id/te$t%iew1" android:text&""earance="&
android:attr/te$t'ppearance"arge" android:layout_width="wrap_content"
android:layout_align0arentTo"="true" android:layout_align0arentLe$t="true"
android:layout_marginTo"="11dp" android:text=",ser-ame :"><!TextView>
<1ditText android:layout_height="wrap_content"
android:in"utTy"e="te$t.erson-ame" android:layout_width="wrap_content"
android:layout_below="@!id/te$t%iew1" android:layout_align0arentLe$t="true"
android:layout_align0arent.ight="true" android:id="@!id/t$tusername"
android:hint="*nter ,ser -ame">
<re2uest(ocus><!re2uest(ocus>
<!1ditText>
<TextView android:layout_height="wrap_content"
android:id="@!id/te$t%iew/" android:text&""earance="&
android:attr/te$t'ppearance"arge" android:layout_width="wrap_content"
android:layout_below="@!id/t$tusername"
PGI Page 11
Patel Group of Institutions
SL-MC( 650017)
android:layout_align0arentLe$t="true" android:layout_marginTo"="1(dp"
android:text=".assword :"><!TextView>
<1ditText android:layout_height="wrap_content"
android:in"utTy"e="te$t.assword" android:layout_width="wrap_content"
android:layout_below="@!id/te$t%iew/" android:layout_align0arentLe$t="true"
android:layout_align0arent.ight="true" android:id="@!id/t$tpassword"
android:hint="*nter .assword"><!1ditText>
<'utton android:layout_height="wrap_content"
android:text,iCe="/0p$" android:text=""ogin 9e"
android:layout_width="wrap_content" android:layout_centerVertical="true"
android:layout_align0arentLe$t="true" android:layout_marginLe$t="88dp"
android:enabled="false" android:id="@!id/btnlogin"><!'utton>
<!.elativeLayout>
<!LinearLayout>
JAVA :
Fo#rthActivity.java :
package "gi#(ourth%
import android#a""#&ctivity%
import android#content#+ntent%
import android#net#Eri%
import android#os#'undle%
import android#text#1ditable%
import android#text#Text>atcher%
import android#view#View%
import android#view#View#3nlickListener%
import android#widget#'utton%
import android#widget#1ditText%
public class (ourth&ctivity extends &ctivity implements
3nlickListener:Text>atcher
)
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
1ditText username = *1ditText-$indView'y+d*.#id#t$tusername-%
1ditText "assword = *1ditText-$indView'y+d*.#id#t$tpassword-%
username#addTexthangedListener*this-%
"assword#addTexthangedListener*this-%
'utton login = *'utton-$indView'y+d*.#id#btnlogin-%
login#set3nlickListener*this-%
/
PGI Page 12
Patel Group of Institutions
SL-MC( 650017)
public void onlick*View v-
)
+ntent i= new +ntent*4android#intent#action#V+1>4-%
i#setFata*Eri#parse*4htt":!!www#kevalnagaria#blogs"ot#com4--%
start&ctivity*i-%
/
public void a$terTexthanged*1ditable s-
)
1ditText e = *1ditText-$indView'y+d*.#id#t$tusername-%
1ditText " = *1ditText-$indView'y+d*.#id#t$tpassword-%
'utton b = *'utton-$indView'y+d*.#id#btnlogin-%
if*e#getText*-#to,tring*-#e2uals*4"gi4- 55
"#getText*-#to,tring*-#e2uals*467894--
)
b#set1nabled*true-%
/
else
)
b#set1nabled*false-%
/
/
public void be$oreTexthanged*har,e2uence argB: int arg6: int arg7:
int arg8- )
!! TODO &utoDgenerated method stub
/
public void onTexthanged*har,e2uence argB: int arg6: int arg7: int
arg8- )
!! TODO &utoDgenerated method stub
/
/
Exercise : $
Create an application that will pass some number to the ne%t screen and on the
ne%t screen that number of items should be display in the list.
Screen Shot:
PGI Page 13
Patel Group of Institutions
SL-MC( 650017)
XML :
PGI Page 14
Patel Group of Institutions
SL-MC( 650017)
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<.elativeLayout android:id="@!id/relatie"a#out1"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/bgcolor">
<TextView android:text&""earance="&
android:attr/te$t'ppearance:mall" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@!id/te$t%iew1"
android:layout_align0arentTo"="true" android:layout_align0arentLe$t="true"
android:layout_marginTo"="(0dp" android:text="*nter -o 5f 7tem ;ispla# 5n
-e$t :creen"><!TextView>
<1ditText android:in"utTy"e="number"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@!id/te$t%iew1" android:layout_align0arentLe$t="true"
android:layout_align0arent.ight="true" android:hint="*nter -o 5f 7tem"
android:id="@!id/t$titem">
<re2uest(ocus><!re2uest(ocus>
<!1ditText>
<'utton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@!id/t$titem"
android:layout_center=oriContal="true" android:id="@!id/btnne$t"
android:text="'dd 7tem 2o -e$t :creen"><!'utton>
<!.elativeLayout>
<!LinearLayout>
main1.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor" android:id="@la#out/main1">
<TextView android:layout_width="wrap_content" android:text&""earance="&
android:attr/te$t'ppearance9edium" android:layout_height="wrap_content"
android:id="@!id/te$t%iew/" android:text="-o 5f 7tem *ntered 're 's
follows"><!TextView>
<ListView android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@!id/@android:id/list"
android:longlickable="true" android:$ast,croll1nabled="false"><!ListView>
<!LinearLayout>
JAVA :
Fi%thActivity.java :
package "gi#(i$th%
import android#a""#&ctivity%
import android#content#+ntent%
import android#os#'undle%
PGI Page 15
Patel Group of Institutions
SL-MC( 650017)
import android#view#View%
import android#view#View#3nlickListener%
import android#widget#'utton%
import android#widget#1ditText%
public class (i$th&ctivity extends &ctivity implements 3nlickListener
)
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
'utton b = *'utton-$indView'y+d*.#id#btnne$t-%
b#set3nlickListener*this-%
/
public void onlick*View v-
)
+ntent i = new +ntent*this:,7#class-%
1ditText item = *1ditText-$indView'y+d*.#id#t$titem-%
i#"ut1xtra*4item4: item#getText*-#to,tring*--%
start&ctivity*i-%
/
/
S2.java :
package "gi#(i$th%
import Hava#util#&rrayList%
import android#a""#List&ctivity%
import android#os#'undle%
import android#widget#&rray&da"ter%
public class ,7 extends List&ctivity
)
!II @author JGGF I!
&rrayList<,tring> arr=new &rrayList<,tring>*-%
&rray&da"ter<,tring> a%
int i=B%
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main1-%
a = new
&rray&da"ter<,tring>*this:android#.#layout#simple_list_item_1 :arr-%
setList&da"ter*a-%
for*i=6% i<= +nteger#parse7nt*get+ntent*-#get,tring1xtra*4item4--%
i??-
)
arr#add*4+tem : 4?i-%
/
!!Toast#makeText*this: 4>elcome4: Toast#L1;<T=_L3;<-#show*-%
/
/
PGI Page 16
Patel Group of Institutions
SL-MC( 650017)
Exercise : &
(nderstand resource folders ,
a. Create spinner with strings taken from resource folder"res -- value folder#.
b. $n changing spinner value change image.
Screen Shot:
PGI Page 17
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor" android:weight,um="1">
<.elativeLayout android:layout_width="match_parent"
android:id="@!id/relatie"a#out1" android:layout_height="match_parent">
<,"inner android:id="@!id/spinner1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_align0arentTo"="true" android:layout_align0arentLe$t="true"
android:layout_marginTo"="(/dp" android:layout_align0arent.ight="true"
android:entries="@arra#/:piner"><!,"inner>
<+mageView android:id="@!id/image%iew1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:src="@drawable/keal1" android:layout_below="@!id/spinner1"
android:layout_align0arentLe$t="true"
android:layout_marginTo"="1(dp"><!+mageView>
<!.elativeLayout>
<!LinearLayout>
JAVA :
SixActivity.java :
package "gi#,ix%
PGI Page 18
Patel Group of Institutions
SL-MC( 650017)
import android#a""#&ctivity%
import android#os#'undle%
import android#view#View%
import android#widget#&da"terView%
import android#widget#+mageView%
import android#widget#,"inner%
import android#widget#Toast%
import android#widget#&da"terView#3n+tem,electedListener%
public class ,ix&ctivity extends &ctivity implements 3n+tem,electedListener
)

+ntegerKL image+Fs =
)
.#drawable#keal1:
.#drawable#keal/:
.#drawable#keal1:
.#drawable#keal(:
.#drawable#keal)
/%

public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
,"inner s = *,"inner-$indView'y+d*.#id#spinner1-%
s#set3n+tem,electedListener*this-%
/
public void on+tem,elected*&da"terView<?> argB: View arg6: int arg7:
long arg8-
)
Toast#make2e$t*this: 4,elected +tem is = 4?
argB#get,elected+tem*-#to,tring*-: Toast#"*-324_:4582-#show*-%
+mageView i = *+mageView-$indView'y+d*.#id#image%iew1-%
i#set+mage.esource*image+FsKarg7L-%
/
public void on;othing,elected*&da"terView<?> argB-
)
/
/
Exercise : '
(nderstand .enu option.
a. Create an application that will change color of the screen based on selected
options from the menu.
Screen Shot:
PGI Page 19
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weight,um="1">
<.elativeLayout android:id="@!id/relatie"a#out1"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_weight="1.0<"
android:background="@color/bgcolor"><!.elativeLayout>
<!LinearLayout>
JAVA :
SeventhActivity.java :
package "gi#,eventh%
import android#a""#&ctivity%
import android#gra"hics#olor%
import android#os#'undle%
import android#view#Menu%
import android#view#Menu+tem%
PGI Page 20
Patel Group of Institutions
SL-MC( 650017)
import android#widget#.elativeLayout%
public class ,eventh&ctivity extends &ctivity
)

G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
/
public boolean onreate3"tionsMenu*Menu menu-
)
super#onreate3"tionsMenu*menu-%
reateMenu*menu-%
return true%
/
public boolean on3"tions+tem,elected*Menu+tem item-
)
return Menuhoice*item-%
/
private void reateMenu*Menu menu-
)
Menu+tem mnu6 = menu#add*B: B: B: 4.ed4-%
)
mnu6#set&l"habetic,hortcut*NrN-%
mnu6#set+con*.#drawable#icon-%
/
Menu+tem mnu7 = menu#add*B: 6: B: 4<reen4-%
)
mnu7#set&l"habetic,hortcut*NgN-%
mnu7#set+con*.#drawable#icon-%
/
Menu+tem mnu8 = menu#add*B: 7: B: 4>hite4-%
)
mnu8#set&l"habetic,hortcut*NwN-%
mnu8#set+con*.#drawable#icon-%
/
/
private boolean Menuhoice*Menu+tem item-
)
.elativeLayout l= *.elativeLayout-
$indView'y+d*.#id#relatie"a#out1-%

switch *item#get+tem+d*--
)
case B:
l#set'ackgroundolor*olor#8*;-%
return true%
case 6:
l#set'ackgroundolor*olor#38**--%
return true%
case 7:
l#set'ackgroundolor*olor#=472*-%
return true%
/
return false%
/
/
PGI Page 21
Patel Group of Institutions
SL-MC( 650017)
Exercise : (
Create an application that will display toast".essage# on specific interval of time.
Screen Shot:
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor">
PGI Page 22
Patel Group of Institutions
SL-MC( 650017)
<'utton android:layout_height="wrap_content"
android:id="@!id/btnfinish" android:layout_width="match_parent"
android:text=".ress 9e"><!'utton>
<!LinearLayout>
JAVA :
Ei)htActivity.java :
package "gi#1ight%
import Hava#util#Timer%
import Hava#util#TimerTask%
import android#a""#&ctivity%
import android#os#'undle%
import android#os#=andler%
import android#view#View%
import android#widget#'utton%
import android#widget#Toast%
public class 1ight&ctivity extends &ctivity
)

public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%

final =andler h = new =andler*-%
final .unnable r = new .unnable*-
)
public void run*-
)
Toast#make2e$t*get'aseontext*-: 0<+:
Toast#"*-324_:4582-#show*-%
/
/%
Timer t = new Timer*-%
t#schedule&t(ixed.ate*new TimerTask*-
)
public void run*-
)
h#"ost*r-%
/
/: 6BBB: ABBB-%
'utton b = *'utton-$indView'y+d*.#id#btnfinish-%
b#set3nlickListener*new View#3nlickListener*-
)
public void onlick*View argB-
)
$inish*-%
android#os#0rocess#kill.rocess*android#os#0rocess#m#.id*--%
/
/-%
/
/
PGI Page 23
Patel Group of Institutions
SL-MC( 650017)
*+
Exercise : (
Create an application that will display toast".essage# on specific interval of time.
Screen Shot:
PGI Page 24
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weight,um="1" android:background="@color/bgcolor">
<hronometer android:id="@!id/chronometer1" android:text="0hronometer"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_weight="0.0)" android:visibility="inisible"><!hronometer>
<1ditText android:id="@!id/edit2e$t1"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:enabled="false">
<re2uest(ocus><!re2uest(ocus>
<!1ditText>
<!LinearLayout>
JAVA :
PGI Page 25
Patel Group of Institutions
SL-MC( 650017)
,hronometer.java :
package "gi#hronometer%
import android#a""#&ctivity%
import android#os#'undle%
import android#widget#hronometer%
import android#widget#hronometer#3nhronometerTickListener%
import android#widget#1ditText%
import android#widget#Toast%
public class hronometer&ctivity extends &ctivity
)
int i=B%
int Furation=6B%
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%

hronometer chn = *hronometer-$indView'y+d*.#id#chronometer1-%
chn#start*-%
chn#set3nhronometerTickListener*new 3nhronometerTickListener*-
)
public void onhronometerTick*hronometer argB-
)
1ditText txt =
*1ditText-$indView'y+d*.#id#edit2e$t1-%
txt#setText*4Message >ill 'e Fis"lay &$ter K4?
*FurationD*i?6--?4L ,econds4-%
i??%
if*i>=Furation-
)
Toast#make2e$t*hronometer&ctivity#this:
4Message4?*i!6B-: 6BBBB-#show*-%
Furation?=6B%
/
/
/-%
/
/
Exercise : -
Create a background application that will open activity on specific time.
PGI Page 26
Patel Group of Institutions
SL-MC( 650017)
Screen Shot:
XML :
PGI Page 27
Patel Group of Institutions
SL-MC( 650017)
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<.elativeLayout android:id="@!id/relatie"a#out1"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/bgcolor">
<'utton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@!id/button1"
android:layout_align0arentTo"="true" android:layout_center=oriContal="true"
android:layout_marginTo"="180dp" android:text=":tart :erice"><!'utton>
<'utton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@!id/button/"
android:layout_below="@!id/button1" android:layout_alignLe$t="@!id/button1"
android:layout_marginTo"="18dp" android:text=":top :erice"><!'utton>
<TextView android:id="@!id/te$t%iew1"
android:layout_width="wrap_content" android:text&""earance="&
android:attr/te$t'ppearance"arge" android:layout_height="wrap_content"
android:text=">3 :erice ;emo" android:layout_align0arentTo"="true"
android:layout_center=oriContal="true"
android:layout_marginTo"="?1dp"><!TextView>
<!.elativeLayout>
<!LinearLayout>
JAVA :
MyService.java
package "gi#;ine%
import kmn#servicedemo#.%
import android#a""#,ervice%
import android#content#+ntent%
import android#media#Media0layer%
import android#os#+'inder%
import android#util#Log%
import android#widget#Toast%
public class My,ervice extends ,ervice
)
private static final ,tring 2'3 = 4My,ervice4%
Media0layer "layer%
public +'inder on'ind*+ntent intent-
)
return null%
/
public void onreate*-
)
Toast#make2e$t*this: 4My ,ervice reated4:
Toast#"*-324_"5-3-#show*-%
Log#d*2'3: 4onreate4-%
"layer = Media0layer#create*this: .#raw#braincand#-%
PGI Page 28
Patel Group of Institutions
SL-MC( 650017)
"layer#setLoo"ing*false-% !! ,et loo"ing
/
public void onFestroy*-
)
Toast#make2e$t*this: 4My ,ervice ,to""ed4:
Toast#"*-324_"5-3-#show*-%
Log#d*2'3: 4onFestroy4-%
"layer#sto"*-%
/
public void on,tart*+ntent intent: int startid-
)
Toast#make2e$t*this: 4My ,ervice ,tarted4:
Toast#"*-324_"5-3-#show*-%
Log#d*2'3: 4on,tart4-%
"layer#start*-%
/
/
.ine.java
package "gi#;ine%
import kmn#servicedemo#.%
import android#a""#&ctivity%
import android#content#+ntent%
import android#os#'undle%
import android#util#Log%
import android#view#View%
import android#view#View#3nlickListener%
import android#widget#'utton%
public class ;ine extends &ctivity implements 3nlickListener
)
!II @author JGGF I!
private static final ,tring 2'3 = 4,ervicesFemo4%
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
'utton start = *'utton-$indView'y+d*.#id#button1-%
'utton sto" = *'utton-$indView'y+d*.#id#button/-%
start#set3nlickListener*this-%
sto"#set3nlickListener*this-%
/
public void onlick*View v-
)
if*v#get+d*-==.#id#button1-
)
Log#d*2'3: 4onlick: starting srvice4-%
start,ervice*new +ntent*this: My,ervice#class--%
!!break%
PGI Page 29
Patel Group of Institutions
SL-MC( 650017)
/
if*v#get+d*-==.#id#button/-
)
Log#d*2'3: 4onlick: sto""ing srvice4-%
sto",ervice*new +ntent*this: My,ervice#class--%
!!break%
/
/
/
Exercise : 1/
Create an application that will have spinner with list of animation names. $n
selecting animation name that animation should affect on the images displayed
below.
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<.elativeLayout android:id="@!id/relatie"a#out1"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/bgcolor">
<,"inner android:id="@!id/spinner1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_align0arentTo"="true" android:layout_align0arent.ight="true"
android:layout_align0arentLe$t="true"
android:entries="@arra#/'nimation"><!,"inner>
<+mageView android:layout_width="wrap_content"
android:src="@drawable/icon" android:layout_height="wrap_content"
android:id="@!id/image%iew1" android:layout_centerVertical="true"
android:layout_center=oriContal="true"><!+mageView>
<!.elativeLayout>
<!LinearLayout>
PGI Page 30
Patel Group of Institutions
SL-MC( 650017)
~> in this problem we make anim folder to res. Directory and
create an xml file like following:
al0ha.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<al"ha
android:$rom&l"ha="0"
android:to&l"ha="1.0"
android:$ill&$ter="true"
android:duration="?000"
!>
<!set>
rotate.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:$romFegrees="0"
android:toFegrees="-1@0"
android:"ivotO=")0A"
android:"ivotJ=")0A"
android:duration="?000"
!>
<!set>
scale.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:"ivotO=")0A"
android:"ivotJ=")0A"
android:$romO,cale="1.0"
android:$romJ,cale="1.0"
android:toO,cale="/.0"
android:toJ,cale="/.0"
android:duration="/)00"
!>
<!set>
s0in.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:$romFegrees="0"
android:toFegrees="1@0"
android:duration="/000"
android:"ivotO=")0A"
PGI Page 31
Patel Group of Institutions
SL-MC( 650017)
android:"ivotJ=")0A"
android:re"eatount="infinite"
!>
<!set>
translate.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:$romOFelta="110"
android:toOFelta="-1/0"
android:duration="(000"
android:$ill&$ter="true"
android:$romJFelta="1<0"
android:toJFelta="0"
!>
<!set>
JAVA :
!enActivity.java :
package "gi#Ten%
import android#a""#&ctivity%
import android#os#'undle%
import android#view#View%
import android#view#animation#&nimation%
import android#view#animation#&nimationEtils%
import android#widget#&da"terView%
import android#widget#&da"terView#3n+tem,electedListener%
import android#widget#+mageView%
import android#widget#,"inner%
public class Ten&ctivity extends &ctivity implements 3n+tem,electedListener
)
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
,"inner s = *,"inner-$indView'y+d*.#id#spinner1-%
s#set3n+tem,electedListener*this-%
/
public void on+tem,elected*&da"terView<?> argB: View arg6: int arg7:
long arg8-
)
+mageView i = *+mageView-$indView'y+d*.#id#image%iew1-%
if*arg8==6-
)
&nimation a = &nimationEtils#load'nimation*this:
.#anim#alpha-%
i#start&nimation*a-%
/
if*arg8==7-
)
PGI Page 32
Patel Group of Institutions
SL-MC( 650017)
&nimation a = &nimationEtils#load'nimation*this:
.#anim#rotate-%
i#start&nimation*a-%
/
if*arg8==8-
)
&nimation a = &nimationEtils#load'nimation*this:
.#anim#scale-%
i#start&nimation*a-%
/
if*arg8==9-
)
&nimation a = &nimationEtils#load'nimation*this:
.#anim#spin-%
i#start&nimation*a-%
/
if*arg8==A-
)
&nimation a = &nimationEtils#load'nimation*this:
.#anim#translate-%
i#start&nimation*a-%
/
/
public void on;othing,elected*&da"terView<?> argB-
)
/
/
PGI Page 33
Patel Group of Institutions
SL-MC( 650017)
Exercise : 11
(nderstanding of (! ,
a. Create an (! such that one screen have list of all the types of cars.
b. $n selecting of any car name ne%t screen should show Car details like , name
launched date company name images"using gallery# if available show different
colors in which it is available.
Screen Shot:
PGI Page 34
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@!id/l"
android:entries="@arra#/carname"
!>
<!LinearLayout>
Main1.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@color/bgcolor">

<TableLayout android:orientation="ertical" android:gravity="center"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Table.ow android:layout_width="fill_parent"
android:layout_height="wrap_content">
PGI Page 35
Patel Group of Institutions
SL-MC( 650017)
<<allery android:id="@!id/e$amplegaller#"
android:layout_width="fill_parent"
android:layout_height="wrap_content"><!<allery>
<!Table.ow >

<Table.ow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@!id/t"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
!>
<!Table.ow>
<!TableLayout>



<!LinearLayout>
JAVA :
ElevenActivity.java :
package "gi#1leven%
import android#a""#&ctivity%
import android#content#+ntent%
import android#os#'undle%
import android#view#View%
import android#widget#&da"terView%
import android#widget#ListView%
import android#widget#Toast%
import android#widget#&da"terView#3n+temlickListener%
public class 1leven&ctivity extends &ctivity implements 3n+temlickListener
)
ListView lv%
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
lv=*ListView- $indView'y+d*.#id#l-%
lv#setlickable*true-%
lv#set3n+temlickListener*this-%
/
public void on+temlick*&da"terView<?> argB: View arg6: int arg7:
long arg8-
)
try
)
+nteger itn=+nteger#alue5f*arg7-%
,tring ext=itn#to,tring*-%
+ntent i = new +ntent*this:ar+n$o#class-%
i#"ut1xtra*4ar0os4: ext-%
start&ctivity*i-%
/
catch*1xce"tion e-
PGI Page 36
Patel Group of Institutions
SL-MC( 650017)
)
Toast#make2e$t*this: e#getMessage*-?4 3n+temlick4:
Toast#"*-324_"5-3-#show*-%
/
/
/
,ar1n%o.java
package "gi#1leven%
import android#a""#&ctivity%
import android#content#ontext%
import android#content#+ntent%
import android#content#res#Ty"ed&rray%
import android#os#'undle%
import android#view#View%
import android#view#View<rou"%
import android#widget#'ase&da"ter%
import android#widget#<allery%
import android#widget#+mageView%
import android#widget#TextView%
public class ar+n$o extends &ctivity
)
TextView tv%
private <allery gallery%
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main1-%

tv=*TextView- $indView'y+d*.#id#t-%
+ntent i=this#get+ntent*-%
,tring str =i#get,tring1xtra*4ar0os4-%
gallery = *<allery- $indView'y+d*.#id#e$amplegaller#-%
gallery#set&da"ter*new &dd+mg&d"*this:str--%
if*str#e2uals*4B4--
)
tv#setText*4;ame:Maruti ,uCuki PBB4?4Qn4?4Launched
Fate:6!8!6RPS4?4Qn4?4om"any ;ame:Maruti ,uCuki4?4Qnolors
&vailable:Qn>hiteQn'lueQn.edQnLight Jellow4-%
/
else if*str#e2uals*464--
)
tv#setText*4;ame:=yundai &ccent 1xecutive4?4Qn4?4Launched
Fate:6A!6B!7B6B4?4Qn4?4om"any ;ame:=yundai4?4Qnolors
&vailable:Qn<rayQn3cean 'lueQnMidnight 'lackQnMaganta 'lue4-%
/
else if*str#e2uals*474--
)
tv#setText*4;ame:hevrolet 'eat4?4Qn4?4Launched
Fate:66!66!7B664?4Qn4?4om"any ;ame:hevrolet4?4Qnolors
&vailable:QnMidnight 'lackQn&maCon <reenQn.oyal <oldQn,"ort .ed4-%
/
else
)
tv#setText*4;o car available4-%
/
/
PGI Page 37
Patel Group of Institutions
SL-MC( 650017)
/
class &dd+mg&d" extends 'ase&da"ter
)
int <al+tem'g%
private ontext cont%
,tring "ositionLast%
private +ntegerKL +mgidb = ).#drawable#b1: .#drawable#b/:
.#drawable#b1: .#drawable#b(/%
private +ntegerKL +mgidh = ).#drawable#h1: .#drawable#h/:
.#drawable#h1: .#drawable#h(/%
private +ntegerKL +mgidm = ).#drawable#m1: .#drawable#m/:
.#drawable#m1: .#drawable#m(/%
public &dd+mg&d"*ontext c:,tring "os-
)
cont = c%
"ositionLast="os%
Ty"ed&rray ty"&rray =
c#obtain,tyled&ttributes*.#styleable#3aller#2heme-%
<al+tem'g =
ty"&rray#get.esource+d*.#styleable#3aller#2heme_android_galler#7tem>ackgrou
nd: B-%
ty"&rray#recycle*-%
/
public int getount*-
)
if*"ositionLast#e2uals*4B4--
)
return +mgidm#length%
/
else if*"ositionLast#e2uals*464--
)
return +mgidh#length%
/
else if*"ositionLast#e2uals*474--
)
return +mgidb#length%
/
else
)
return +mgidm#length%
/
/
public 3bHect get+tem*int "osition- )
return "osition%
/
public long get+tem+d*int "osition- )
return "osition%
/
public View getView*int "osition: View convertView: View<rou" "arent-
)
+mageView imgView = new +mageView*cont-%
if*"ositionLast#e2uals*4B4--
)
imgView#set+mage.esource*+mgidmK"ositionL-%
/
PGI Page 38
Patel Group of Institutions
SL-MC( 650017)
else if*"ositionLast#e2uals*464--
)
imgView#set+mage.esource*+mgidhK"ositionL-%
/
else if*"ositionLast#e2uals*474--
)
imgView#set+mage.esource*+mgidbK"ositionL-%
/
else
)
imgView#set+mage.esource*+mgidmK"ositionL-%
/
!! (ixing width 5 height $or image to dis"lay
imgView#setLayout0arams*new <allery#Layout0arams*7BB: 7BB--%
imgView#set,caleTy"e*+mageView#,caleTy"e#672_BC-%
imgView#set'ackground.esource*<al+tem'g-%
return imgView%
/
/
Exercise : 12
(nderstanding content providers and permissions,
a. +ead phonebook contacts using content providers and display in list.
Screen Shot:
PGI Page 39
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@!id/list%iew1"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/bgcolor"><!ListView>
<!LinearLayout>
JAVA :
!2elveActivity.java :
package "gi#Twelve%
import Hava#util#&rrayList%
import android#a""#&ctivity%
import android#database#ursor%
import android#os#'undle%
PGI Page 40
Patel Group of Institutions
SL-MC( 650017)
import android#"rovider#ontactsontract%
import android#"rovider#ontactsontract#0honeLooku"%
import android#util#Log%
import android#widget#&rray&da"ter%
import android#widget#ListView%
public class Twelve&ctivity extends &ctivity
)

private &rrayList<,tring> list%
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
int H=B%
ursor
c=getontent.esolver*-#2uery*ontactsontract#ontacts#05-2*-2_,87: null:
null:null:null-%
list= new &rrayList<,tring>*-%
while*c#moveTo;ext*--
)
int nameindex=c#getolumn+ndex*0honeLooku"#;7:."'C_-'9*-%
,tring name=c#get,tring*nameindex-%
list#add*name-%
Log#d*4ontacts:4:name-%
/
ListView l=*ListView-$indView'y+d*.#id#list%iew1-%
&rray&da"ter<,tring> aa=new
&rray&da"ter<,tring>*this:android#.#layout#simple_list_item_1:list-%
l#set&da"ter*aa-%
/
/
Exercise : 1
+ead messages from the mobile and display it on the screen.
Screen Shot:
PGI Page 41
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!LinearLayout>
JAVA :
!hirteenActivity.java :
package "gi#Thirteen%
import android#a""#&ctivity%
import android#database#ursor%
PGI Page 42
Patel Group of Institutions
SL-MC( 650017)
import android#gra"hics#olor%
import android#net#Eri%
import android#os#'undle%
import android#widget#TextView%
public class Thirteen&ctivity extends &ctivity
)
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
TextView view = new TextView*this-%
Eri uri,M, = Eri#parse*4content:!!sms!inbox4-%
ursor c = getontent.esolver*-#2uery*uri,M,: null: null:
null:null-%
,tring sms = 44%
while *c#moveTo;ext*--
)
sms ?= 4(rom :4 ? c#get,tring*7- ? 4 : 4 ?
c#get,tring*66-?4Qn4%
/
view#setText*sms-%
view#set'ackgroundolor*olor#>",*-%
view#setTextolor*olor#=472*-%
setontentView*view-%
/
/
Exercise : 1"
Create an application to call specific entered number by user in the &ditTe%t
Screen Shot:
PGI Page 43
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
PGI Page 44
Patel Group of Institutions
SL-MC( 650017)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" android:background="@color/bgcolor">
<TableLayout android:layout_height="wrap_content"
android:id="@!id/table"a#out1" android:layout_width="match_parent">
<Table.ow android:id="@!id/table8ow1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@!id/te$t%iew1"
android:text&""earance="&android:attr/te$t'ppearance9edium"
android:text=".hone D"><!TextView>
<1ditText android:layout_height="wrap_content"
android:layout_width="/(0dp" android:in"utTy"e="phone"
android:id="@!id/t$tnumber" android:hint="eg. <<<8@//<(<"><!1ditText>
<!Table.ow>
<!TableLayout>
<'utton android:layout_height="wrap_content" android:id="@!id/btncall"
android:layout_width="wrap_content" android:text="9ake 0all"><!'utton>
<!LinearLayout>
JAVA :
Fo#rteenActivity.java :
package "gi#(ourteen%
import android#a""#&ctivity%
import android#content#+ntent%
import android#net#Eri%
import android#os#'undle%
import android#view#View%
import android#widget#'utton%
import android#widget#1ditText%
public class (ourteen&ctivity extends &ctivity
)

G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
'utton call = *'utton-$indView'y+d*.#id#btncall-%
call#set3nlickListener*new View#3nlickListener*-
)
public void onlick*View v-
)
1ditText txt = *1ditText-$indView'y+d*.#id#t$tnumber-%
Eri uri=Eri#parse*4tel:4?txt#getText*-#to,tring*--%
+ntent intent=new +ntent*+ntent#'0275-_0'"": uri-%
start&ctivity*intent-%
/
/-%
/
PGI Page 45
Patel Group of Institutions
SL-MC( 650017)
/
Exercise : 1$
Create an application that will create database with table of (ser credential.
Screen Shot:
XML :
PGI Page 46
Patel Group of Institutions
SL-MC( 650017)
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor">
<!LinearLayout>
JAVA :
Fi%teenActivity.java :
a"ackage "gi#(i$teen%
import android#a""#&ctivity%
import android#database#ursor%
import android#database#s2lite#,TLiteFatabase%
import android#gra"hics#olor%
import android#os#'undle%
import android#util#Log%
import android#widget#TextView%
public class (i$teen&ctivity extends &ctivity
)
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%

,TLiteFatabase myF'= null%
,tring Table;ame = 4myTable4%

,tring Fata=44%

!I reate a Fatabase# I!
try
)
myF' = this#o"en3rreateFatabase*4Fatabase;ame4:
95;*_.87%'2*: null-%

!I reate a Table in the Fatabase# I!
myF'#exec,TL*4.1&T1 T&'L1 +( ;3T 1O+,T, 4 ? Table;ame ?
4 *(ield6 V&.=&.: (ield7 +;T*8--%4-%
!I +nsert data to a TableI!
myF'#exec,TL*4+;,1.T +;T3 4 ? Table;ame ? 4 *(ield6:
(ield7-4 ? 4 V&LE1, *NJGGFN: 77-%4-%
!Iretrieve data $rom database I!
ursor c = myF'#rawTuery*4,1L1T I (.3M 4 ? Table;ame :
null-%

int olumn6 = c#getolumn+ndex*4(ield64-%
int olumn7 = c#getolumn+ndex*4(ield74-%
!! heck i$ our result was valid#
c#moveTo(irst*-%
if *c @= null-
)
PGI Page 47
Patel Group of Institutions
SL-MC( 650017)
!! Loo" through all .esults
do
)
,tring ;ame = c#get,tring*olumn6-%
int &ge = c#get+nt*olumn7-%
Fata =Fata ?;ame?4!4?&ge?4Qn4%
/while*c#moveTo;ext*--%
/
TextView tv = new TextView*this-%
tv#set'ackgroundolor*olor#>",*-%
tv#setTextolor*olor#=472*-%
tv#setText*Fata-%
setontentView*tv-%
/
catch*1xce"tion e-
)
Log#e*41rror4: 41rror4: e-%
/
finally
)
if *myF' @= null-
myF'#close*-%
/
/
/
3ro0!4.java :
package "gi#(i$teen%
import android#a""#&ctivity%
import android#database#s2lite#,TLiteFatabase%
import android#os#'undle%
import android#util#Log%
public class Fro"Tb extends &ctivity
)
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
,TLiteFatabase myF'= null%
,tring Table;ame = 4myTable4%
!II
I reate a Fatabase#
I I!
try
)
myF' = this#o"en3rreateFatabase*4Fatabase;ame4:
95;*_.87%'2*: null-%
myF'#exec,TL*4F.30 T&'L1 +( 1O+,T, 4?Table;ame?4%4-%
/
catch*1xce"tion e-
)
Log#e*41rror4: 41rror4: e-%
/
finally
PGI Page 48
Patel Group of Institutions
SL-MC( 650017)
)
if *myF' @= null-
myF'#close*-%
/
/
/
Exercise : 1&
Create an application to read file from asset folder and copy it in memory card.
Screen Shot:
XML :
s1.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
PGI Page 49
Patel Group of Institutions
SL-MC( 650017)
android:layout_height="fill_parent"
android:weight,um="1" android:background="@color/bgcolor">
<TextView android:text&""earance="&android:attr/te$t'ppearance"arge"
android:layout_height="wrap_content" android:text="2e$t%iew"
android:layout_weight="0.1?" android:layout_width="match_parent"
android:id="@!id/show2$t"><!TextView>
<!LinearLayout>
JAVA :
SixteenActivity.java :
package "gi#,ixteen%
import Hava#io#(ile%
import Hava#io#(ile3ut"ut,tream%
import Hava#io#+31xce"tion%
import Hava#io#+n"ut,tream%
import Hava#io#3ut"ut,tream%
import android#a""#&ctivity%
import android#content#+ntent%
import android#os#'undle%
import android#widget#Toast%
public class ,ixteen&ctivity extends &ctivity
)
(ile3ut"ut,tream $os %

G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#s1-%

try
)
,tring dest0ath = 4!sdcard!kmn#txt4%
(ile $ = new (ile*dest0ath-%
if *@$#exists*--
)
o"yF'* get'aseontext*-#get&ssets*-#o"en*4$ile#txt4-:new
(ile3ut"ut,tream*dest0ath--%
/
+ntent i = new
+ntent*,ixteen&ctivity#this:screen6#class-%
start&ctivity*i-%
/
catch *1xce"tion e-
)
PGI Page 50
Patel Group of Institutions
SL-MC( 650017)
Toast#make2e$t*,ixteen&ctivity#this: 4 4?e#to,tring*-:
Toast#"*-324_"5-3-#show*-%
/
/
public void o"yF'*+n"ut,tream in"ut,tream:3ut"ut,tream
out"ut,tream-throws +31xce"tion
)
!!DDDco"y 6U bytes at a timeDDD
byteKL bu$$er = new byteK6B79L%
int length%
while **length = in"ut,tream#read*bu$$er-- > B-
)
out"ut,tream#write*bu$$er: B: length-%
/
in"ut,tream#close*-%
out"ut,tream#close*-%
/
/
screen1.java :
package "gi#,ixteen%
import Hava#io#Fata+n"ut,tream%
import Hava#io#(ile%
import Hava#io#(ile+n"ut,tream%
import android#a""#&ctivity%
import android#os#'undle%
import android#os#1nvironment%
import android#widget#TextView%
import android#widget#Toast%
public class screen6 extends &ctivity
)
public void onreate*'undle b-
)
super#onreate*b-%
setontentView*.#layout#s1-%
try
)
(ile $ileFir = 1nvironment#get*$ternal:torage;irector#*-%
(ile directory = new (ile*$ileFir#get&bsolute0ath*--%
(ile $ile = new (ile*directory : 4kmn#txt4-%
(ile+n"ut,tream $is = new (ile+n"ut,tream*$ile-%
,tring str = null%
,tring'u$$er sbu$$er = new ,tring'u$$er*-%
Fata+n"ut,tream dataio = new Fata+n"ut,tream*$is-%
while**str = dataio#readLine*-- @= null-
)
sbu$$er#a""end*str ? 4Qn4-%
/
TextView txt=*TextView-$indView'y+d*.#id#show2$t-%
txt#setText*sbu$$er-%
/
catch *1xce"tion e-
)
Toast#make2e$t*this: 4 4?e#to,tring*-:
Toast#"*-324_"5-3-#show*-%
/
/
PGI Page 51
Patel Group of Institutions
SL-MC( 650017)
/
Exercise : 1'
Create an application that will play a media file from the memory card.
Screen Shot:
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weight,um="1">
PGI Page 52
Patel Group of Institutions
SL-MC( 650017)
<.elativeLayout android:id="@!id/relatie"a#out1"
android:layout_width="match_parent" android:background="@color/bgcolor"
android:layout_height="match_parent">
<'utton android:id="@!id/button/" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@!id/button1"
android:layout_alignLe$t="@!id/button1" android:layout_marginTo"="1@dp"
android:text="stop"><!'utton>
<Mediaontroller android:id="@!id/media0ontroller1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_above="@!id/button1" android:layout_center=oriContal="true"
android:layout_margin'ottom="/1dp"><!Mediaontroller>
<'utton android:layout_width="wrap_content" android:id="@!id/button1"
android:text="start" android:layout_height="wrap_content"
android:layout_align0arentTo"="true" android:layout_center=oriContal="true"
android:layout_marginTo"="1(8dp"><!'utton>
<!.elativeLayout>
<!LinearLayout>
JAVA :
SeventeenActivity.java :
package "gi#,eventeen%
import android#a""#&ctivity%
import android#content#ontext%
import android#media#Media0layer%
import android#os#'undle%
import android#view#View%
import android#widget#'utton%
import android#widget#Toast%
public class ,eventeen&ctivity extends &ctivity
)
Media0layer "layer=null%
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
'utton start=*'utton-$indView'y+d*.#id#button1-%
'utton sto"=*'utton-$indView'y+d*.#id#button/-%
get,ystem,ervice*ontext#',;75_:*8%70*-%
start#set3nlickListener*new View#3nlickListener*- )

public void onlick*View argB-
)
try
)
"layer=null%
"layer=new Media0layer*-%
,tring
audio(ile0ath=4!sdcard!braincandy#m"84%
"layer#setFata,ource*audio(ile0ath-%
"layer#"re"are*-%
"layer#start*-%
/
catch*1xce"tion e-
PGI Page 53
Patel Group of Institutions
SL-MC( 650017)
)
Toast#make2e$t*,eventeen&ctivity#this:44?e:Toast#"*-324_"5-3-#show*-%
/
/
/-%
sto"#set3nlickListener*new View#3nlickListener*-
)
public void onlick*View argB-
)
"layer#sto"*-%
/
/-%
/
/
56ste0 to add a m0 %ile to sdcard
17)oto the ddms
27clic8 on 0#sh 4#tton
7and select the )iven m0 %ile.
Exercise : 1(
Create an application to make !nsert update 'elete and retrieve operation on
the database.
Main.java:
package com#lebreemelow#$irsta""%
import android#os#'undle%
import android#a""#&ctivity%
import android#widget#'utton%
import android#widget#+mageView%
import android#view#View%
import android#content#+ntent%
import android#view#Menu%
public class Main&ctivity extends &ctivity )
private 'utton b%
private 'utton b6%
PGI Page 54
Patel Group of Institutions
SL-MC( 650017)
G3verride
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#actiit#_main-%
b=*'utton- $indView'y+d*.#id#button1-%
b#set3nlickListener*new View#3nlickListener*- )

public void onlick*View argB- )
start&ctivity*new
+ntent*Main&ctivity#this:+nsert#class--%
!!setontentView*.#layout#second-%
/
/-%
b6=*'utton- $indView'y+d*.#id#button/-%
b6#set3nlickListener*new View#3nlickListener*- )
public void onlick*View argB- )
!! TODO &utoDgenerated method stub
+ntent i=new +ntent*Main&ctivity#this:,how#class-%
start&ctivity*i-%
/
/-%
/
/
nsert.java:
package com#lebreemelow#$irsta""%
import android#a""#&ctivity%
import android#content#ontentValues%
import android#database#s2lite#,TLiteFatabase%
import android#os#'undle%
import android#view#View%
PGI Page 55
Patel Group of Institutions
SL-MC( 650017)
import android#widget#'utton%
import android#widget#1ditText%
import android#widget#Toast%
"
public class +nsert extends &ctivity )
private 1ditText name%
private 1ditText mno%
private 'utton submit%
G3verride
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#second-%
submit=*'utton- $indView'y+d*.#id#button1-%
name=*1ditText- $indView'y+d*.#id#edit2e$t1-%
mno=*1ditText- $indView'y+d*.#id#edit2e$t/-%

submit#set3nlickListener*new View#3nlickListener*- )
public void onlick*View argB- )
,TLiteFatabase
db=o"en3rreateFatabase*4Mydb#db4:95;*_.87%'2*:null-%
db#exec,TL*4create table i$ not exists Mytable*"name
varchar:"mo varchar-%4-%
!! TODO &utoDgenerated method stub
,tring n=name#getText*-#to,tring*-%
,tring m=mno#getText*-#to,tring*-%
ontentValues cv=new ontentValues*-%
cv#"ut*4"name4:n-%
cv#"ut*4"mo4:m-%
db#insert*4Mytable4:null:cv-%
PGI Page 56
Patel Group of Institutions
SL-MC( 650017)
!!db#close*-%
name#setText*44-%
mno#setText*44-%
Toast#make2e$t*get&""licationontext*-:4recodrd
save4:Toast#"*-324_"5-3-#show*-%
/
/-%
/
/
!how.java:
"ackage com#lebreemelow#$irsta""%
im"ort Hava#util#&rrayList%
im"ort android#a""#&ctivity%
im"ort android#content#+ntent%
im"ort android#database#ursor%
im"ort android#database#s2lite#,TLiteFatabase%
!!im"ort android#database#ursor%
!!im"ort android#database#s2lite#,TLiteFatabase%
im"ort android#os#'undle%
im"ort android#view#ontextMenu%
im"ort android#view#Menu+tem%
im"ort android#view#View%
im"ort android#view#ontextMenu#ontextMenu+n$o%
im"ort android#widget#&da"terView%
im"ort android#widget#&da"terView#&da"terontextMenu+n$o%
im"ort android#widget#&rray&da"ter%
im"ort android#widget#ListView%
im"ort android#widget#Toast%
"ublic class ,how extends &ctivity )
"rivate ListView l%
PGI Page 57
Patel Group of Institutions
SL-MC( 650017)
"rivate &rrayList<,tring> items=new &rrayList<,tring>*-%
,TLiteFatabase db%
G3verride
"ublic void onreate*'undle saved+nstance,tate- )
su"er#onreate*saved+nstance,tate-%
setontentView*.#layout#third-%
db=o"en3rreateFatabase*4Mydb#db4:M3F1_0.+V&T1:null-%
l=*ListView- $indView'y+d*.#id#listView6-%

ursor c=db#rawTuery*4select I $rom Mytable4:null-%
while*c#moveTo;ext*--
)
,tring n=c#get,tring*B-%
,tring m=c#get,tring*6-%
!!items#add*n?4 Qn 4?m-%
items#add*n-%

/
l#set&da"ter*new
&rray&da"ter<,tring>*this:android#.#layout#sim"le_list_item_6:
items--%
register(orontextMenu*l-%


/
"ublic void onreateontextMenu*ontextMenu menu: View v:
ontextMenu+n$o mi-
)
su"er#onreateontextMenu*menu: v:mi-%
i$*v#get+d*-==.#id#listView6-
)
menu#set=eaderTitle*4,elect 3"tion4-%
PGI Page 58
Patel Group of Institutions
SL-MC( 650017)
menu#add*B: 6: menu#;3;1:4.emove +tem4-%
menu#add*B:7:menu#;3;1:41dit +tem4-%
/
/
"ublic boolean onontext+tem,elected*Menu+tem item-
)
int itemid=item#get+tem+d*-%
i$*itemid==6-
)
&da"terView#&da"terontextMenu+n$o menuin$o%
menuin$o=*&da"terontextMenu+n$o- item#getMenu+n$o*-%
int index_id=menuin$o#"osition%
,tring user_id=items#get*index_id-%
db#exec,TL*4delete $rom Mytable where
"name=N4?user_id?4N4-%
Toast#makeText*this:user_id:Toast#L1;<T=_L3;<-#show*-%
/
else i$*itemid==7-
)
&da"terView#&da"terontextMenu+n$o menuin$o%
menuin$o=*&da"terontextMenu+n$o- item#getMenu+n$o*-%
int index_id=menuin$o#"osition%
,tring user_id=items#get*index_id-%
+ntent i=new +ntent*Third#this:(ourth#class-%
start&ctivity*i-%
Toast#makeText*this:41dit
Fata4:Toast#L1;<T=_L3;<-#show*-%
PGI Page 59
Patel Group of Institutions
SL-MC( 650017)
/
return su"er#onontext+tem,elected*item-%
/
/
"dit.java:
package com#lebreemelow#$irsta""%
import android#a""#&ctivity%
import android#content#ontentValues%
import android#database#ursor%
import android#database#s2lite#,TLiteFatabase%
import android#os#'undle%
import android#view#View%
import android#widget#'utton%
import android#widget#1ditText%
import android#widget#Toast%
public class 1dit extends &ctivity )
private 1ditText name%
private 1ditText mno%
private 'utton bedit%
,tring user_id%
G3verride
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#fourth-%
name=*1ditText- $indView'y+d*.#id#edit2e$t1-%
mno=*1ditText- $indView'y+d*.#id#edit2e$t(-%
bedit=*'utton- $indView'y+d*.#id#button(-%
final ,TLiteFatabase
db=o"en3rreateFatabase*4Mydb#db4:95;*_.87%'2*:null-%

PGI Page 60
Patel Group of Institutions
SL-MC( 650017)
ursor c=db#2uery*4Mytable4:null:4"name=?4:new ,tringKL)4aHay4/:
null: null: null-%
if*c@=null-
)
c#moveToLast*-%
/

name#setText*c#get,tring*B--%
mno#setText*c#get,tring*6--%
bedit#set3nlickListener*new View#3nlickListener*- )
public void onlick*View argB- )
ontentValues cv=new ontentValues*-%
cv#"ut*4"name4:name#getText*-#to,tring*--%
cv#"ut*4"mo4:mno#getText*-#to,tring*--%
db#u"date*4Mytable4:cv:4"name=?4:new ,tringKL
)4aHay4/-%
Toast#make2e$t*(ourth#this:4success$ully
u"date4:Toast#"*-324_"5-3-#show*-%
!! TODO &utoDgenerated method stub
/
/-%
/
/
Main#xml:
<.elativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".9ain'ctiit#" >
<'utton
PGI Page 61
Patel Group of Institutions
SL-MC( 650017)
android:id="@!id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_align0arentLe$t="true"
android:layout_align0arentTo"="true"
android:layout_marginLe$t="/(dp"
android:layout_marginTo"="1(dp"
android:background="@drawable/rect"
android:text ="7nsert data" !>
<'utton
android:id="@!id/button/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLe$t="@!id/button1"
android:layout_below="@!id/button1"
android:layout_marginTo"="1@dp"
android:text =":how data" !>
<!.elativeLayout>
nsert.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="ertical">
<TextView
android:id="@!id/te$t%iew1"
android:layout_width="fill_parent"
android:layout_height="1/dp"
PGI Page 62
Patel Group of Institutions
SL-MC( 650017)
android:text ="this is the second actiit#" !>
<1ditText
android:id="@!id/edit2e$t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<re2uest(ocus !>
<!1ditText>
<1ditText
android:id="@!id/edit2e$t/"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" !>
<'utton
android:id="@!id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="submit" !>
<!LinearLayout>
!how.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="ertical" >
<ListView
PGI Page 63
Patel Group of Institutions
SL-MC( 650017)
android:id="@!id/list%iew1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!ListView>
<!LinearLayout>
#pdate.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="ertical" >
<TextView
android:id="@!id/te$t%iew1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="-ame" !>
<1ditText
android:id="@!id/edit2e$t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<re2uest(ocus !>
<!1ditText>
PGI Page 64
Patel Group of Institutions
SL-MC( 650017)
<TextView
android:id="@!id/te$t%iew/"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="9obile" !>
<1ditText
android:id="@!id/edit2e$t("
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" !>
<'utton
android:id="@!id/button("
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text =",pdate" !>
<!LinearLayout>
Exercise : 1-
Create an application to read file from the sdcard and display that file content to
the screen.
PGI Page 65
Patel Group of Institutions
SL-MC( 650017)
Main.java:
package com#store#$ilestorage%
import Hava#io#(ile%
import Hava#io#(ile+n"ut,tream%
import Hava#io#(ile;ot(ound1xce"tion%
import Hava#io#(ile3ut"ut,tream%
import Hava#io#+31xce"tion%
import android#os#'undle%
import android#os#1nvironment%
import android#a""#&ctivity%
import android#view#Menu%
import android#view#View%
import android#widget#'utton%
import android#widget#1ditText%
import android#widget#TextView%
public class Main extends &ctivity )
public TextView t%
private 1ditText ed%
private 'utton b%
public ,tring in"ut%
G3verride
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
t=*TextView- $indView'y+d*.#id#te$t%iew1-%
ed=*1ditText- $indView'y+d*.#id#edit2e$t1-%
b=*'utton- $indView'y+d*.#id#button1-%
PGI Page 66
Patel Group of Institutions
SL-MC( 650017)
b#set3nlickListener*new View#3nlickListener*- )
G3verride
public void onlick*View argB- )
(ile sdcard=1nvironment#get*$ternal:torage;irector#*-%
if*sdcard#exists*- 55 sdcard#can>rite*--
)
(ile udir=new (ile*sdcard#get&bsolute0ath*-
?4!android,tring(ile4-%
udir#mkdir*-%
if*udir#exists*- 55 udir#can>rite*--
)
(ile $ile=new (ile*udir#get&bsolute0ath*-
?4!my,tring#txt4-%
try
)
$ile#create;ew(ile*-%
/
catch*+31xce"tion e-
)
e#"rint,tackTrace*-%
/
if*$ile#exists*- 55 $ile#can>rite*--
)
(ile3ut"ut,tream $os=null%
try
)
$os=new (ile3ut"ut,tream*$ile-%
in"ut=ed#getText*-#to,tring*-%
$os#write*in"ut#get'ytes*--%
/
catch*(ile;ot(ound1xce"tion e-
)
PGI Page 67
Patel Group of Institutions
SL-MC( 650017)
e#"rint,tackTrace*-%
/
catch*+31xce"tion e-
)
e#"rint,tackTrace*-%
/
finally
)
if*$os@=null-
)
try
)
$os#$lush*-%
$os#close*-%
/
catch*+31xce"tion e-
)
e#"rint,tackTrace*-%
/
/
/
/
else
)
t#setText*4unable to write4-%
/
/
else
)
t#setText*4unable to create $ile4-%
/
PGI Page 68
Patel Group of Institutions
SL-MC( 650017)
/
else
)
t#setText*4unable to create dir4-%
/
(ile r$ile=new (ile*4!sdcard!android,tring(ile!my,tring#txt4-%
if*r$ile#exists*- 55 r$ile#can.ead*--
)
(ile+n"ut,tream $is=null%
try
)
$is=new (ile+n"ut,tream*r$ile-%
byteKL readers=new byteK$is#available*-L%
while*$is#read*readers-@=D6-
)
/
t#setText*new ,tring*readers--%
/
catch*(ile;ot(ound1xce"tion e-
)
e#"rint,tackTrace*-%
/
catch*+31xce"tion e-
)
e#"rint,tackTrace*-%
/
finally
)
if*$is@=null-
)
try
)
$is#close*-%
PGI Page 69
Patel Group of Institutions
SL-MC( 650017)
/
catch*+31xce"tion e-
)
e#"rint,tackTrace*-%
/
/
/
/
else
)
t#setText*4unable to read $ile4-%
/
/
/-%
/
G3verride
public boolean onreate3"tionsMenu*Menu menu- )
getMenu+n$later*-#in$late*.#menu#main: menu-%
return true%
/
/
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="ertical">
<1ditText
PGI Page 70
Patel Group of Institutions
SL-MC( 650017)
android:id="@!id/edit2e$t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<re2uest(ocus !>
<!1ditText>
<'utton
android:id="@!id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="8ead data from file" !>
<TextView
android:id="@!id/te$t%iew1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" !>

<!LinearLayout>
Manifest.xml:
<mani$est xmlns:android="http://schemas.android.com/apk/res/android"
"ackage="com.store.filestorage"
android:versionode="1"
android:version;ame="1.0" >
<usesDsdk
android:min,dkVersion="8"
android:target,dkVersion="1)" !>
<usesD"ermission
android:name="android.permission.=872*_*B2*8-'"_:258'3*"!>
PGI Page 71
Patel Group of Institutions
SL-MC( 650017)
<a""lication
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@st#le/'pp2heme" >
<activity
android:name=".9ain"
android:label="@string/title_actiit#_main" >
<intentD$ilter>
<action android:name="android.intent.action.9'7-" !>
<category android:name="android.intent.categor#."',-04*8"
!>
<!intentD$ilter>
<!activity>
<!a""lication>
<!mani$est>
Exercise : 2/
Create an application to draw line on the screen as user drag his finger.
Screen Shot:
PGI Page 72
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
!>
<!LinearLayout>
JAVA :
!2entyActivity.java :
package "gi#Twenty%
import android#a""#&ctivity%
import android#os#'undle%
PGI Page 73
Patel Group of Institutions
SL-MC( 650017)
public class Twenty&ctivity extends &ctivity
)
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
super#onreate*saved+nstance,tate-%
setontentView*new 1x"loreTouch1vent*this: null--%
/
/
Ex0lore!o#chEvent.java :
package "gi#Twenty%
import android#content#ontext%
import android#gra"hics#anvas%
import android#gra"hics#olor%
import android#gra"hics#0aint%
import android#gra"hics#0ath%
import android#util#&ttribute,et%
import android#view#Motion1vent%
import android#view#View%
public class 1x"loreTouch1vent extends View
)
private 0aint "aint = new 0aint*-%
private 0ath "ath = new 0ath*-%
public 1x"loreTouch1vent*ontext context: &ttribute,et attrs-
)
super*context: attrs-%
"aint#set&nti&lias*true-%
"aint#set,troke>idth*V$-%
"aint#setolor*olor#=472*-%
"aint#set,tyle*0aint#,tyle#:285E*-%
"aint#set,trokeWoin*0aint#Woin#85,-;-%
/
protected void onFraw*anvas canvas-
)
canvas#drawolor*olor#>",*-%
canvas#draw0ath*"ath: "aint-%
/
public boolean onTouch1vent*Motion1vent event-
)
float eventO = event#getO*-%
float eventJ = event#getJ*-%
switch *event#get&ction*--
)
case Motion1vent#'0275-_;5=-:
"ath#moveTo*eventO: eventJ-%
return true%
case Motion1vent#'0275-_95%*:
"ath#lineTo*eventO: eventJ-%
break%
case Motion1vent#'0275-_,.:
!! nothing to do
break%
default:
return false%
/
!! ,chedules a re"aint#
PGI Page 74
Patel Group of Institutions
SL-MC( 650017)
invalidate*-%
return true%
/
/
Exercise : 21
Create an application to send message between two emulators.
Screen Shot:
Main$ctivity.java
package com#"gi#smsbook%
import android#os#'undle%
import android#a""#&ctivity%
import android#a""#0ending+ntent%
import android#content#'roadcast.eceiver%
import android#content#ontext%
import android#content#+ntent%
import android#content#+ntent(ilter%
import android#tele"hony#,msManager%
import android#tele"hony#,msMessage%
PGI Page 75
Patel Group of Institutions
SL-MC( 650017)
import android#util#Log%
import android#view#Menu%
import android#view#View%
import android#widget#'utton%
import android#widget#1ditText%
import android#widget#TextView%
public class Main&ctivity extends &ctivity )
'roadcast.eceiver rcvMsg,ent=null%
'roadcast.eceiver rcvMsg.ecei"t=null%
'roadcast.eceiver rcv+ncoming=null%
G3verride
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#actiit#_main-%
final 1ditText message=*1ditText- $indView'y+d*.#id#edit2e$t/-%
final 1ditText number=*1ditText- $indView'y+d*.#id#edit2e$t1-%
final ,msManager sms=,msManager#get;efault*-%
+ntent msg,ent=new +ntent*4&T+3;_M,<_,1;T4-%
+ntent msg.ecei"t=new +ntent*4&T+3;_M,<_.11+0T4-%
final 0ending+ntent "msg,ent=0ending+ntent#get>roadcast*this:
B:msg,ent:B-%
final 0ending+ntent "msg.ecei"t=0ending+ntent#get>roadcast*this:
B:msg.ecei"t:B-%
'utton sent,ms=*'utton- $indView'y+d*.#id#button1-%
sent,ms#set3nlickListener*new View#3nlickListener*- )
G3verride
public void onlick*View argB- )
,tring destination=number#getText*-#to,tring*-%
,tring msg=message#getText*-#to,tring*-%
sms#sendTextMessage*destination:null:msg:"msg,ent:"msg.ecei"t-%
PGI Page 76
Patel Group of Institutions
SL-MC( 650017)
!! TODO &utoDgenerated method stub
if*rcvMsg,ent@=null-
)
unregister.eceiver*rcvMsg,ent-%
rcvMsg,ent=null%
/
rcvMsg,ent=new 'roadcast.eceiver*- )
G3verride
public void on.eceive*ontext argB: +ntent
arg6- )
!! TODO &utoDgenerated method stub
int result=get.esultode*-%
if*result@=&ctivity#8*:,"2_5E-
)
Log#e*4tele"hony4:4sms send
$ailed=4?result-%
"msg.ecei"t#cancel*-%
/
else
)
Log#i*4tele"hony4:4sms messge
sent###4-%
message#setText*44-%
/
/
/%
register.eceiver*rcvMsg,ent:new
+ntent(ilter*4&T+3;_M,<_,1;T4--%
/
/-%
PGI Page 77
Patel Group of Institutions
SL-MC( 650017)
final TextView rmsg=*TextView- $indView'y+d*.#id#te$t%iew1-%

rcv+ncoming=new 'roadcast.eceiver*- )
G3verride
public void on.eceive*ontext argB: +ntent intent- )
!! TODO &utoDgenerated method stub
Log#i*4tele"hony4:4sms received4-%
'undle data=intent#get1xtras*-%
if*data@=null-
)
3bHect "dusKL=*3bHectKL- data#get*4"dus4-%
,tring message=4;ew Message:Qn4%
,tring sender=null%
for*3bHect "du : "dus-
)
,msMessage
"art=,msMessage#create6rom.du**byteKL-"du-%
message?="art#getFis"layMessage'ody*-%
if*sender==null-
)
sender="art#getFis"lay3riginating&ddress*-%
/
/
rmsg#setText*message?4$romQn4? sender-%
number#setText*sender-%
/
/
/%
register.eceiver*rcv+ncoming: new
+ntent(ilter*4android#"rovider#Tele"hony#,M,_.11+V1F4--%
/
PGI Page 78
Patel Group of Institutions
SL-MC( 650017)
G3verride
public boolean onreate3"tionsMenu*Menu menu- )
getMenu+n$later*-#in$late*.#menu#actiit#_main: menu-%
return true%
/
/
Main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="ertical">
<1ditText
android:id="@!id/edit2e$t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:in"utTy"e="phone" >
<re2uest(ocus !>
<!1ditText>
<1ditText
android:id="@!id/edit2e$t/"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLength="1(0"!>
PGI Page 79
Patel Group of Institutions
SL-MC( 650017)
<'utton
android:id="@!id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text =":ent :ms" !>
<TextView
android:id="@!id/te$t%iew1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="7ncoming 9essage ;raw here..." !>
<!LinearLayout>
Manifest.xml:
<mani$est xmlns:android="http://schemas.android.com/apk/res/android"
"ackage="com.sb.smsbook"
android:versionode="1"
android:version;ame="1.0" >
<usesDsdk
android:min,dkVersion="10"
android:target,dkVersion="1)" !>
<usesD"ermission android:name="android.permission.:*-;_:9:"!>
<usesD"ermission android:name="android.permission.8*0*7%*_:9:"!>
<a""lication
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@st#le/'pp2heme" >
<activity
android:name=".9ain'ctiit#"
PGI Page 80
Patel Group of Institutions
SL-MC( 650017)
android:label="@string/title_actiit#_main" >
<intentD$ilter>
<action android:name="android.intent.action.9'7-" !>
<category android:name="android.intent.categor#."',-04*8"
!>
<!intentD$ilter>
<!activity>
<!a""lication>
<!mani$est>
Ste0 to ,reat t2o em#lator and send sms.
17)oto 2indo2s96Android S3: and AV3 Man)aer.
27creat t2o em#lator and )ive the di%%erent name.
7start em#lator 4y clic8in) start 4#tton
7no2 enter the em#lator n#m4er to 0hone n#m4er and send it.
.ote:9 see the )ive *#t;#t 1ma)e..
Enjoy.........
Exercise : 22
Create an application to take picture using native application.
Screen Shot:
PGI Page 81
Patel Group of Institutions
SL-MC( 650017)
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!LinearLayout>
JAVA :
!2enty!2oActivity.java :
package "gi#TwentyTwo%
import Hava#io#(ile%
PGI Page 82
Patel Group of Institutions
SL-MC( 650017)
import android#a""#&ctivity%
import android#content#+ntent%
import android#net#Eri%
import android#os#'undle%
import android#os#1nvironment%
import android#"rovider#Media,tore%
public class TwentyTwo&ctivity extends &ctivity
)
,tring "ath%
G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
"ath = 1nvironment#get*$ternal:torage;irector#*- ? (ile#separator ?
4make_machine_exam"le#H"g4%
(ile $ile = new (ile* "ath -%
Eri out"ut(ileEri = Eri#from6ile* $ile -%
+ntent intent = new
+ntent*android#"rovider#Media,tore#'0275-_79'3*_0'.2,8* -%
intent#"ut1xtra* Media,tore#*B28'_5,2.,2: out"ut(ileEri -%
start&ctivity(or.esult* intent: B -%

/
/
Exercise : 2
Create an application to pick up any image from the native application gallery and
display it on the screen.
Main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<<ridView
android:id="@!id/grid%iew1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numolumns="auto_fit" >
<!<ridView>
<!LinearLayout>
Manifest.xml:
PGI Page 83
Patel Group of Institutions
SL-MC( 650017)
<mani$est xmlns:android="http://schemas.android.com/apk/res/android"
"ackage="com.imgmr#.imagememoer#"
android:versionode="1"
android:version;ame="1.0" >
<usesDsdk
android:min,dkVersion="10"
android:target,dkVersion="1)" !>
<usesD"ermission
android:name="android.permission.95,-2_,-95,-2_67"*:C:2*9:"!>
<a""lication
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@st#le/'pp2heme" >
<activity
android:name=".9ain'ctiit#"
android:label="@string/title_actiit#_main" >
<intentD$ilter>
<action android:name="android.intent.action.9'7-" !>
<category android:name="android.intent.categor#."',-04*8"
!>
<!intentD$ilter>
<!activity>
<!a""lication>
<!mani$est>
Main$ctivity.java:
package com#imgmry#imagememoery%
import android#net#Eri%
PGI Page 84
Patel Group of Institutions
SL-MC( 650017)
import android#os#'undle%
import android#"rovider#Media,tore%
import android#a""#&ctivity%
import android#content#ontext%
import android#database#ursor%
import android#view#Menu%
import android#view#View%
import android#view#View<rou"%
import android#widget#'ase&da"ter%
import android#widget#<ridView%
import android#widget#+mageView%
public class Main&ctivity extends &ctivity )
private ursor cursor%
private <ridView sdcardimg%
private int cindex%
G3verride
public void onreate*'undle saved+nstance,tate- )
super#onreate*saved+nstance,tate-%
setontentView*.#layout#actiit#_main-%
,tring "roHectionKL=)Media,tore#+mages#Thumbnails#_7;/%

cursor=this#getontent.esolver*-#2uery*Media,tore#+mages#Media#*B2*8-'"_05-
2*-2_,87:null:null:null:null-%
startManagingursor*cursor-%

cindex=cursor#getolumn+ndex3rThrow*Media,tore#+mages#Thumbnails#_7;-%
sdcardimg=*<ridView- $indView'y+d*.#id#grid%iew1-%
sdcardimg#set&da"ter*new +mage&da"ter*this--%
/
private class +mage&da"ter extends 'ase&da"ter
)
private ontext context%
public +mage&da"ter*ontext c-
PGI Page 85
Patel Group of Institutions
SL-MC( 650017)
)
context=c%
/
G3verride
public int getount*- )
!! TODO &utoDgenerated method stub
return cursor#getount*-%
/
G3verride
public 3bHect get+tem*int "osition- )
!! TODO &utoDgenerated method stub
return "osition%
/
G3verride
public long get+tem+d*int "osition- )
!! TODO &utoDgenerated method stub
return "osition%
/
G3verride
public View getView*int "osition: View convertView: View<rou"
"arent- )
+mageView "icview%
if*convertView==null-
)
"icview=new +mageView*context-%
cursor#moveTo0osition*"osition-%
int imgid=cursor#get+nt*cindex-%
"icview#set+mageE.+*Eri#with'ppended.ath*Media,tore#+mages#Thumbnails#*B2*8
-'"_05-2*-2_,87:44?imgid--%
"icview#set,caleTy"e*+mageView#,caleTy"e#672_0*-2*8-%
"icview#set0adding*P:P:P:P-%
!!"icview#setLayout0arams*new <ridView#-
PGI Page 86
Patel Group of Institutions
SL-MC( 650017)
/
else
)
"icview=*+mageView- convertView%
/
!! TODO &utoDgenerated method stub
return "icview%
/
/

/
Ste0 !o store ima)es on sdcard and scannin) in to the em#lator.
17clic8 on 0#sh on %ile onto the device 4#tton
27no2 select the iam)e an #0load it
7)oto the <3ev !ools< on yo#r em#lator and clic8 on <Media Scanner<.
"7no2 chec8 yo#r ima)e in <)allery<
$7r#n yo#r 0ro)ram
Exercise : 2"
Create an application to open any (+* inside the application and clicking on any
link from that (+l should not open )ative browser but that (+* should open the
same screen.
Screen Shot:
XML :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@!id/"inear"a#out01"
PGI Page 87
Patel Group of Institutions
SL-MC( 650017)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="ertical" android:gravity="center"
android:"adding="10p$" android:background="@color/bgcolor">
<>ebView android:id="@!id/webiew"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor"!>
<!LinearLayout>
JAVA :
!2entyFo#rActivity.java :
package com#"gi#Twenty(our%
import android#net#Eri%
import android#os#'undle%
import android#a""#&ctivity%
import android#content#+ntent%
import android#view#Menu%
import android#webkit#>ebView%
public class Twenty(our&ctivity extends &ctivity
) G3verride
public void onreate*'undle saved+nstance,tate-
)
super#onreate*saved+nstance,tate-%
setontentView*.#layout#main-%
+ntent brower+ntent=new
+ntent*+ntent#'0275-_%7*=:Eri#parse*4htt":!!www#google#co#in4--%
start&ctivity*brower+ntent-%
/
/
Manifest.xml:
<mani$est xmlns:android="http://schemas.android.com/apk/res/android"
"ackage="com.tf.gtutwent#("
android:versionode="1"
android:version;ame="1.0" >
<usesDsdk
android:min,dkVersion="10"
android:target,dkVersion="1)" !>
<usesD"ermission
android:name="android.permission.'00*::_-*2=58E_:2'2*"!>
<a""lication
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@st#le/'pp2heme" >
<activity
android:name=".9ain'ctiit#"
android:label="@string/title_actiit#_main" >
<intentD$ilter>
<action android:name="android.intent.action.9'7-" !>
PGI Page 88
Patel Group of Institutions
SL-MC( 650017)
<category android:name="android.intent.categor#."',-04*8"
!>
<!intentD$ilter>
<!activity>
<!a""lication>
<!mani$est>
PGI Page 89

Вам также может понравиться