MC Final Lab Manual
MC Final Lab Manual
Introduction to J2ME
What is J2ME
J2me is a software or specification for developing small computing devices
Or
J2me is a Java platform designed for Small Computing Devices ranging from pagers,
mobile phones, Personal Digital Assistance (PDA) to the set up boxes.
J2ME provides a robust, flexible environment for applications running on mobile and other
embedded devices such as mobile phones, personal digital assistants (PDAs), TV set-top
boxes, and printers.
J2ME includes flexible user interfaces, robust, security, built-in network protocols, and
support for networked and offline applications that can be downloaded dynamically.
Applications based on J2ME are portable across many devices, yet leverage each device's
native capabilities.
J2ME is divided into
1. Configuration
2. Profile
3. Optional API’S
J2ME Configuration :
Persistence
Networking
Event handling
Hardware characteristics
Screen size 96 x 54
The PDA Profile is similar to MIDP, but it is aimed at PDAs that have better screens and
more memory than cell phones.
Foundation Profile
The Foundation Profile extends the CDC to include almost all of the core Java 2 Version 1.3
core libraries. As its name suggests, it is intended to be used as the basis for most of the
other CDC profiles.
The Personal Basis Profile adds basic user interface functionality to the Foundation Profile. It
is intended to be used on devices that have an unsophisticated user interface capability, and
it therefore does not allow more than one window to be active at any time. Platforms that can
support a more complex user interface will use the Personal Profile instead.
RMI Profile
The RMI Profile adds the J2SE Remote Method Invocation libraries to the Foundation
Profile. Only the client side of this API is supported.
Game ProfileThis will provide a platform for writing games software on CDC devices.
Step-3:-A setting window will open up. Accept the defaults by clicking ok in that
window.
Aim:-
Description:-
Program:-
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
import java.lang.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
public class changFont extends MIDlet
{
public static final boolean COLOR=false;
public static final boolean DEBUG=false;
public static final int DARKGOLDENROD=0xB8860B;
public static final int DARKBLUE=0x00008B;
public static final int DEEPPINK=0xFF1493;
public static final int LIGHT_GRAY=0xAAAAAA;
public static final int DARK_GRAY=0x555555;
private Display mydisp=null;
private DecodeCanvasdecodcanvas=null;
private boolean painting=false;
public changFont()
{
mydisp=Display.getDisplay(this);
decodcanvas=new DecodeCanvas(this);
}
public void startApp() throws MIDletStateChangeException
{
mydisp.setCurrent(decodcanvas);
}
public void pauseApp()
{
}
Font f1=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,
Font.SIZE_LARGE);
Fontf2=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,
Font.SIZE_MEDIUM);
Fontf3=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,
Font.SIZE_SMALL);
intyPos=0;
if(COLOR)
g.setColor(DEEPPINK);
else
g.setColor(LIGHT_GRAY);
g.fillRect(0,yPos,width,f1.getHeight());
if(COLOR)
g.setColor(DARKGOLDENROD);
else
g.setColor(DEEPPINK);
g.setFont(f1);
g.drawString("BIG FONT",0,yPos, Graphics.LEFT |
Graphics.TOP);
yPos=yPos+f1.getHeight()+10;
g.setFont(f2);
g.drawString("MEDIUM FONT",0,yPos, Graphics.LEFT |
Graphics.TOP);
g.setColor(DARKBLUE);
yPos=yPos+f1.getHeight()+10;
g.setFont(f3);
g.drawString("SMALL FONT",0,yPos, Graphics.LEFT |
Graphics.TOP);
yPos=yPos+f1.getHeight()+10;
g.drawLine(0, f3.getHeight() + yPos -1, width,f3.getHeight() +
yPos - 1 );
painting=false;
}
}
}
Output:-
Result:
EXPERIMENT - 2
Aim:-
Description:-
Program:-
importjavax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MenuCreation extends MIDlet implements CommandListener {
public ChoiceGroupch;
public Form form;
public Display display;
public Command command;
public StringItemst;
public MenuCreation()
{
display=Display.getDisplay(this);
ch=new ChoiceGroup("Edit",Choice.EXCLUSIVE);
ch.append("cut",null);
ch.append("copy",null);
ch.append("paste",null);
ch.append("delete",null);
ch.append("select all",null);
ch.append("unselect all",null);
ch.setSelectedIndex(1, true);
command=new Command("Select list item",Command.OK,1);
form=new Form("");
form.append(ch);
form.addCommand(command);
form.setCommandListener(this);
st=new StringItem("","");
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command,Displayable displayable)
{
if(command==command)
{
st.setText("");
st.setText("your selected option is "+ch.getString(ch.getSelectedIndex()));
form.append(st);
}
}
}
Output:-
Result:-
EXPERIMENT - 3
Aim:-
Description:-
Program:-
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MenuEvents extends MIDlet implements CommandListener,ItemStateListener
{
public ChoiceGroupch;
public ChoiceGroup ch1;
public Form form;
public Form form1;
public Display display;
public Command View;
public Command Exit;
public Command Back;
public StringItem options;
public Item item;
public MenuEvents()
{
display=Display.getDisplay(this);
form=new Form("");
form1=new Form("selected options are");
ch=new ChoiceGroup("Preferences",Choice.MULTIPLE);
ch.append("cut",null);
ch.append("copy",null);
ch.append("paste",null);
ch.append("delete",null);
ch.setSelectedIndex(1,true);
form.append(ch);
ch1=new ChoiceGroup("",Choice.EXCLUSIVE);
ch1.append("select all",null);
ch1.append("unselect all",null);
ch1.setSelectedIndex(1,true);
form.append(ch1);
View=new Command("View",Command.OK,1);
Exit=new Command("Exit",Command.EXIT,1);
Back=new Command("Back",Command.BACK,1);
form.addCommand(View);
form.addCommand(Exit);
form.addCommand(Back);
form.setCommandListener(this);
form1.setCommandListener(this);
form.setItemStateListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command,Displayable displayable)
{
if(displayable==form)
{
if(command==View)
{
boolean opt[]=new boolean[ch.size()];
options=new StringItem("","");
String values="";
ch.getSelectedFlags(opt);
options.setText("");
for(int i=0;i<opt.length;i++)
{
if(opt[i])
{
values+=ch.getString(i)+"\n";
}
}
options.setText(values);
form1.append(options);
display.setCurrent(form1);
}
else if(command==Exit)
{
destroyApp(true);
notifyDestroyed();
}
}
else if(displayable==form1)
{
if(command==Back)
{
display.setCurrent(form);
options.setText("");
}
}
}
public void itemStateChanged(Item item)
{
if(item==ch1)
{
int i=0;
int size=ch.size();
while(i<size)
{
if(ch1.getSelectedIndex()==0)
ch.setSelectedIndex(i,true);
else
ch.setSelectedIndex(i,false);
i++;
}
}
}
}
Output:-
Result:-
EXPERIMENT - 4
Aim:-
Description:-
Program:-
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class BarGraphMIDlet extends MIDlet implements CommandListener
{
public Form form;
public Command exitCommand;
public Command okCommand;
public Command backCommand;
public Displayable d;
public Display display;
public TextField textfield1;
public TextField textfield2;
public TextField textfield3;
public TextField textfield4;
public TextField textfield5;
public BarGraphMIDlet()
{
display=Display.getDisplay(this);
form=new Form("BarGraph");
textfield1=new TextField("Value1:-","",30,TextField.ANY);
textfield2=new TextField("Value2:-","",30,TextField.ANY);
textfield3=new TextField("Value3:-","",30,TextField.ANY);
textfield4=new TextField("Value4:-","",30,TextField.ANY);
textfield5=new TextField("Value5:-","",30,TextField.ANY);
form.append(textfield1);
form.append(textfield2);
form.append(textfield3);
form.append(textfield4);
form.append(textfield5);
okCommand=new Command("Ok",Command.OK,1);
exitCommand=new Command("Exit",Command.EXIT,1);
backCommand=new Command("Back",Command.BACK,1);
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command,Displayable displayable)
{
if(displayable==form)
{
if(command==okCommand)
{
int[] data=new int[5];
data[0]=Integer.parseInt(textfield1.getString());
data[1]=Integer.parseInt(textfield2.getString());
data[2]=Integer.parseInt(textfield3.getString());
data[3]=Integer.parseInt(textfield4.getString());
data[4]=Integer.parseInt(textfield5.getString());
d=new BarCanvas(data);
d.addCommand(backCommand);
d.setCommandListener(this);
display.setCurrent(d);
}
else if(command==exitCommand)
notifyDestroyed();
}
else if(displayable==d)
{
if(command==backCommand)
display.setCurrent(form);
}
}
}
class BarCanvas extends Canvas
{
int[] data;
public int x;
public int y;
public int y1;
public int h;
public BarCanvas(int[] data)
{
this.data=data;
x=10;
}
public void paint(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(255,125,100);
int i=0;
y1=data[0];
h=200;
while(i<data.length)
{
y=data[i];
h=200+y1-y;
g.fillRect(x,y,25,h);
x+=30;
i++;
}
}
}
Output:-
Result:-
EXPERIMENT - 5
Aim:-
Description:-
Program:-
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class InputChecking extends MIDlet implements CommandListener
{
public Form form1;
public TextField textfield1;
public Command exitCommand;
public Command okCommand;
public StringItemst;
public Display display;
public InputChecking()
{
display=Display.getDisplay(this);
form1=new Form("Insert the phone number");
exitCommand=new Command("Exit",Command.EXIT,1);
okCommand=new Command("Ok",Command.OK,1);
st=new StringItem("phone number is","");
textfield1=new TextField("phone;","",30,TextField.ANY);
form1.append(textfield1);
form1.addCommand(okCommand);
form1.addCommand(exitCommand);
form1.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form1);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command cmd,Displayable displayable)
{
if(cmd==exitCommand)
notifyDestroyed();
else if(cmd==okCommand)
{
String s=textfield1.getString();
s=s.replace(' ','.');
intlen=s.length();
int i=0;
int c=0;
String s1="";
while(i<len)
{
if(s.charAt(i)=='.')
{
if(c==0)
{
if(s1.equals("040")||s1.equals("041")||s1.equals("050")||s1.equals("0400")||s1.equals("044"))
{
c++;
s1="";
}
}
if(c==1)
{
if(s1.length()-1==3)
{
c++;
s1="";
}
}
}
s1=s1+s.charAt(i);
i++;
}
if(s1.length()-1==3||s1.length()-1==4||s1.length()-1==5)
{
c++;
}
if(c==3)
st.setText("ok");
else
{
st.setText("wrong\n phone number Format is XXX XXX XXXX\n Area code must be
040|041|050|0400|044");
}
form1.append(st);
}
}
}
Output:-
Result:-
EXPERIMENT - 6
Aim:-
Description:-
Program:-
SERVER SIDE:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
/**
* @author ADMIN
*/
public class DatagramServer extends MIDlet implements CommandListener{
public Form form1;
public Form form2;
public Command startCommand;
public Command refreshCommand;
public Command exitCommand;
public Display display;
public StringItemst;
public DatagramServer()
{
display=Display.getDisplay(this);
startCommand=new Command("Start",Command.OK,1);
refreshCommand=new Command("Refresh",Command.OK,1);
exitCommand=new Command("Exit",Command.EXIT,1);
st=new StringItem(" "," ");
form1 =new Form("DataGramserver");
form2=new Form("Ready to receive Messages");
form1.addCommand(startCommand);
form1.setCommandListener(this);
form2.addCommand(refreshCommand);
form2.addCommand(exitCommand);
form2.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form1);
}
form2.append(datagram.getData().toString());
} finally {
dgc.close();
}
} catch (Exception x){
x.printStackTrace();
}
display.setCurrent(form2);
}
}
else if(displayable==form2)
{
if(cmd==exitCommand)
{
notifyDestroyed();
}
else if(cmd==refreshCommand)
{
st.setText(" ");
}
}
}
}
CLIENT PROGRAM:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class DatagramClient extends MIDlet implements CommandListener
{
public Form form1;
public Display display;
public TextFieldtextfield;
public Command sendCommand;
public DatagramClient()
{
display=Display.getDisplay(this);
form1=new Form("Datagram Client");
sendCommand=new Command("send",Command.OK,1);
textfield=new TextField("Enter Text",null,30,TextField.ANY);
form1.append(textfield);
form1.addCommand(sendCommand);
form1.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form1);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command cmd,Displayable d)
{
if(cmd==sendCommand)
{
try {
DatagramConnectiondgc = (DatagramConnection)
Connector.open("datagram://localhost:9001");
try {
while(true)
{
byte[] payload = textfield.getString().getBytes();
Datagram datagram = dgc.newDatagram(payload, payload.length);
dgc.send(datagram);
}
} finally
{
dgc.close();
}
} catch (Exception x)
{
x.printStackTrace();
}
}
}
}
OUTPUT:
Result:
Android Application
Android applications are written in the Java programming language. The Android SDK tools
compile the code—along with any data and resource files—into an Android package, an archive
file with an .apk suffix. All the code in a single .apk file is considered to be one application and
is the file that Android-powered devices use to install the application.
Once installed on a device, each Android application lives in its own security sandbox:
The Android operating system is a multi-user Linux system in which each application is a
different user.
By default, the system assigns each application a unique Linux user ID (the ID is used
only by the system and is unknown to the application). The system sets permissions for
all the files in an application so that only the user ID assigned to that application can
access them.
Each process has its own virtual machine (VM), so an application's code runs in isolation
from other applications.
By default, every application runs in its own Linux process. Android starts the process
when any of the application's components need to be executed, then shuts down the
process when it's no longer needed or when the system must recover memory for other
applications.
In this way, the Android system implements the principle of least privilege. That is, each
application, by default, has access only to the components that it requires to do its work and no
more. This creates a very secure environment in which an application cannot access parts of the
system for which it is not given permission.
However, there are ways for an application to share data with other applications and for an
application to access system services:
It's possible to arrange for two applications to share the same Linux user ID, in which
case they are able to access each other's files. To conserve system resources, applications
with the same user ID can also arrange to run in the same Linux process and share the
same VM (the applications must also be signed with the same certificate).
An application can request permission to access device data such as the user's contacts,
SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. All
application permissions must be granted by the user at install time.
That covers the basics regarding how an Android application exists within the system. The rest
of this document introduces you to:
The user interface for an activity is provided by a hierarchy of views—objects derived from the
View class. Each view controls a particular rectangular space within the activity's window and
can respond to user interaction. For example, a view might be a button that initiates an action
when the user touches it.
Android provides a number of ready-made views that you can use to design and organize your
layout. "Widgets" are views that provide a visual (and interactive) elements for the screen, such
as a button, text field, checkbox, or just an image. "Layouts" are views derived from ViewGroup
that provide a unique layout model for its child views, such as a linear layout, a grid layout, or
relative layout. You can also subclass the View and ViewGroup classes (or existing subclasses)
to create your own widgets and layouts and apply them to your activity layout.
The most common way to define a layout using views is with an XML layout file saved in your
application resources. This way, you can maintain the design of your user interface separately
from the source code that defines the activity's behavior. You can set the layout as the UI for
your activity with setContentView(), passing the resource ID for the layout
You must declare your activity in the manifest file in order for it to be accessible to the system.
To declare your activity, open your manifest file and add an <activity> element as a child of the
<application> element. For example:
The same lifecycle callback methods are listed in table 1, which describes each of the callback
methods in more detail and locates each one within the activity's overall lifecycle, including
whether the system can kill the activity after the callback method completes.
Killable
Method Description Next
after?
Called when the activity is first created.
This is where you should do all of your
normal static set up — create views,
bind data to lists, and so on. This
onCreate() No onStart()
method is passed a Bundle object
containing the activity's previous state,
if that state was captured (see Saving
Activity State, later).
Killable
Method Description Next
after?
Aim:-
Description:-
Program:-
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloActivity extends Activity
{
/**Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
// Construct a TextView UI component
textView.setText("Hello,world");
// Set the text message for TextView
setContentView(textView);
// this Activity sets its content to the TextView
}
}
OUTPUT:
Result:
EXPERIMENT-8
Aim:-
Description:-
Program:-
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloActivity extends Activity
{
/**Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
// Construct a TextView UI component
textView.setText("Hello,world");
// Set the text message for TextView
setContentView(textView);
// this Activity sets its content to the TextView
}
}
OUTPUT:
Result:
EXPERIMENT-9
Aim:-
Description:-
Program:-
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/empty"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#A4C639"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="23dp"
android:text="@string/input_lable"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0099FF"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:background="#CCCCCC"
android:ems="10"
android:inputType="textPersonName">
<requestFocus/>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="20dp"
android:background="#0099FF"
android:text="@string/sub"
android:textColor="#FFFFFF"/>
</RelativeLayout>
PROGRAM:
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.TextView;
publicclassFrmActivity extendsActivity
{
Button mButton;
EditText mEdit;
TextView mText;
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frm);
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(newView.OnClickListener()
{
publicvoidonClick(View view) {
mEdit = (EditText)findViewById(R.id.editText1);
mText = (TextView)findViewById(R.id.textView1);
mText.setText("Welcome "+mEdit.getText().toString()+"!");
}
}
}
}
Output:-
Result:-
EXPERIMENT-10
Aim:-
Description:-
Program:-
Android supports the following ViewGroups:
Linear Layout
TableLayout
RelativeLayout
The LinearLayout arranges views in a single column or single row. Child views can either be arranged vertically
or horizontally.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
In the main.xml file, observe that the root element is <LinearLayout> and it has a <TextView> element
Contained within it. The <LinearLayout> element controls the order in which the views contained within it
appear.
Each View and ViewGroup has a set of common attributes, some of which are shown in Table 1.
Attribute Description
layout_width Specifies the width of the View or ViewGroup
layout_height Specifies the height of the View or ViewGroup
layout_marginTop Specifies extra space on the top side of the View or ViewGroup
layout_marginBottom Specifies extra space on the bottom side of the View or ViewGroup
layout_marginLeft Specifies extra space on the left side of the View or ViewGroup
layout_marginRight Specifies extra space on the right side of the View or ViewGroup
layout_gravity Specifies how child Views are positioned
Specifies how much of the extra space in the layout to be allocated to the
layout_weight
View
layout_x Specifies the x-coordinate of the View or ViewGroup
layout_y Specifies the y-coordinate of the View or ViewGroup
The default orientation of LinearLayout is set to horizontal. If you want to change its orientation to vertical,
set the orientation attribute to vertical, like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
Figure shows the effect of changing the orientation to vertical.
InLinearLayout, you can apply the layout_weight and layout_gravity attributes to views contained within it, as
the following modifications to main.xml shows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:layout_width="105px"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"
android:layout_weight="0.2"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_weight="0.8" />
</LinearLayout>
Figure 5 shows that the button is aligned to the right of its parent (which is the LinearLayout) using the layout_
attribute. At the same time, you use the layout_weight attribute to specify the ratio in which the Button
andEditText views occupy the remaining space on the screen. The total value for the layout_weight attribute
must be equal to 1.
TableLayout
The TableLayout groups views into rows and columns. You use the <TableRow> element to designate a row in
the table. Each row can contain one or more views. Each view you place within a row forms a cell. The width for
each column is determined by the largest width of each cell in that column.
Populate main.xml with the following elements and observe the UI as shown in Figure 7.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow>
<TextView
android:text="User Name:"
android:width ="120px"
/>
<EditText
android:id="@+id/txtUserName"
android:width="200px" />
</TableRow>
<TableRow>
<TextView
android:text="Password:" />
<EditText
android:id="@+id/txtPassword"
android:password="true"
/>
</TableRow>
<TableRow>
<TextView/>
<CheckBox android:id="@+id/chkRememberPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Remember Password"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonSignIn"
android:text="Log In" />
</TableRow>
</TableLayout>
RelativeLayout
The RelativeLayout lets you specify how child views are positioned relative to each other.
Consider the following main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/lblComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<EditText
android:id="@+id/txtComments"
android:layout_width="fill_parent"
android:layout_height="170px"
android:textSize="18sp"
android:layout_alignLeft="@+id/lblComments"
android:layout_below="@+id/lblComments"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/btnSave"
android:layout_width="125px"
android:layout_height="wrap_content"
android:text="Save"
android:layout_below="@+id/txtComments"
android:layout_alignRight="@+id/txtComments"
/>
<Button
android:id="@+id/btnCancel"
android:layout_width="124px"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_below="@+id/txtComments"
android:layout_alignLeft="@+id/txtComments"
/>
</RelativeLayout>
Notice that each view embedded within the RelativeLayout have attributes that allow them to align
with another view. These attributes are:
layout_alignParentTop
layout_alignParentLeft
layout_alignLeft
layout_alignRight
layout_below
layout_centerHorizontal
The value for each of these attributes is the ID for the view that you are referencing.
The above XML UI creates the screen shown in Figure 9.
Description:-
Program:-
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Week13" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius to Fahrenheit"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the temperature"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus />
</EditText>
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONVERT" />
packagecom.example.converttemperatureexample;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.EditText;
importandroid.widget.RadioButton;
importandroid.widget.Toast;
publicclassConvertTempertureExample extendsActivity
{
privateEditText text;
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
}
// This method is called at button click because we assigned the name to the
// "On Click property" of the button
publicvoidmyClickHandler(View view)
{
switch(view.getId())
{
caseR.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if(text.getText().length() == 0)
{
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
floatinputValue = Float.parseFloat(text.getText().toString());
if(celsiusButton.isChecked())
{
text.setText(String
.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else
{
text.setText(String
.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
// Converts to celsius
privatefloatconvertFahrenheitToCelsius(floatfahrenheit)
{
return((fahrenheit - 32) * 5/ 9);
}
// Converts to fahrenheit
privatefloatconvertCelsiusToFahrenheit(floatcelsius)
{
return((celsius * 9) / 5) + 32;
}
}
Output:
Result:
EXPERIMENT-12
Aim:-
Description:-
Program:-
Intents are a powerful concept as they allow the creation of loosely coupled applications. Intents can be
used to communicate between any installed application components on the device.
Put the following code in layout/main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
>
<Buttonandroid:id="@+id/Button01"android:layout_width="wrap_content"android:layout_height="wrap_co
</LinearLayout>
We will start a new Intent with the method :
Intent i = newIntent("android.intent.action.VIEW",
Uri.parse("http://www.google.com"));
As your activity gets called with intent you can get the data from the intent and display it in your
application.
On Clicking Finish IntentBrowser code structure is generated with the necessary Android Packages being
imported along with IntentBrowser.java. IntentBrowser class will look like following:
Source Code:
packagecom.sample.IntentBrowser;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.view.View;
publicclassIntentBrowser extendsActivity
{
/** Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
publicvoidopenBrowser(View view)
{
Intent i = newIntent("android.intent.action.VIEW",
Uri.parse("http://www.google.com"));
startActivity(i);
}
}
Output:
Result:-