Android Programs
Android Programs
1.Hello World
Aim:Write a program to Toast Hello World
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
a nd r oi d : or i en t a ti o n = " vertical "
tools:context = " . MainActivity " >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ c e n t e r I n P a r e n t = " true "
android:text = " Show Toast " / >
</ RelativeLayout >
MainActivity.java
package com . example . helloworld ;
1
}
UI Diagram
2
2.Addition of two Numbers
Aim:Write a program to add two numbers
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
tools:context = " . MainActivity " >
< TextView
android:id = " @ + id / textview1 "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ a l i g n P a r e n t T o p = " true "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " Enter number 1 "
android:textSize = " 18 sp " / >
< EditText
android:id = " @ + id / first "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / textview1 "
a n d r o i d : l a y o u t _ a l i g n P a r e n t T o p = " false "
a n d r o i d : l a y o u t _ a l i g n P a r e n t R i g h t = " false "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:ems = " 10 "
andro id:input Type = " number " / >
< TextView
android:id = " @ + id / textView2 "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / first "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " Enter Number 2 "
android:textSize = " 18 sp " / >
< EditText
android:id = " @ + id / second "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / textView2 "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:ems = " 10 "
andro id:input Type = " number " / >
3
< Button
android:id = " @ + id / buttonadd "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / second "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " Add " / >
< TextView
android:id = " @ + id / result "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / buttonadd "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:textSize = " 24 sp " / >
MainActivity.java
package com . abhay . addition ;
4
UI Diagram
5
3.Alert Box
Aim:Write a program to Display an alert box with OK and Cancel
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ c e n t e r I n P a r e n t = " true "
android:text = " Show Alert " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . alertbox ;
6
public void onClick ( DialogInterface arg0 , int arg1 ) {
Toast . makeText ( MainActivity . this , " You Clicked OK " ,
Toast . LENGTH_SHORT ) . show () ;
}
}) ;
b . setNegat iveButto n ( " Cancel " , new DialogInterface .
OnClickListener ()
{
@Override
public void onClick ( DialogInterface arg0 , int arg1 ) {
Toast . makeText ( MainActivity . this , " You Clicked Cancel " ,
Toast . LENGTH_SHORT ) . show () ;
}
}) ;
AlertDialog d = b . create () ;
d . show () ;
}
}) ;
}
}
UI Diagram
7
4.Menu Program
Aim:Write a Program to create menu with three menu items
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " / >
menu.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< menu xmlns:android = " http: // schemas . android . com / apk / res / android " >
< item
android:id = " @ + id / create "
android:title = " New " / >
< item
android:id = " @ + id / open "
android:title = " Open " / >
< item
android:id = " @ + id / exit "
android:title = " Exit " / >
</ menu >
MainActivity.java
package com . teachics . menu ;
@Override
protected void onCreate ( Bundle s av edI ns ta nce St at e ) {
super . onCreate ( s av edI ns ta nce St at e ) ;
setContentView ( R . layout . activity_main ) ;
}
@Override
public boolean o nC r ea t e Op t io n s Me n u ( Menu menu ) {
getMenuInflater () . inflate ( R . menu . menu , menu ) ;
return true ;
8
}
@Override
public boolean o n O p t i o n s I t e m S e l e c t e d ( MenuItem item ) {
int id = item . getItemId () ;
if ( id == R . id . create ) {
Toast . makeText ( MainActivity . this , " You Clicked New " ,
Toast . LENGTH_LONG ) . show () ;
}
else if ( id == R . id . open ) {
Toast . makeText ( MainActivity . this , " You Clicked Open " , Toast .
LENGTH_LONG ) . show () ;
}
else if ( id == R . id . exit ) {
Toast . makeText ( MainActivity . this , " You Clicked Exit " , Toast .
LENGTH_LONG ) . show () ;
this . finish () ;
}
return super . o n O p t i o n s I t e m S e l e c t e d ( item ) ;
}
}
9
UI Diagram
10
5.Radio Button
Aim:Write a Program to Select gender using radio button
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< RadioGroup
android:id = " @ + id / radiogrp "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content " >
< TextView
android:id = " @ + id / textView "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
android:text = " Select Your Gender " / >
< RadioButton
android:id = " @ + id / radiomale "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
android:text = " Male " / >
< RadioButton
android:id = " @ + id / radiofemale "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
android:text = " Female " / >
< RadioButton
android:id = " @ + id / radiononbin "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
android:text = " Non binary " / >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
android:text = " Submit " / >
</ RadioGroup >
</ RelativeLayout >
MainActivity.java
11
package com . teachics . a5radiobutton ;
12
UI Diagram
13
6.Spinner
Aim:Write a Program to Spin the four items
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< Spinner
android:id = " @ + id / spinner "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ a l i g n P a r e n t L e f t = " true "
a n d r o i d : l a y o u t _ a l i g n P a r e n t T o p = " true " / >
MainActivity.java
package com . teachics . spinner ;
@Override
protected void onCreate ( Bundle s av edI ns ta nce St at e ) {
super . onCreate ( s av edI ns ta nce St at e ) ;
setContentView ( R . layout . activity_main ) ;
spin = ( Spinner ) findViewById ( R . id . spinner ) ;
spin . s e t O n I t e m S e l e c t e d L i s t e n e r ( new AdapterView . O n I t e m S e l e c t e d L i s t e n e r ()
{
@Override
public void onItemSelected ( AdapterView <? > parent , View view , int
position , long id ) {
14
Toast . makeText ( MainActivity . this , " Selected Item " + items [
position ] , Toast . LENGTH_SHORT ) . show () ;
}
@Override
public void o nNothing Selected ( AdapterView <? > parent ) {
Toast . makeText ( MainActivity . this , " No item selected " , Toast .
LENGTH_SHORT ) . show () ;
}
}) ;
ArrayAdapter < String > adt = new ArrayAdapter < String >( this ,
android . R . layout . simple_list_item_1 , items ) ;
spin . setAdapter ( adt ) ;
}
}
15
UI Diagram
16
.
7.Check box
Aim:Write a Program to check the items listed
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< TextView
android:id = " @ + id / textView "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
android:text = " Areas of interest " / >
< CheckBox
android:id = " @ + id / crypt "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / textView "
android:text = " Cryptography " / >
< CheckBox
android:id = " @ + id / ai "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / crypt "
android:text = " Artificial Intelligence " / >
< CheckBox
android:id = " @ + id / ml "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / ai "
android:text = " Machine Learning " / >
< CheckBox
android:id = " @ + id / se "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / ml "
android:text = " Software Engineering " / >
< Button
17
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / se "
android:text = " Submit " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . checkbox ;
18
UI Diagram
19
8.Image View
Aim:Write a Program to Display images from local drive of the computer
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< ImageView
android:id = " @ + id / imageView "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " 400 dp "
app:srcCompat = " @drawable / image1 " / >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / imageView "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " Load Image 1 " / >
< Button
android:id = " @ + id / button2 "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / button "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " Load Image 2 " / >
< Button
android:id = " @ + id / button3 "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / button2 "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " Load Image 3 " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . imageview ;
20
import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . ImageView ;
}
public void onClick ( View button ) {
switch ( button . getId () )
{
case R . id . button :
imgview . setImageResource ( R . drawable . image1 ) ;
break ;
case R . id . button2 :
imgview . setImageResource ( R . drawable . image2 ) ;
break ;
case R . id . button3 :
imgview . setImageResource ( R . drawable . image3 ) ;
break ;
default :
break ;
}
}
}
21
UI Diagram
22
9.Fetch Text
Aim:Fetch data from an EditText and display it in a TextView
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< TextView
android:id = " @ + id / textView "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ a l i g n P a r e n t L e f t = " true "
a n d r o i d : l a y o u t _ a l i g n P a r e n t T o p = " true "
android:text = " Enter a string " / >
< EditText
android:id = " @ + id / editText "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / textView "
android:ems = " 10 "
andro id:input Type = " textPersonName " / >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / editText "
android:text = " Submit " / >
< TextView
android:id = " @ + id / text_display "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / button " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . edittext ;
23
import android . widget . Button ;
import android . widget . EditText ;
import android . widget . TextView ;
24
UI Diagram
25
10.Multiplication Table
Aim:Write a program to display multiplication table of a given number
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< TextView
android:id = " @ + id / textView "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ a l i g n P a r e n t L e f t = " true "
a n d r o i d : l a y o u t _ a l i g n P a r e n t T o p = " true "
android:text = " Enter a number " / >
< EditText
android:id = " @ + id / editTextNumber "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / textView "
android:ems = " 10 "
andro id:input Type = " number " / >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / editTextNumber "
android:text = " Find " / >
< TextView
android:id = " @ + id / textViewResult "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / button " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . m ul t ip l ic a t io n ta b l e ;
26
import android . widget . Button ;
import android . widget . EditText ;
import android . widget . TextView ;
@Override
protected void onCreate ( Bundle s av edI ns ta nce St at e ) {
super . onCreate ( s av edI ns ta nce St at e ) ;
setContentView ( R . layout . activity_main ) ;
27
UI Diagram
28
11.Background Colour
Aim:Write a program to Change the Background colour of the Activity
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
android:id = " @ + id / rel "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ c e n t e r I n P a r e n t = " true "
android:text = " Set Colour " / >
< Button
android:id = " @ + id / butclear "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ b e l o w = " @id / button "
a n d r o i d : l a y o u t _ c e n t e r I n P a r e n t = " true "
android:text = " Clear " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . backgroundcolor ;
29
butc =( Button ) findViewById ( R . id . butclear ) ;
rel =( RelativeLayout ) findViewById ( R . id . rel ) ;
but . set On Cl ick Li st ene r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
rel . set Ba ck gro un dC olo r ( Color . MAGENTA ) ;
}
}) ;
butc . s etO nC li ckL is te ne r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
rel . set Ba ck gro un dC olo r ( Color . WHITE ) ;
}
}) ;
}
}
30
UI Diagram
31
12. Background Image
Aim:Write a program to Change the Background image
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
android:id = " @ + id / rel "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ c e n t e r I n P a r e n t = " true "
android:text = " Change Background " / >
MainActivity.java
package com . teachics . backgroundimage ;
32
}
}
UI Diagram
33
13. Intent
Aim:Write a program to start another activity from your own activity using intent
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< TextView
android:id = " @ + id / textView "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ a b o v e = " @id / button "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " Home " / >
< Button
android:id = " @ + id / button "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ c e n t e r I n P a r e n t = " true "
android:text = " Open next activity " / >
</ RelativeLayout >
activity_next_activity.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . NextAcivity " >
< TextView
android:id = " @ + id / textView2 "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y ou t _ a b o v e = " @id / buttonnext "
a n d r o i d : l a y o u t _ c e n t e r H o r i z o n t a l = " true "
android:text = " New Activity " / >
< Button
android:id = " @ + id / buttonnext "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
34
a n d r o i d : l a y o u t _ c e n t e r I n P a r e n t = " true "
android:text = " Go back " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . intent ;
NextActivity.java
package com . teachics . intent ;
35
}) ;
}
}
UI Diagram
36
14.List View
Aim:Write a Program to Display the items in a list
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< ListView
android:id = " @ + id / list "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
a n d r o i d : l a y o u t _ a l i g n P a r e n t L e f t = " true "
a n d r o i d : l a y o u t _ a l i g n P a r e n t T o p = " true " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . listview ;
@Override
protected void onCreate ( Bundle s av edI ns ta nce St at e ) {
super . onCreate ( s av edI ns ta nce St at e ) ;
setContentView ( R . layout . activity_main ) ;
37
UI Diagram
38
15. IP Address
Aim:Write a program to Get IP Address of the device
activity_main.xml
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< RelativeLayout xmlns:android = " http: // schemas . android . com / apk / res / android "
xmlns:app = " http: // schemas . android . com / apk / res - auto "
xmlns:tools = " http: // schemas . android . com / tools "
a n d r o i d : l a y ou t _ w i d t h = " match_parent "
a n d r o i d : l a y o u t _ h e i g h t = " match_parent "
tools:context = " . MainActivity " >
< TextView
android:id = " @ + id / textView "
a n d r o i d : l a y ou t _ w i d t h = " wrap_content "
a n d r o i d : l a y o u t _ h e i g h t = " wrap_content "
a n d r o i d : l a y o u t _ a l i g n P a r e n t L e f t = " true "
a n d r o i d : l a y o u t _ a l i g n P a r e n t T o p = " true " / >
</ RelativeLayout >
MainActivity.java
package com . teachics . ipaddress ;
@Override
protected void onCreate ( Bundle s av edI ns ta nce St at e ) {
super . onCreate ( s av edI ns ta nce St at e ) ;
setContentView ( R . layout . activity_main ) ;
39
UI Diagram
40