0% found this document useful (0 votes)
3 views29 pages

Android

The document outlines various Android programming exercises, including creating a 'Hello World' toast message, adding two numbers, displaying the current date and time, and implementing alert dialogs. Each program includes source code snippets for XML layouts and Java classes, demonstrating basic Android UI components and event handling. The document serves as a guide for beginners to understand fundamental Android development concepts.

Uploaded by

sinubaby044
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views29 pages

Android

The document outlines various Android programming exercises, including creating a 'Hello World' toast message, adding two numbers, displaying the current date and time, and implementing alert dialogs. Each program includes source code snippets for XML layouts and Java classes, demonstrating basic Android UI components and event handling. The document serves as a guide for beginners to understand fundamental Android development concepts.

Uploaded by

sinubaby044
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

-1 22 -2-2o25

Program

Nrite a program to toast hello orld

Source Code

Str ings. xml

<resour ces>

<sring name helo. world"> Hello Worldl! 4s ing>


<string
name"bin ">Display /string>

</resource s>
activitj-main- m

<Aelativelayout tool :
Contex t -". MainAe tivity "
<Button

android : id
=@+ia/button1"

ondroid :tet '@sting/bn/>

YRelativelayout>

MainActivity: java
Public cla ss Main Aetiviy exends Aetivity $
Button btn;

btn =(Button) findViewByla CR [Link] 1);


bin-setOn
chkListher Cnew onClickLisner
cos
pulaie void onickcew argo)

Toast. make Text CMain


Activity. this,
"®shring/[Link] Toast.
[Link]).shewc;
2

22-2-202S
Prgram 2

Addition of tuo numbers.

Source code

strings. xml

Kresour ces>

name 1">First NO
Lotring /sting

Lstring nametv2" > second No 4sring

<string naes"btna">Addition <lstring >

</resources>

activity-main,m!

KPelatieloyout too l3: context =".Main Activity "


Feetview

android :id = +id/tetViews"

android itext =*sring /tv1>


(Tert View

android: id 'etid/tectView2
android: tert ostringl +v2" />

<Button

android:id="@+id /buttond "


androd :
tert @string /bnn" />

LTextvie
android :id ="+id /tetView />
3

MainActivity- java

Publ'ie class Main Activiy etend s Acviy


Edi+Tezt tet1;
Edit Text tat2 ;
Button btn

Tectview tiew;
Float ci
Protected
void onCreate CBun dle Savedlnstane Shate)

et1=Édi+Tezt) findviewByld CR-id. edit Te+1)i


txt2= CEditTe zA)
btn=CButton)
FndviewByld [Link] editTet) ;
findView Byld CR-
id.
tview button1);
(Tez+ve)
findViewBylad
[Link] 3);
btn.
setOncicklistner new onclickListherc) $
Publie void oncick (view argo)

a-float. par seFloat (tz ge tetc).


b= Float. toStng;
C=atb ',
parseFloas
:
[Link]).to St»ing c)

tview setText
[Link] Ce) :
3
program 3 22- 2- 2025

Write a program to display date and tine


using dialag box
Sour Ce code
Srigs. xml

cresources >

<string names* btnn"> DIspIAY DeTE


4String
Jresources>

activitymain. eml

CRelativelayous>

LButton

android :ide etid/atton1 4

android:tert =" SAring/ btnn" />


4Re lative Layouts

Main
Acivityjava

Piblie class Main Activity etend s Activity


Beutton btn ;
int
year,mo nth, day:
string
sysda+e;

proteetd void
onCreateCBundle Saved lnstance Sta fe)
btn= CButton)

btn =
setOnclik
findview Byld

Listhe
CiR-id-

Cnew oncick
button1) ;
Listher c) $
public void
onclick (view
argo
Calendar
ce CalendergetIn tance C))
Jear= C-ges [Link]):

month Cget CCalender. MoNTHJ:


aay * Cger Ccalender .[Link]. MONTN)
oate= day
Sys tr" tCmonth t1) "/ +yea
5
Alert Dialog .Bulider b-rew Aler+Dialoy-Builder CMain Activiy thi);
bi. e+Title ( sysdem Date ")

bl. Setmessage
C" Curvent
ystem Date is : "tsyslae) i
[Link] Button
Alert Dialog
d
(oine
=b1. cr
[Link] ,
nui)

eate ci
d1-showc);
6

Program 4 22-2-2o25

et Write a to
program Display an ale+ boz with ok and Canel

Source Code

activity [Link]

<Relativeayout>

<Button
android :id = "+id/button1"

android text Button" s


dRelativelayous

MainActivityjova
impleme nts onclickListher
e+ends Activis
public class MainAc tivity

Button b+n

Proteeted vod Oncreate (Bundale Savellnstan ce State

bn Cbutton) findviews Byld CR-id- button1) ;


[Link]
Listner Cthis);

public vod onclick(view arg o)


Alertpialog -Builde b1= new Aler +Dialog .Buileler Main Activitythi )
bd. SesefiHe C Sample Alert Box );
b2. serne ssagec'
tHello, Gaoodraor ning ");
b2.
serpositive
Button("ok",nu);
b1. SesNeq ahve
Buton C'Cancel ".nui);

AlertDialog

[Link]);
d4= [Link] c ;
3)
program 5 22- 2- 2025

Wite rrnu thvee


a programm to
Source Code

stringsXmI
cresources>

<sring nameCu scut 4sming >

Lswing name ps+" >paste yssing

LString nare cog"> copy fsig


4resourses)

re/rmenu/main. m

Cmenus
item

android id ="@id
andrbid :+iHe=@stringl eut
/eutlten"

android:id eid pasteltem


android:tezt =string fp+"/>

<item

andioid :id ="e+d [copylhen "


ondroid :4et =*estringlepy"

andnod :d
etid/google
android :tect e

noogle />
Ymenu >

Mainhctivithy java

piblie bocl ean ooptiontem Selected


int
(Menutitem){
id- iten
.get
ltemldc);

asmake Tet (main Activitthis " diched cog ma


else ifC id =[Link]-cutitem ){

Toast ma ke text CMain Activiy his,'You clicked cut menu IHem"


Toa [Link]).
showc)

else if [Link]- paste Hem )


Toast makeTes (mainActvity thisyou clickod poste menaT

Soo)-showc);
3
else if Cd=» R- idgoog le)i

Intent i>new Intent ([Link]-vIEw);


i-
se+Data Uri. par
se C"http/www:
Joogle -com d:
tartActivity ci);

3
4

Progr am 6 22-2- 2025

Write a program to select


gender 4sing radio button

Source Code

string [Link]

Cresources >

<string
namem"Male /sring
cstring name e "f
female
</string
string name =o">Other
</ss-ings

<Stting name =t" Enter Your ender


</strings
<sbring name = bnn" show
4sAring>

resourcess

Actvi-mainm
<Relativelayput

LTetview

android: id ="tid/tetview1"
android4et
LRadioGiroup
=sring /tet">
LRadioBuHon android :id
="tid/ radioGroup4")
android:
id="tid/radioo
androicl ttdt="e string/ m" >
LRadioButton

android :id " tid /vadiot "


android :text ="@string /f"/>
<Radio Button

android: id ="@+id /radio 2"

android: tect =@ String/o'/


anddididra
(Buton
android:ide"@+id / butHond

android:tez"e string/b+nn ">


</Aalativelayout >

MainActiv
ityjava
Public
Clags Main Activity
B4on etends Activity
btn)
Radio
Grop rdg i
Radioßutton rdb

protected void
Oncreate CBun ale
SavellngtaeState)i
btn=cButton)

rdg
(Radioroup)
find Views By ld [Link] .button1)

findview&ld [Link].
radio
[Link] on Group1)
clickListher Cnew
onlicdkListherCo1
publie vod Oncick
view
argo)i
int
selectedid =rdq -qetchacked
Radio 13utto nld Co >
rdb =(Ra dio
Buton)
findview
yld Cselected la)
Toastnavetet
CmainActivity-this,
rdb-gerteet c).Toast.
LEN
TH_SHORT). show c)

3;
3
Program7 22-2- 2025
Write
prgram to
Spin 4i+ems
Source code

MainActivit java
onltenSelected lisner
f
class Maintetivity extends Acivity implements
publie

Spinner sPi

TextView ti
Heybord;"Mouse", *printe:speaker
j;
String CJ itemg

protected void onGeate (Bundle Savedlnsance States)

Sp= Cspinner) find View By ld (R id. spinnei);

tv (etVie findView Byld [Link] 1);

Sp- seton itenelected LiStnes cthis)

adt hew
ArragAdapter csiring > ArraAdapter estring Cthiss android. Rlayout

Simple Iish- item.1,itens);

Sp. Set Adapler adt);

puble void onhemSelected CAdapteview ?> argo view arg% nt arg2 a)


log
t- serText Gitenlarg;
3

<Relative layout

<spinner

android:id <"@id/spinner4
2Tet view
androld :id ="@+ id/teView1"

android te "Large Te" />


/eelativelagout>
progr am g 23-2-2025

Nrte
program to Emul ate a Stoplwateh
SourceCode

(resour ces

(string
Yarings

<string name- Sop > 5Top 45tring


LString nae = Reses RESET
Yresources > 4shriy

activitmain m

<Rlativelayou>

LText View

androd :d = +id/ tevie4

andraiditert
o0:00:00

(Button

anclrod :id = @tid/ buttond

android:text =

LButton
android:id =@ tid /button 2

android:tezt =@ string bz

<Button
andvoid: id @tid/ buton g "
android : tert =@siring/ss >
Mainhctivitt jave

publie class
MainAciity extendo activiy t
private Buton Start B utton

priae Buton PauseButon;

private Button resesButton ;

privare Texview time Value;

private long sturt Time zol;

private Handler custom Handl ers new Handlerc

imelwamillisecondls =oL;
long

long timeSwapButfe -oL;

long qpdatedTime =0L;

public void onoeate CBundle Savedinstance State)

(Rid+evie);

.
timervalue = Cevien) finelview B ll

start But ton utton)


= B
findVieubyld (Rid-buttoni);

StartButton setonclick List her Cnew


vieuoncliek istherc)
Public void
ondick (view vie) i

StartTine= Systemclockayptime miiS


C) ;
CustomHandle
post Delayed Cyadate Time Thread, o) ;

reseButton= (Autton) indiewhyld CR-ld. butons)


reseBu Hon. setontiListher Cne [Link] Cliek Listhe cJi
public void onClick Cview veu)

timervalae. settet C'oo:00; 00"):


Pause ButHon

PauseButton
= CButton)
findView By ld
[Link].buton2) ;
SetOn Click Listher
(ew [Link] Listher (
public void onclick (ew view) $

time suap BufF += timein Muisecondo;

Custom [Link] cal backs CupdaseTinesThread):

private Runnable date Tiver Thyead =new Runnablet )5


public voicd run c{
time In milisecondS = Systemclbck. uptimmili s c) -staTe ;
ypdated Time tim SuapuF + time in mini seconds;

see s =tin) (ypdatedTime /1oo0);

int mins= Secs/6o

See 3 = Secs % 60

int milli seconcls= Cint) Cuodatecl Time


o lo00);

fimervalue. se+Tezt C4 mingt +Sting. ormat 024

+ Strin9 forma+ C' yo 63d", mjliseo nds)J

Custom Han dler pos Delaged Cthis, 6);


-9 23-2-202

s
Program

Nrite a to check the items listect


program

Source code

activiy-main.

<Rlativelayouts

<kheck Box
androd id="'+i4/checkBo"

androld: text @string la" />

(check Box
androd: id = tid /check Box 2

android : text"estring
/$J" >
checkBox
Box3
: id="+id check
android

android :tezt@ sing/p "/>


lcheekBox
tid check Bor4
android:id=

tezs =" string ls/>


android

<Button

Ondroid id =e tid/butten1"

android :tet string lbtn "/>

L1elakve Lay out>


Shings-xml

(resources>

(sAring name m"a" > Androd


</string>

<Sring name ="J> Java </4ring >

striy
name p"> PHe L|string >

name s"> shell Suript 4Btring

lresour ces >

Main Activity. java

public class Main Activiy extends


Activiy implemen s
onClickListne

checkßox chkt,chke, chk9, chk 4;


Button btn :
protected void on Create
CBundle Saved ln stanceStete

s4per. onereate
Csaved Instance
Stase):
5eContentvies CR layous.
activitymain)
chk2 >Ccheck
Box)
finaviewByld
[Link].
chk2= checkBona)
Ccheek Bor ) find View Byld
([Link]. checkBo
hks =lcheekBo) 2);
fndView By ld C [Link]. check Boes):
hka
cheek Bor)
findviewBy ld [Link].
checkBoe):
bn =CButton)
findview3yld [Link]
b+n. se4
button 4);
On clickListher
this):
3
13

publie void oncict Cview argo) {


SJrba new

Strb. append Candrold :").append [Link] khched c));


Strb append cln Java :")-append Cchkai schecked C)):

Strbappend C\n PHP :. append Cchlkg. i


scheckd c);
SArb- append CIn Shel senie : ").append Cchk4-iscleckedc

Toast. mak Te CmainA ctivity thi 2, S+rb.Jo57 ing c,Toast.


LENATH- LoNG). show C;
Program i 23-2-2025

Write
program
to
the Colour of the
change Backgrourd

acivity

Source Code

strings m)
<resources >

Lstring name =" 4 > Apply Backy roundl olor

L59riny name S2" > Reset


</sring

Jresources>

Qchvity- [Link]

<Relative Lay out android :id ="+id/r1")

<8utton
andro'd: id =" id/ buton

android :text = e string |


ss"/>

2Button
android: id = etid/B utton?"

android tezt =@ string/ 52 "/>


Yalative Layout>

Man Activityjava

public class MainActivity


etends Activity í
Button butn, bn2

pretected void
onCreate CBundle Savedlnstance stae) i

fina
id (R id.r1):|
Plativelayot ri CPelativeLayot) findviewoy
20

batn = CBaten) findViewBy ld CRid. button1) ;


butn. set On Click Listhar (new OnClick Listhar 1

public void Oniti ck Cview


ago) $

r1. ses Backgroundl Color


(Colbr. vE LL0w) ;

btn= (Button) find view By ld R-id- Button2);

btn2. set OncicelisthO new onciceListhorcs

public void on Click Cview

r1.5e Background color ([Link]) ;


33:

3
21

|-3-2025
Pprogram
AlM- Write
prog ram to
change the backgourd image
Source Code

activitymain- xm

<Rlativelayout >

Buton
androidd: id =`+id (button"
anroid :tex = Image 1

(Button
andr oid:id ="+id /button2"

androd:tert Image 2" />

(mageView

android iid=" @+id /imageiew 1"

android :5re "@drawable(bq 3

<Ilelaive layout >

MainAei iy java

public class Mainetivity extends Aciviy (mplenets vew onClick Lisherf

Imageview 4;
Button btn1, bn2
protected void onCreae CBundle Savedlnstancestate) s

"bm1=CButton) fmdvi ew By ld Caoid button);


btn 2 (Butto n) findview Byld CR idbutton 2):

Omagevieny AindView By,td [Link]):


btn1 .setOn cick Listher th'is):
btnz 5eton Click Listher (this);3
publie vod

Switeh
onClie

Cv geld C)
view

)i
) 22

case R\d buton

V settm ay «Resoure e (R. draable .by 1):

break ?

Case [Link] button2 :

V1SetmageResource (R:drawable. by :
break ;
Program 12
|
-3-2o25

Am :Write
pregram Display
image fhon local drive of the

Computer

ource coele

[Link] xmt

<Rlativelayot>

<image view
android :id = tid /image viewa "
andlroid: sre = " drawable / bg t")>

(B4Hon
an droid :id= O +id/btn Changel m age "
android: texte changelmage

</Relativelayout>

Main. AHviyjava

public class MainAcivity etends Activiy 2

Button button:

Imageview image s

publie void oncr ede CBumdle Stance Sta+e)


Sav ed In i

jmage=mageview) findview Byld

buton =C8 uton)


caid. imageviewa) ;
[Link]. btnchargelmage)
button. s e On Click Lis}her Cne
onclicleListerC)

publie void onclick Cview argo) L

image. setmage Re source CRdrawable. bg?))


3
b;
program i3

AM rite a
display muliplic ation tabie of a given number
program to

Source code

[Link]

E ditrex+

androd :id®+id /editTee1 "


android: inputtype "number " I>

<Button
android :id= " +id /bttong"
android: tezt "Buton "/>

<Tectvi ew
teztViewt
android:id"+idl

android:tect"

PRlativelayou>

Main Activity java

public class Main Activity exten ds Activ ity2

Buton

Tet
int
view

ans
btn;

=
t;
1

proteced void
on Create (Bundle Sav ed In stanceState) §
tetCEdiTez) find ViewByld CR:id edit Tex4) ;
bn =Coutton) fndview Byld (R-id button4 J:
+ztv = CTet view) view Byld CR-id .tezview1):
fined

btn setOncdick Lisner Crew onclick Li sther OE


25
public v oic onCick Cview
argo)

sringBuffer temp ne String Bffer C)

String num t geTex C). to string (:


int n=[Link] (n um)

for Cint i =1;i2=10 ;it+)1


ans= *n):
temp. qppend (i +"x'+n + = "
4 ang +"\n ):
tetv. se+ Text Gemp)
26

program 14

AM :Nrite phgram to Get lp address of the device

sourte Code

[Link]!

<Relative Layout

(But ton
android:id ="tid buton1

android :texs= het p Address" />

LTetview

android : iele eid/tex+View'

android :text "LargeTe xt" /s

/Pelative Layout>

MainActviy java

public class Main Actiity eztends Aivity

Button buttn;

Tezsview ttview

protecs ed void onGeate CBundle Saved lnstanceState) s

butn =(Button) fndview By ld (Rid. butt on):

[Link] Listher hew on dickListherCo $

public void
onclick Cview arg o)

teuview (rexview) findvie w BylaCR-id:tee view);

wAManager = (wifManger)
string ipAddress=
wifiManager
for matter
ge sysemserie
lS
.fornatIPAddress
(wi Manager getConn econ
nfoc)ger lPAddre ss C) )
27

+ztview. serTet Your Device iP Acldr es5 : ip Addrecs ):


24

Program is

AIM

List view - write a prog ram to 4 ems in a ligh


display 7e

Source Code

activitymain. em!

<RelativelLdypu>

LListview >

android :id=@+id i view1

<Jis+ View

<| palative Logout>

M
ainAciviyjava

Main etends ListA ctivity S


Public class Activity

Shatic final s}ring CJ FRUI TS= new S+ring I {apple", Avs Cado3anam

Blueberry", coLonut, "Durian",Guava" <iwifruit, Jackfute.

mango, "olive", " pear ",Sugar apple" :


public
void oaGeate CBundle Savedlnstantestye)

S4per. onCreose avedlnstan castase) :

ser List Adaer Cnew ArrayaAapler < Str ing Cohis, RiLayot aivityain,

FRUTS ));
is+Vie
Lis+View gelisivie cs;

Lis+yew. serTeztFiterEnablad Corue);

Listview set onl emCick Li sther Cw onhem CickListher C) $


public void onitem cllckbittrAdeptrlew
22> parent, Vie v

in posio9, long ia)


24.

Toas+ MakeTe lae+ A çpk cation ,


(oeesC) Lsasie)vie)ge Teet().

Toast NGTH- SBoRT).showo;

You might also like