0% found this document useful (0 votes)
13 views

Android Programs

This document provides code examples for basic Android programming concepts including displaying a "Hello World" toast notification, adding two numbers, and displaying an alert box. The code examples include XML layout files, Java activity code, and UI diagrams. For the "Hello World" example, it shows how to create a button in the XML layout that displays a toast with text when clicked from the Java activity code. For adding numbers, it shows how to create EditText fields to input numbers and display the result. And for the alert box, it demonstrates how to create a button that displays an alert dialog with OK and Cancel buttons when clicked.

Uploaded by

manzoormachu369
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Android Programs

This document provides code examples for basic Android programming concepts including displaying a "Hello World" toast notification, adding two numbers, and displaying an alert box. The code examples include XML layout files, Java activity code, and UI diagrams. For the "Hello World" example, it shows how to create a button in the XML layout that displays a toast with text when clicked from the Java activity code. For adding numbers, it shows how to create EditText fields to input numbers and display the result. And for the alert box, it demonstrates how to create a button that displays an alert dialog with OK and Cancel buttons when clicked.

Uploaded by

manzoormachu369
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Android Programming

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . View ;
import android . widget . Button ;
import android . widget . Toast ;

public class MainActivity extends Ap pCompatAc tivity {


Button but ;
@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 ) ;
but =( Button ) findViewById ( R . id . button ) ;
but . set On Cl ick Li st ene r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
Toast . makeText ( MainActivity . this , " Hello World " , Toast . LENGTH_LONG
) . show () ;
}
}) ;
}

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 " / >

</ RelativeLayout >

MainActivity.java
package com . abhay . addition ;

import androidx . appcompat . app . AppComp atActivi ty ;


import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . EditText ;
import android . widget . TextView ;

public class MainActivity extends Ap pCompatAc tivity {


EditText firstnum , secondnum ;
TextView r ;
Button bt ;
double a ,b , c ;
@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 ) ;
firstnum =( EditText ) findViewById ( R . id . first ) ;
secondnum =( EditText ) findViewById ( R . id . second ) ;
bt =( Button ) findViewById ( R . id . buttonadd ) ;
r =( TextView ) findViewById ( R . id . result ) ;
bt . s et OnC li ck Lis te ne r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
a = Double . parseDouble ( firstnum . getText () . toString () ) ;
b = Double . parseDouble ( secondnum . getText () . toString () ) ;
c=a+b;
r . setText ( " Sum = " + c ) ;
}
}) ;
}
}

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 ;

import androidx . appcompat . app . AlertDialog ;


import androidx . appcompat . app . AppComp atActivi ty ;

import android . content . DialogInterface ;


import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . Toast ;

public class MainActivity extends Ap pCompatAc tivity {


Button but ;
@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 ) ;
but =( Button ) findViewById ( R . id . button ) ;
but . set On Cl ick Li st ene r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
AlertDialog . Builder b = new AlertDialog . Builder ( MainActivity . this
);
b . setTitle ( " Alert " ) ;
b . setMessage ( " Are you sure ? " ) ;
b . setPosit iveButton ( " OK " , new DialogInterface . OnClickListener ()
{
@Override

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . Menu ;
import android . view . MenuItem ;
import android . widget . Toast ;

public class MainActivity extends Ap pCompatA ctivity {

@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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . View ;
import android . widget . Button ;
import android . widget . RadioButton ;
import android . widget . RadioGroup ;
import android . widget . Toast ;

public class MainActivity extends Ap pCompatA ctivity {


Button but ;
RadioGroup rgroup ;
@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 ) ;
but =( Button ) findViewById ( R . id . button ) ;
rgroup =( RadioGroup ) findViewById ( R . id . radiogrp ) ;
but . set On Cl ick Li st ene r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
int selected = rgroup . g e t C h e c k e d R a d i o B u t t o n I d () ;
RadioButton radio =( RadioButton ) findViewById ( selected ) ;
Toast . makeText ( MainActivity . this , " You selected : " + radio . getText
() , Toast . LENGTH_LONG ) . show () ;
}
}) ;
}
}

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 " / >

</ RelativeLayout >

MainActivity.java
package com . teachics . spinner ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . View ;
import android . widget . AdapterView ;
import android . widget . ArrayAdapter ;
import android . widget . Spinner ;
import android . widget . TextView ;
import android . widget . Toast ;

public class MainActivity extends Ap pCompatA ctivity {


Spinner spin ;
String [] items = { " Machine Learning " ," Artificial Intelligence " ," Cyber
security " ," Data Analytics " ," Bio Informatics " };

@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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . View ;
import android . widget . Button ;
import android . widget . CheckBox ;
import android . widget . Toast ;

public class MainActivity extends Ap pCompatA ctivity {


CheckBox cr , ai , ml , se ;
Button but ;
@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 ) ;

cr =( CheckBox ) findViewById ( R . id . crypt ) ;


ai =( CheckBox ) findViewById ( R . id . ai ) ;
ml =( CheckBox ) findViewById ( R . id . ml ) ;
se =( CheckBox ) findViewById ( R . id . se ) ;
but =( Button ) findViewById ( R . id . button ) ;

but . set On Cl ick Li st ene r ( new View . OnClickListener () {


@Override
public void onClick ( View v ) {
String res = " Crptography : " + cr . isChecked () + " \ n Artificial
Intelligence : " + ai . isChecked () + " \ n Machine Learning : " + ml . isChecked () + " \ n
Software Engineering : " + se . isChecked () ;
Toast . makeText ( MainActivity . this , res , Toast . LENGTH_LONG ) . show ()
;
}
}) ;
}
}

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . media . Image ;

20
import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . ImageView ;

public class MainActivity extends Ap pCompatA ctivity implements View .


OnClickListener {
Button but1 , but2 , but3 ;
ImageView imgview ;
@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 ) ;

but1 =( Button ) findViewById ( R . id . button ) ;


but2 =( Button ) findViewById ( R . id . button2 ) ;
but3 =( Button ) findViewById ( R . id . button3 ) ;
imgview =( ImageView ) findViewById ( R . id . imageView ) ;

but1 . s etO nC li ckL is te ne r ( this ) ;


but2 . s etO nC li ckL is te ne r ( this ) ;
but3 . s etO nC li ckL is te ne r ( this ) ;

}
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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . View ;

23
import android . widget . Button ;
import android . widget . EditText ;
import android . widget . TextView ;

public class MainActivity extends Ap pCompatA ctivity {


EditText edt ;
Button but ;
TextView txt ;
@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 ) ;

edt =( EditText ) findViewById ( R . id . editText ) ;


but =( Button ) findViewById ( R . id . button ) ;
txt =( TextView ) findViewById ( R . id . text_display ) ;

but . set On Cl ick Li st ene r ( new View . OnClickListener () {


@Override
public void onClick ( View v ) {
txt . setText ( edt . getText () . toString () ) ;
}
}) ;
}
}

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . View ;

26
import android . widget . Button ;
import android . widget . EditText ;
import android . widget . TextView ;

public class MainActivity extends Ap pCompatA ctivity {


TextView txtres ;
Button but ;
EditText edtnum ;

@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 ) ;

txtres = ( TextView ) findViewById ( R . id . textViewResult ) ;


edtnum = ( EditText ) findViewById ( R . id . editTextNumber ) ;
but = ( Button ) findViewById ( R . id . button ) ;

but . set On Cl ick Li st ene r ( new View . OnClickListener () {


@Override
public void onClick ( View v ) {
int num = Integer . parseInt ( edtnum . getText () . toString () ) ;
txtres . setText ( " " ) ;
for ( int i =1; i <11; i ++) {
txtres . append ( i + " * " + num + " = " + i * num + " \ n " ) ;
}
}
}) ;
}
}

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . graphics . Color ;


import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . RelativeLayout ;

public class MainActivity extends Ap pCompatAc tivity {


Button but , butc ;
RelativeLayout rel ;
@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 ) ;

but =( Button ) findViewById ( R . id . button ) ;

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 " / >

</ RelativeLayout >

MainActivity.java
package com . teachics . backgroundimage ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . graphics . Color ;


import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . RelativeLayout ;

public class MainActivity extends Ap pCompatAc tivity {


Button but ;
RelativeLayout rel ;
@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 ) ;

but =( Button ) findViewById ( R . id . button ) ;


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 . s e t B a c k g r o u n d R e s o u r c e ( R . drawable . cat ) ;
}
}) ;

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . content . Intent ;


import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;

public class MainActivity extends Ap pCompatA ctivity {


Button but ;
@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 ) ;
but =( Button ) findViewById ( R . id . button ) ;
but . set On Cl ick Li st ene r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
Intent i = new Intent ( MainActivity . this , NextAcivity . class ) ;
startActivity ( i ) ;
}
}) ;
}
}

NextActivity.java
package com . teachics . intent ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . view . View ;
import android . widget . Button ;

public class NextAcivity extends Ap pCompatA ctivity {


Button but ;
@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 . a c t i v i t y _ n e x t _ a c i v i t y ) ;
but =( Button ) findViewById ( R . id . buttonnext ) ;
but . set On Cl ick Li st ene r ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
finish () ;
}

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . os . Bundle ;


import android . widget . ArrayAdapter ;
import android . widget . ListView ;

public class MainActivity extends Ap pCompatA ctivity {


ListView listv ;

@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 ) ;

listv = findViewById ( R . id . list ) ;


String [] items = { " Machine Learning " , " Artificial Intelligence " , " Cyber
security " , " Data Analytics " , " Bio Informatics " };
ArrayAdapter < String > adpt = new ArrayAdapter < String >( MainActivity . this ,
android . R . layout . simple_list_item_1 , android . R . id . text1 , items ) ;
listv . setAdapter ( adpt ) ;
}
}

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 ;

import androidx . appcompat . app . AppComp atActivi ty ;

import android . net . wifi . WifiInfo ;


import android . net . wifi . WifiManager ;
import android . os . Bundle ;
import android . text . format . Formatter ;
import android . widget . TextView ;

public class MainActivity extends Ap pCompatA ctivity {


TextView txtip ;

@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 ) ;

txtip = findViewById ( R . id . textView ) ;

WifiManager wifiManager = ( WifiManager ) g e t A p p l i c a t i o n C o n t e x t () .


getSystemService ( WIFI_SERVICE ) ;
String ipAddress = Formatter . formatIpAddress ( wifiManager .
getCo nnection Info () . getIpAddress () ) ;

txtip . setText ( " Your device IP address " + ipAddress ) ;


}
}

39
UI Diagram

40

You might also like