Professional Documents
Culture Documents
Mad Lab
Mad Lab
Mad Lab
J2ME Introduction
J2ME stands for Java 2 Micro Edition. It is Java's specification
for devices which have limited processing power, storage capabilities
and intermittent or fairly low-bandwidth network connections. These
include mobile phones, pagers, wireless devices and set-top boxes
among others. A key benefit of using J2ME is compatibility with all
Java-enabled devices. Motorola, Nokia, Panasonic all have Java-
enabled devices. The Java Community Process Program used two
approaches for addressing the needs of small computing devices.
J2ME Configuration
J2ME Profiles
J2ME Configuration:
1
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
J2ME Profile:
J2ME Architecture:
JVM
Operating System (Bottom Layer).
2
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Midlet
Giving access to classes of the JVM and CLDC for Each member of the
MIDlet suite.
Makes the Java archive (JAR) file and the Java application descriptor
(JAD) file available to members of the MIDlet suite.
JAR Files:
JAR stands for Java Archive file. It is nothing but collection of all java
classes which are related to particular application. This Jar file includes a
special file known as Manifest file which contains information of class files
included in the Jar file. Nine attributes are defined in this file. The first six
attributes are necessary. The files extension is changed to .mf when the
MIDlet is prepared for deployment. All entries are Name: Value pairs. Each
pair must be terminated with a carriage return. Whitespace between the colon
and the attribute value is ignored. Some of Necessary Attributes of a Manifest
File are:
JAD Files:
3
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
the JAD file must match the same attributes in the manifest. All entries are
name:value pairs. Necessary Attributes are
A MIDlet is a class that extends MIDlet class & is the interface between
application statements & run-time environment, which is controlled by
application manager. Basic structure of Midlet program is as follows:
A MIDlet class must contain three abstract methods that are called by
the application manager to manage the life cycle of the MIDlet.
4
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Click on the new project button beside the menu like below
Create a Source code file in java ME language and save in the path
\j2mewtk\2.5.2\apps\HelloworldProject\src
5
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
6
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 01:
Implementation of J2ME program to show how to change Font
size & Color
Theory:
Canvas Class [javax.microedition.lcdui]:
Source Code:
7
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
import
javax.microedition.lcdui.*;
import
javax.microedition.midlet.*;
{ fc.addCommand(exit);
fc.setCommandListener(t
his); dis.setCurrent(fc);
}
public void
pauseApp() { }
protected void destroyApp(boolean unconditional)
notifyDestroyed()
{ ; }
notifyDestroyed();
}
}
class FontCanvas extends Canvas
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xFFFFFF);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_
PLAIN,
Font.SIZE_LARGE));
g.drawString("Welcome",0,
0,0); g.setColor(0x00FFFF);
8
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC,
Font.SIZE_SMALL));
g.drawString("to",0,20
,0);
g.setColor(0xFFFF00);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_B
OLD,
Font.SIZE_LARGE));
g.drawString("CSE",0,4
0,0);
}
Output:
9
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 02:
Theory:
List Class [javax.microedition.lcdui]:
An alert is a screen that shows data to the user and waits for a
certain period of time before proceeding to the next Displayable. An
alert can contain a text string and an image. The intended use of Alert
is to inform the user about errors and other exceptional conditions.
10
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Source Code:
package week2;
import
javax.microedition.lcdui.*;
import
javax.microedition.midlet.*;
public class Menu1Midlet extends MIDlet implements CommandListener
{
Display dis=Display.getDisplay(this);
List menu=new List("Edit Menu", Choice.IMPLICIT); Command
exit=new Command("Exit App",Command.EXIT,0); public void
showItem(String item)
msg.setType(AlertType.CONFIRMATI
ON); msg.setString("You have
Selected:\n"+item);
dis.setCurrent(msg);
public Menu1Midlet() {
} public void
startApp()
{ menu.append("Cut", null);
menu.append("Copy", null);
menu.append("Paste", null);
menu.append("Delete", null);
menu.append("Select All",
null); menu.append("Unselect
All", null);
menu.addCommand(exit);
menu.setCommandListener(t
his); dis.setCurrent(menu);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional)
{notifyDestroyed(); }
11
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
notifyDestroye
d(); else
showItem(menu.getString(menu.getSelectedIndex()));
}
}
Output:
12
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 03:
Theory:
ChoiceGroup Class [javax.microedition.lcdui]:
13
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Source Code:
package week3;
import
javax.microedition.midlet.*;
import
javax.microedition.lcdui.*;
public class Menu2Midlet extends MIDlet implements ItemStateListener
{
Display dis=Display.getDisplay(this);
public Menu2Midlet() { }
14
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
{s menu1.append("Cut", null);
menu1.append("Copy", null);
menu1.append("Paste", null);
menu1.append("Delete", null);
menu2.append("Select All",
null); menu2.append("Unselect
All", null);
menu2.setSelectedIndex(1,
true); form.append(menu1);
form.append(menu2);
form.append(msg);
form.addCommand(exit);
form.setItemStateListener(this)
; dis.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional)
{notifyDestroyed(); }
{ boolean[] ch1=new
boolean[menu1.size()]; boolean[]
ch2=new boolean[menu2.size()];
menu1.getSelectedFlags(ch1);
menu2.getSelectedFlags(ch2);
String s="";
for(int
i=0;i<4;i++) {
if(item==menu2)
ch1[i]=ch2[0];
s=s+ "\n"+ menu1.getString(i)+": "+ch1[i];
}
f(item==menu2)
menu1.setSelectedFlags(
ch1); msg.setText("Status of
Items:"+s);
}}
15
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Output:
16
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 04:
Theory:
TextField Class [javax.microedition.lcdui]:
Source Code:
package week4;
import
javax.microedition.midlet.*;
import
javax.microedition.lcdui.*;
public class BarGraphMidlet extends MIDlet implements
CommandListener
{
Display dis=Display.getDisplay(this);
17
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
public BarGraphMidlet() {}
public void startApp()
{ dinput.addCommand(exit);
dinput.addCommand(submit);
dinput.append(t1);
dinput.append(t2);
dinput.append(t3);
dinput.append(t4);
dinput.setCommandListener(this);
dis.setCurrent(dinput);
}
public void pauseApp() { }
{
chart.setData("0"+t1.getString(),"0"+t2.getString(),"0"+t3.getString(),"0"+t
4.getStri
ng());
dis.setCurrent(chart);
}
}
}
int
w=getWidth(),h=getHeight()
; String title="Bar Chart";
int hspace=(int)(h-tmar-
bmar)/10; int
vspace=(int)(w-lmar)/5;
18
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
int
bwidth=(int)(vspace/4)
; int v[]={0,0,0,0};
public void setTitle(String t) { title=t; }
v[1]=Integer.parseInt(v2);
v[2]=Integer.parseInt(v3);
v[3]=Integer.parseInt(v4);
repaint();
}
public void
paint(Graphics g) {
g.setColor(0x0000FF);
g.fillRect(0,0,w,15);
g.setColor(0xFFFFFF);
g.drawString(title,0,
0,0);
for(int i=0;i<=10;i++)
{ g.drawLine(lmar-3,tmar+i*hspace,w-
2,tmar+i*hspace); g.setColor(0xFFFFFF);
g.drawString(""+(i*10),0,tmar+i*hspace-7, 0);
g.setColor(0xAAAAAA);
g.setColor(0xFFFFFF);
g.drawLine(lmar,tmar,lmar,h-
bmar); for(int i=1;i<=4;i++)
{ g.setColor(0xFFFF00); g.drawString("V"+i,lmar+i*vspace-
5,tmar-15, 0); g.drawString(""+v[i-1],lmar+i*vspace-
5,tmar+(v[i-1]*hspace/10), 0); g.setColor(0xFFFFFF);
g.fillRect(lmar+i*vspace-10,tmar,20,(v[i-1]*hspace/10));
}
}
}
19
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Output:
20
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 05:
Theory:
TextField Class [javax.microedition.lcdui]:
Source Code:
package week5;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CheckPhoneNoMidlet extends MIDlet implements
CommandListener {
21
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Display
dis=Display.getDisplay(this); Form
form=new Form("Phone No. Input");
Command exit=new Command("Exit App",Command.EXIT,0);
Command submit=new Command("Submit",Command.SCREEN,0);
StringItem res=new StringItem("","");
TextField phno = new TextField("Enter Phone
Number:",null,256,TextField.NUMERIC);
public CheckPhoneNoMidlet() {
}
public void startApp()
{
form.append(phno);
form.append(res);
form.addCommand(exit);
form.addCommand(submit);
form.setCommandListener(this);
dis.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional)
{notifyDestroyed();}
public void commandAction(Command c, Displayable s)
{ if (c==exit)
notifyDestroyed();
else if(c==submit)
{ String areacode=phno.getString().substring(0,3); String
phnum=phno.getString().substring(3);
if(areacode.length()==3)
{
if(areacode.equals("040")||areacode.equals("041")||areacode.equals("0
50")||areacode.equals("044"))
{ if(phnum.length()>=6&&phnum.length()<=8)
res.setText("Phone Number is valid...\n Area Code:
"+areacode+"\n Phone Number: "+phnum);
else
res.setText("There should be 6-8 numbers in telephone
number");
}else
res.setText("Areas Code should be one of the following: \n
040, 041, 050,044");
}else
res.setText("Areas Code should be one of the following: 040\n
040, 041, 050, 044");
}}}
22
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Output:
23
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 06:
Theory:
Connector Class [javax.microedition.io]:
24
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
public DataOutputStream
openDataOutputStream()
throws
IOException
25
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Source Code:
Server
Midlet:
package
week6;
import
java.io.InputStream;
import
java.io.OutputStream;
import javax.microedition.io.Connector;
import
javax.microedition.io.ServerSocketConnection
import
javax.microedition.io.StreamConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ServerMidlet extends MIDlet implements CommandListener
{
Display dis=Display.getDisplay(this);
public ServerMidlet() { }
public void startApp()
{ dis.setCurrent(tb);
try
{ StreamConnection
sc=null; InputStream
in=null; OutputStream
out=null; while(true)
{String pname="",cost="";
tb.setString(tb.getString()+"Waiting for
clients...\n"); ServerSocketConnection
ssc=(ServerSocketConnection)Connector.open("socket://:9002");
sc=ssc.acceptAndOpen();
26
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
in=sc.openDataInputStre
am(); int b;
while((b=in.read())!=-1)
pname=pname+(char)b;
in.close();
tb.setString(tb.getString()+"Client> "+pname+"\n");
if(pname.equals("LAPTOP")) cost="32000";
else if(pname.equals("MOUSE")) cost="300";
else if(pname.equals("MONITOR")) cost="2500";
else cost="Unknown Product";
byte
buf[]=cost.getBytes();
out=sc.openOutputStrea
m(); out.write(buf);
out.close();
sc.close();
ssc.close();
}
}
catch(Exception e)
{ tb.setString(e.toString()); }
}
notifyDestroyed();
}
27
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
ClientMidle:
package
week6;
import
java.io.InputStream;
import
java.io.OutputStream;
import javax.microedition.io.Connector;
import
javax.microedition.io.SocketConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ClientMidlet extends MIDlet implements CommandListener
{
Display
dis=Display.getDisplay(this);
StringItem res=new
StringItem("","");
public ClientMidlet() { }
public void startApp()
{ form.append(pname);
form.append(res);
form.addCommand(send);
form.setCommandListener(this);
dis.setCurrent(form);
}
public void pauseApp() { }
28
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
{ notifyDestroyed(); }
notifyDestroye
d(); else
if(c==send)
{ try
{ SocketConnection
sc=(SocketConnection)Connector.open("socket://localhost:9002");
OutputStream out=sc.openDataOutputStream();
byte[]
buf=pname.getString().toUpperCase().getBytes()
; out.write(buf);
out.close();
int b;
String cost="";
InputStream
in=sc.openInputStream();
while((b=in.read())!=-1)
cost=cost+(char)b;
in.close();
sc.close();
}
catch(Exception e) { res.setText(e.toString()); }
}}
}
29
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Output:
30
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 07:
Theory:
TextField Class [javax.microedition.lcdui]:
31
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Source Code:
Server Servlet:
import
javax.servlet.*;
import java.sql.*;
import java.io.*;
public class Login extends GenericServlet
{ String
uname=req.getParameter("uname");
String
pwd=req.getParameter("pwd");
PrintWriter pw=res.getWriter();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcD
river"); Connection
32
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
con=DriverManager.getConnection("jdbc:odbc:mobiledb
","",""); Statement
smt=con.createStatement();
ResultSet
rs=smt.executeQuery("select * from
users where
uname='"+uname+"' and pwd='"+pwd+"'");
if(rs.next())
pw.println("You are authorized...\nWelcome
to
"+uname);
else
pw.println("Invalid
Username/Password?"); }catch(Exception e) {
pw.println(e.toString()); } pw.close();
}
}
Client
Midlet:
package
week7;
import java.io.InputStream;
import javax.microedition.io.Connector;
import
javax.microedition.io.InputConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
Display
dis=Display.getDisplay(this);
Form form=new Form("Login
Page");
Command submit=new
Command("Submit",Command.SCREEN,0); Command
exit=new Command("Exit App",Command.EXIT,0);
33
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Password:",null,256,TextField.PASSWORD);
StringItem res=new StringItem("","Enter User Name & Password");
public ClientMidlet() {
} public void
startApp()
{
form.append(una
me);
form.append(pwd)
;
form.append(res);
form.addCommand(exit);
form.addCommand(submit);
form.setCommandListener(this);
dis.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional)
{notifyDestroyed(); }
notifyDestroye
d(); else
if(c==submit)
submitForm();
}
public void submitForm()
{ InputConnection
ic=null; InputStream
in=null;
try
{
String
url="http://localhost:4040/dbserver/Login?uname="+uname.getString()
+"&pwd="+ pwd.getString();
ic=(InputConnection)Connector.open(url);
34
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
in=ic.openInputStrea
m(); String msg="";
int b;
while((b=in.read())!=-
1)
msg=msg+(char)b;
uname.setString("");
pwd.setString("");
res.setText(msg);
ic.close();
}
catch(Exception e)
{ res.setText(e.toString()); }
}}
Output:
35
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 08:
Theory:
TextField Class [javax.microedition.lcdui]:
36
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Source Code:
Server Servlet:
import
javax.servlet.*;
import java.sql.*;
import java.io.*;
{ String etype=req.getParameter("etype");
String
equery=req.getParameter("equery");
String msg="",cmd="";
PrintWriter
pw=res.getWriter(); try
37
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:mobiledb
","","");
Statement
smt=con.createStatement();
ResultSet rs=null;
if(etype.equals("STUDENT"))
{ rs=smt.executeQuery("select * from student_db
where
htno='"+equery+"'");
if(rs.next())
{int
s1=rs.getInt("sub1"),s2=rs.getInt("sub2"),s3=rs.getInt("
sub3"); int
s4=rs.getInt("sub4"),s5=rs.getInt("sub5"),s6=rs.getInt("sub6");
msg="Student Marks\nH.T.No.:
"+equery; msg=msg+"\nSubject1: "+s1;
msg=msg+"\nSubject2: "+s2;
msg=msg+"\nSubject3: "+s3;
msg=msg+"\nSubject4: "+s4;
msg=msg+"\nSubject5: "+s5;
msg=msg+"\nSubject6: "+s6;
msg=msg+"\nTotal:"+(s1+s2+s3+s4+s5
+s6);
}
}
else if(etype.equals("MOVIES"))
{rs=smt.executeQuery("select * from movie_db
where
mname='"+equery+"'");
if(rs.next())
{ msg="Movie Information\nMovie
Name:
"+equery;
msg=msg+"\nTheatreName:"+rs.getString("tname");
msg=msg+"\nAddress:"+rs.getString("taddress");
msg=msg+"\nTicketsAvailable:"+rs.getString("tickets");
}
}
else if(etype.equals("PNR"))
{rs=smt.executeQuery("select * from train_db where
pnrno='"+equery+"'");
if(rs.next())
{msg="Train Information\nPNR No.: "+equery;
msg=msg+"\nTrainName:
"+rs.getString("tname");
38
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
msg=msg+"\nFrom: "+rs.getString("from");
msg=msg+"\nTo: "+rs.getString("to");
msg=msg+"\nPassingerName:"+rs.getString("pname");
msg=msg+"\nSeatAlloted:
"+rs.getString("seat");
}
}
else if(etype.equals("CRICKET"))
{rs=smt.executeQuery("select * from cricket_db where
mcode='"+equery+"'");
if(rs.next())
{msg="SportsInformation\nSportsCode:"+equery;
msg=msg+"\n"+rs.getString("team1")+"Vs."+rs.getString("team2");
msg=msg+"\nBatingTeam:"+rs.getString("bteam");
msg=msg+"\nScore: "+rs.getString("score");
}
}
else if(etype.equals("WEATHER"))
{rs=smt.executeQuery("select * from weather_db where
city='"+equery+"'");
if(rs.next())
{msg="Weather Information\nCity: "+equery;
msg=msg+"\nHighTemparature:
"+rs.getString("high_temp");
msg=msg+"\nLowTemparature:
"+rs.getString("low_temp");
}
}
else if(etype.equals("EAMCET"))
39
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
}catch(Exception e) { pw.println(e.toString()); }
pw.close();
}
}
Client
Midlet:
package
week8;
import java.io.InputStream;
import javax.microedition.io.Connector;
import
javax.microedition.io.InputConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
Command submit=new
Command("Submit",Command.SCREEN,0); Command
home=new Command("Home",Command.SCREEN,0);
Command exit=new Command("Exit
App",Command.EXIT,0); TextField equery=new
TextField("",null,256,TextField.ANY);
public ClientMidlet() {
} public void
startApp()
{ form.append(equery);
form.append(res);
form.addCommand(home);
form.addCommand(submit);
form.setCommandListener(th
is);
menu.append("Student Marks
Enquiry",null); menu.append("Movies
40
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Enquiry",null); menu.append("PNR
Enquiry",null); menu.append("Cricket
Enquiry",null); menu.append("Weather
Enquiry",null); menu.append("EAMCET
Results Enquiry",null);
menu.setCommandListener(this);
dis.setCurrent(menu);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional)
{notifyDestroyed(); }
notifyDestroyed()
; else if(c==submit)
submitForm();
else if(c==home)
dis.setCurrent(men);
else
{ switch(menu.getSelectedIndex())
{ case 0:
equery.setLabel("EnterStudentH.T.N");
etype="STUDENT";
break;
case 1:
equery.setLabel("EnterMovieName")
; etype="MOVIES";
break;
case 2:
equery.setLabel("EnterPNRNo");
etype="PNR";
break;
case 3:
equery.setLabel("EnterCricketMatchCode"
); etype="CRICKET";
break;
case 4:
equery.setLabel("Enter City
Name:"); etype="WEATHER";
41
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
break;
case 5:
break;
}
res.setText("");
dis.setCurrent(form);
}
}
public void submitForm()
{ InputConnection ic=null;
InputStream in=null;
res.setText("Getting
information..."); try
{String
url="http://localhost:4040/dbserver/Enquiry?etype="+etype+"&equery
="+equery.g etString();
res.setText(url);
ic=(InputConnection)Connector.open(url);
in=ic.openInputStrea
m(); String msg="";
int b;
while((b=in.read())!=-
1)
msg=msg+(char)b;
res.setText(msg);
ic.close();
equery.setString("");
}
catch(Exception e)
{ res.setText(e.toString()); }
}
}
42
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Output:
43
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
44
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Android Introduction
Android Architecture:
onCreate
onStart
Process-specific events (Ex: launching activities/ accessing a
database)
onStop
45
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
onDestroy
com.Hello
o HellowWorldActivity.java
gen/
com.Hello
o R.java
Android 3.0/
android.jar
Assets/
res/
drawable-hdpi
drawable-ldpi
drawable-mdpi
layout
o main.xml
46
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
values
AndroidManifest.out.xml
AndroidManifest.xml
default.properties
proguard.cfg
8. In this directory structure two files are very importent for the
development of application, they are:
9. After editing the code, Right click on the desired application from
Package Explorer window , click on Run As > Android
Application option to execute the application
47
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 09:
Source Code:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d" android:orientation="vertical"
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>
HelloWorldActivity.j
ava: package
com.Hello;
import
android.app.Activity;
import android.os.Bundle;
import
android.widget.TextView;
}}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" package="com.Hello"
android:versionCode="1
"
48
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
android:versionName="
1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".HelloWorldActivity"
android:label="@string/app_n
ame"> <intent-filter>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
49
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 10:
Source Code:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d" android:orientation="vertical"
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>
HelloWorldActivity.j
ava: package
com.Hello;
import
android.app.Activity;
import android.os.Bundle;
import
android.widget.TextView;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
50
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
<manifest
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" package="com.Hello"
android:versionCode="1
"
android:versionName="
1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".HelloWorldActivity"
android:label="@string/app_n
ame"> <intent-filter>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
51
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 11:
Source Code:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText android:id="@+id/editText1"
android:hint="Enter_User_Name"
android:layout_height="wrap_content"
android:layout_width="match_parent"> </EditText>
<Button android:text="Accept"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button
>
HelloUserActivity.jav
a: package
com.HelloUser; import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
52
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
{ EditText ed;
Button
Accept;
TextView
show;
show=(TextView)findViewById(R.id.textVi
ew1); Accept.setOnClickListener(new
OnClickListener()
{public void onClick(View v)
{ String
Value=ed.getText().toString();
show.setText("Hello "+Value);
}
});
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" package="com.HelloUser"
android:versionCode="1
"
android:versionName="
1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".HelloUserActivity"
android:label="@string/app_n
ame"> <intent-filter>
53
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
54
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 12:
12. a) Linear
Layout: Source
Code:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d" android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:text="Button"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="Button"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="Button"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
LinearLayoutActivity.j
ava: package
com.LinearLayout;
import
android.app.Activity;
import
android.os.Bundle;
55
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
setContentView(R.layout.main);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" package="com.LinearLayout"
android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".LinearLayoutActivity"
android:label="@string/app_n
ame"> <intent-filter>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
56
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
</application>
</manifest>
Output:
57
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
12. b) Relative
Layout: Source Code:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/relativeLayout1">
RelativeLayoutActivity
.java: package
com.Relative;
import
android.app.Activity;
import
android.os.Bundle;
{
super.onCreate(savedInstanc
eState);
setContentView(R.layout.mai
n);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
58
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
<manifest
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" package="com.Relative" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".RelativeLayoutActivity"
android:label="@string/app_n
ame"> <intent-filter>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
59
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
12. c) Table
Layout: Source
Code:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout android:layout_width="match_parent"
android:id="@+id/tableLayout1"
android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="1"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="2"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="3"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="4"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TableRow android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TableRow>
<TableRow android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TableRow>
</TableRow>
<TableRow android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
60
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
<Button android:text="5"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="6"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Butto
n>
<Button android:text="7"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Butto
n>
<Button android:text="8"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Butto
n>
</TableRow>
</TableLayout>
</LinearLayout>
TableLayoutActivity.j
ava: package
com.Table;
import
android.app.Activity;
import
android.os.Bundle;
public class TableLayoutActivity extends Activity
setContentView(R.layout.main);
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" package="com.Table"
61
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
android:versionCode="1
"
android:versionName="
1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".TableLayoutActivity"
android:label="@string/app_n
ame"> <intent-filter>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
62
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android
" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView android:numColumns="auto_fit"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:id="@+id/grid"></GridV
iew> </LinearLayout>
GridActivityDemo.j
ava: package
com.list;
import
android.app.Activity;
import
android.os.Bundle;
import
android.widget.ArrayAdapter;
import android.widget.Gallery;
import
android.widget.GridView;
import
android.widget.ListView;
public class GridActivityDemo extends Activity
{ArrayAdapter<String> aa;
String[]
names={"abc","xyz","abc","xyz","abc","xyz","abc","xyz"};
int[] images={R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon, R.drawable.icon,
R.drawable.icon};
GridView g;
63
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
aa=new
ArrayAdapter(this,android.R.layout.simple_list_item
_1,names); g.setAdapter(aa);
}}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.list" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".GridActivityDemo"
android:label="@string/app_n
ame"> <intent-filter>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>Output:
64
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 13:
Source Code:
Main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<RadioGroup android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" android:text="In Celcious" />
<RadioButton android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In Farhenheat" />
</RadioGroup>
<Button
android:id="@+id/butt
on1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Caluculate"
/>
65
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
<TextView
android:id="@+id/textVie
w1"
android:layout_marginTop="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textAppearance="?android:attr/textAppearanc
eLarge" /> </LinearLayout>
MainActivity.java:
package
com.convert.convertion;
import
android.app.Activity;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import
android.widget.EditText;
import
android.widget.RadioButton;
import
android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity
float
result=0.0f;
String dg="";
66
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText1);
rg=(RadioGroup)findViewById(R.id.radioGroup1);
rb1=(RadioButton)findViewById(R.id.radio0);
rb2=(RadioButton)findViewById(R.id.radio1);
b1=(Button)findViewById(R.id.button1);
tv1=(TextView)findViewById(R.id.textView1);
rg.setOnCheckedChangeListener(new
OnCheckedChangeListener()
{ dg=ed1.getText().toString();
if (dg==null)
else
{
switch (checkedId)
{ case R.id.radio0:
float
f1=Float.parseFloat(dg);
result=((f1 - 32) * 5 / 9);
break;
case R.id.radio1:
float
f2=Float.parseFloat(dg);
result=((f2 * 9) / 5) +
32;
break;
}}}});
b1.setOnClickListener(new OnClickListener()
{public void onClick(View v)
{
tv1.setText(""+result);
}
});}}
67
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT
LAB
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/an
droid" package="com.convert.convertion"
android:versionCode="1" android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.convert.convertion.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
68
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
Experiment 14:
Source Code:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffcc00" > <TextView
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceL
arge" android:id="@+id/textView1"
android:layout_height="wrap_content"
android:text="FirstClass"></TextView>
<Button android:text="move"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button
> </LinearLayout>
Second.xml;
<?xml version="1.0"
encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk
/res/android" android:orientation="vertical"
android:layout_width="match_parent"
android:background="#ff0000"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView1" android:layout_height="wrap_content"
android:text="second class"></TextView>
</LinearLayout>
69
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
IntentDemoActivity.
java: package
com.Demo;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
public class IntentDemoActivity extends Activity
{Button move;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
move=(Button)findViewById(R.id.button
1); move.setOnClickListener(new
OnClickListener()
{
public void onClick(View v)
{
Intent i=new
Intent(getApplicationContext(),second.class);
startActivity(i);
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" package="com.Demo"
70
IV BTECH I SEMISTER MOBILE APPLICATION DEVELOPMENT LAB
android:versionCode="1
"
android:versionName="
1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity
android:name=".IntentDemoActivity"
android:label="@string/app_n
ame"> <intent-filter>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".second"></activity>
</application>
</manifest>
Output:
71