You are on page 1of 62

EX.

NO: 1 Develop an application that uses GUI components, Font and Colors
DATE:

AIM:
To develop an application that uses GUI components, Fonts and colors using Eclipse.

PROCEDURE:

1. Start the application by


Click on File -> New and choose Android Application Project and type the application name for
the project and click on the next button.
2. Configure the project by choosing the workspace location and then click the next button.
3. Configure the Launcher Icon by choosing the attributes of icon sets like foreground image,
scaling, shape and its background color and then click on to next button.
4. Create an activity by selecting blank activity and then click next.
5. A blank activity will be created and finally click finish button.
6. On clicking the finish button a new project will be created from which the java code and layout
code can be opened.
7. Type the following XML program in layout xml file.
8. Run the project by right clicking on your project in Package Explorer window on the left and
choose Run As => Android Application.
9. Watch the deployment preparation for the app by checking the bottom-right of Eclipse for
deployment progress.
10. Once Eclipse has finished preparing for the deployment, you will see a popup which ask for
adding a new android virtual device by clicking the yes button.
11. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

1
CODING:

activity_main.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="This is a 'sans' demo!"
android:typeface="sans"
android:textStyle="bold"
android:textSize="30sp"
android:shadowColor="#00cc00"
android:shadowRadius="3"
android:shadowDx="3"
android:shadowDy="3"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is a 'serif' demo!"
android:typeface="serif"
android:textStyle="italic"/>

2
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="54dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="This is a 'monospace' demo!"
android:typeface="monospace"
android:textStyle="bold"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginRight="36dp"
android:layout_marginTop="20dp"
android:background="#00ccff"
android:text="OK"/>

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginRight="14dp"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="Enter your name">
<requestFocus/>
</EditText>

</RelativeLayout>

3
OUTPUT:

RESULT:
Thus the application using GUI components, fonts and colors was successfully created using
eclipse.

4
EX.NO: 2 Develop an application that uses Layout Managers and event listeners
DATE:

AIM:
To develop an application that uses Layout Managers and event listeners using Eclipse.

PROCEDURE:

1. Start the application by


Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
2. Configure the project by choosing the workspace location and then click the next button.
3. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
4. Create an activity by selecting blank activity and then click next.
5. A blank activity will be created and finally click finish button.
6. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
7. Type the following XML program in layout code and type the Java program in java file.
8. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
9. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
10. Once Eclipse has done preparing for the deployment, you will see a popup which ask for
adding a new android virtual device by clicking the yes button.
11. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

5
CODING:

activity_main.xml:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ccccff"

android:gravity="center_horizontal"
android:orientation="vertical">

<EditText
android:id="@+id/ed1"
android:layout_width="fill_parent"
android:layout_height="wrap_content
" android:textColor="#800000"
android:textSize="20dp"
android:textStyle="italic">
</EditText>

<EditText
android:id="@+id/ed2"
android:layout_width="fill_parent"
android:layout_height="wrap_content
" android:inputType="textPassword">
</EditText>

<Button
android:id="@+id/bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"/>

</LinearLayout>

MainActivity.java:
package com.example.login;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

6
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

publicclass MainActivity extends Activity {


EditText ed1,ed2;
Button bu;

@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.ed1);
ed2=(EditText)findViewById(R.id.ed2);
bu=(Button)findViewById(R.id.bu);

bu.setOnClickListener(new View.OnClickListener(){
@Override
publicvoid onClick(View v){
String a = ed1.getText().toString();
String b = ed2.getText().toString();
if(a.equals("Welcome")&& b.equals("abcd"))
{

Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Invalid User",Toast.LENGTH_LONG).show();
}
}
});
}
protectedvoid onResume(){
super.onResume();
}

@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}

7
OUTPUT:

RESULT:
Thus the application that uses Layout Managers and event listeners was successfully
created using eclipse.

8
EX.NO: 3 Write an application that draws basic graphical primitives on the screen
DATE:

AIM:
To develop an application that uses GUI components, Fonts and colors using Eclipse.

PROCEDURE:
1. Start the application by
2. Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
3. Configure the project by choosing the workspace location and then click the next button.
4. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
5. Create an activity by selecting blank activity and then click next.
6. A blank activity will be created and finally click finish button.
7. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
8. Type the following MainActivity.java Code
9. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
10. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
11. Once Eclipse has done preparing for the deployment, you will see a popup which ask for
adding a new android virtual device by clicking the yes button.
12. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

9
CODING:

MainActivity.java:
Package com.example.shapes;

import android.app.Activity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
publicclass MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(new myView(this));
}

private class myView extends View{

public myView(Context context) {


super(context);
}
protected void onDraw(Canvas canvas) {
Paint myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
canvas.drawRect(10, 10, 100, 100, myPaint);
canvas.drawLine(200, 200, 300, 300, myPaint);
canvas.drawCircle(200, 100, 50, myPaint);

canvas.drawText("Hello", 100, 50, myPaint);


}
}
}

10
OUTPUT:

RESULT:
Thus the application to draw the basic graphical primitive on the screen was successfully
created using eclipse.

11
EX.NO:4 Develop an application the makes use of database
DATE:

AIM:
To develop an application that makes use of a database using Eclipse.

PROCEDURE:

1. Start the application by


2. Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
3. Configure the project by choosing the workspace location and then click the next button.
4. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
5. Create an activity by selecting blank activity and then click next.
6. A blank activity will be created and finally click finish button.
7. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
8. Type the following XML program in layout code and java to perform the operation for
insertion, updating, and retrieve.
9. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
10. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
11. Once Eclipse has done preparing for the deployment, you will see a popup which ask
for adding a new android virtual device by clicking the yes button.
12. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

12
CODING:
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"

android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />

<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content
" android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" />

13
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content
" android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"

android:textSize="20sp" />

<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content
" android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />

<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content
" android:layout_x="12dp"
android:layout_y="248dp"
android:text="Insert"
android:textSize="30dp" />

<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content
" android:layout_x="171dp"
android:layout_y="248dp"
android:text="Delete"
android:textSize="30dp" />

14
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content
" android:layout_x="14dp"
android:layout_y="312dp"
android:text="Update"
android:textSize="30dp" />

<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content
" android:layout_x="188dp"
android:layout_y="314dp"
android:text="View"
android:textSize="30dp" />

</AbsoluteLayout>

MainActivity.java:

package com.example.ex5;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
importandroid.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
importandroid.widget.Toast;

publicclass MainActivity extends Activity implements OnClickListener {

EditText Rollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override

15
publicvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);

Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);

// Creating database and table


db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name
VARCHAR,marks VARCHAR);");
}
publicvoid onClick(View view)

// Inserting a record to the Student table


if(view==Insert)
{
// Checking for empty fields
if(Rollno.getText().toString().trim().length()==0||
Name.getText().toString().trim().length()==0||
Marks.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('"+Rollno.getText()+"','"+Name.getText()+
"','"+Marks.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table

16
if(view==Delete)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst()) {

db.execSQL("UPDATE student SET name='" + Name.getText() + "',marks='" + Marks.getText()


+
"' WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Rollno");
}

17
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
Name.setText(c.getString(1));
Marks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
}
publicvoid showMessage(String title,String message)
{
Builder builder=newBuilder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}

publicvoid clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}

18
OUTPUT:

19
RESULT:
Thus the application that uses database was successfully created using Eclipse.

20
EX.NO: 5 Develop a mobile application that make use of Notification Manager
DATE:

AIM:
To develop a mobile application that makes of Notification Manager
PROCEDURE:

1. Start the application by


2. Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
3. Configure the project by choosing the workspace location and then click the next button.
4. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
5. Create an activity by selecting blank activity and then click next.
6. A blank activity will be created and finally click finish button.
7. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
8. Type the following XML program in layout code and java to create an alert upon
receiving a message application.
9. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
10. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
11. Once Eclipse has done preparing for the deployment, you will see a popup which ask
for adding a new android virtual device by clicking the yes button.
12. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

21
CODING:
activity_main.xml
<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"
tools:context=".BroadcastPhoneStates">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="When new SMS will come it will show a alert message."/>

</RelativeLayout>
BroadcastNewSms.java:
package com.androidexample.broadcastreceiver;

import android.os.Bundle;
import android.app.Activity;

public class BroadcastNewSms extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.androidexample_broadcast_newsms);
}
}

IncomingSms.java:
package com.androidexample.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;

22
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager


final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the intent.


final Bundle bundle = intent.getExtras();

try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage =
SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber =
currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;


String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
Toast.makeText(context, "senderNum: "+ senderNum + "message: " + message,
Toast.LENGTH_LONG).show();
} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);

}
}
}

Android Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

23
OUTPUTS:

RESULT:
Thus the developed application for notification manager was done successfully

24
EX.NO: 6 Implement an application that implements Multi-threading
DATE:

AIM:
To develop an application that implements Multi-threading.

PROCEDURE:

1. Start the application by


2. Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
3. Configure the project by choosing the workspace location and then click the next button.
4. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
5. Create an activity by selecting blank activity and then click next.
6. A blank activity will be created and finally click finish button.
7. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
8. Type the following XML program in layout code and java to implement multithreading.
9. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
10. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
11. Once Eclipse has done preparing for the deployment, you will see a popup which ask
for adding a new android virtual device by clicking the yes button.
12. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device

25
CODING:
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/
>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"/>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignLeft="@+id/textView2"

26
android:layout_below="@+id/textView2"
android:hint="-1"/>

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:hint="0"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:onClick="clicked"
android:text="Start"/>

</RelativeLayout>

MainActivity.java
package com.example.multithreading;

import android.app.Activity;
import android.os.Bundle;
importandroid.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
publicclass MainActivity extends Activity {
TextView tv1, tv2, tv3, tv4;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.textView1);
tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
tv4 = (TextView)findViewById(R.id.textView4);

27
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}

publicvoid clicked(View view)


{
new Thread()
{
publicvoid run()
{
for(int i=1; i<=50; i++)
{
try
{
runOnUiThread(
new Runnable()
{
publicvoid run()
{
tv1.setText(tv1.getText().toString()+"0");
}
}
);
Thread.sleep(100);
}
catch(Exception e)
{
Log.i("", e.getMessage());
}
}
}
}.start();
new Thread()
{
publicvoid run()
{

28
for(int i=1; i<=50; i++)
{
try
{ runOnUiThread(
new Runnable()
{
publicvoid run()
{

tv2.setText(tv2.getText().toString()+"1");
}
}
);

Thread.sleep(100);
}
catch(Exception e)
{
Log.i("", e.getMessage());
}
}
}
}.start();
new Thread()
{
publicvoid run()
{
for(int i=1; i<=50; i++)
{
try
{
runOnUiThread(
new Runnable()
{
publicvoid run()
{
tv3.setHint(String.valueOf(Integer.parseInt(tv3.getHint().toString())+2));
tv3.setText(tv3.getText().toString()+" "+tv3.getHint().toString());
}
}
);

29
Thread.sleep(100);
}
catch(Exception e)

{
Log.i("", e.getMessage());
}
}
}
}.start();

new Thread()
{
publicvoid run()
{
for(int i=1; i<=50; i++)
{
try
{
runOnUiThread(
new Runnable()
{
}
}
);
Thread.sleep(100);
}
catch(Exception e)
{
Log.i("", e.getMessage());
}
}
}
}.start();
}
}

30
OUTPUT:

RESULT:
Thus the developed application that implements Multi-threading.

31
EX.NO: 7 Develop a mobile application that uses GPS location information
DATE:

AIM:
To develop a mobile application that uses GPS location information.

PROCEDURE:

1. Start the application by


2. Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
3. Configure the project by choosing the workspace location and then click the next button.
4. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
5. Create an activity by selecting blank activity and then click next.
6. A blank activity will be created and finally click finish button.
7. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
8. Type the following XML program in layout code and java to create GPS application.
9. Click windows-> open perspective-> DDMS. Select emulator under devices and in
Emulator Control Change the location controls.
10. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
11. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
12. Once Eclipse has done preparing for the deployment, you will see a popup which ask
for adding a new android virtual device by clicking the yes button.
13. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

32
CODING:
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Latitude and longitude"/>

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editTextlat"
android:layout_marginTop="23dp"
android:ems="10"/>

<requestFocus/>

<EditText
android:id="@+id/editTextlon"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="15dp"
android:ems="10"/>

</RelativeLayout>
33
MainActivity.java:
package com.example.emergency;

import com.example.emergency.R;

import android.app.Activity;

import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity


{

EditText textlat;

EditText textlon;

@Override

public void onCreate(Bundle savedInstanceState)


{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textlat = (EditText) findViewById(R.id.editText1);
textlon = (EditText) findViewById(R.id.editTextlon);
LocationManager mlocManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);

34
LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);

public class MyLocationListener implements LocationListener


{

public void onLocationChanged(Location loc)


{

Double lat=loc.getLatitude();

Double lon=loc.getLongitude();

textlat.setText(lat.toString());

textlon.setText(lon.toString());

public void onProviderDisabled(String provider)


{

Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();

public void onProviderEnabled(String provider)


{

Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();

public void onStatusChanged(String provider, int status, Bundle extras)


{

35
Android Manifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

OUTPUT:

RESULT:
Thus the developed application that implements GPS location information.

36
EX.NO: 8 Develop a mobile application that writes data to the SD card.
DATE:

AIM:
To develop a mobile application that writes data to the SD card.

PROCEDURE:

1. Start the application by


2. Click on File -> New and choose Android Application Project and type the
application name for the project and click on the next button.
3. Configure the project by choosing the workspace location and then click the next
button.
4. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
5. Create an activity by selecting blank activity and then click next.
6. A blank activity will be created and finally click finish button.
7. On clicking the finish button a new project will be created from which the java
code and layout code can be opened.
8. Type the following XML program in layout code and java to create application that
writes data to the SDcard.
9. Click windows-> open perspective-> DDMS. Select emulator under devices and in
File explorer Control, file will be saved under mnt->sdcard.
10. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
11. Watch the deployment preparation for the app by checking the bottom-right of
Eclipse for deployment progress.
12. Once Eclipse has done preparing for the deployment, you will see a popup which ask
for adding a new android virtual device by clicking the yes button.
13. A device chooser popup will be opened, select the device and click OK. Now the app
will be deployed on the device.

37
CODING:
activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/textView1"
android:layout_marginTop="32dp"
android:onClick="onReadClicked"
android:text="Read"/>

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10">

<requestFocus/>
</EditText>

38
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/editText1"
android:layout_marginTop="34dp"
android:onClick="onWriteClicked"
android:text="Write"/>

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:onClick="onClearClicked"
android:text="Clear"/>

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="21dp"
android:onClick="onExitClicked"
android:text="Exit"/>

</RelativeLayout>
MainActivity.java:
package com.example.sdcard;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
39
import java.io.InputStreamReader;

import com.example.sdcard.R.id;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


TextView tv;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onWriteClicked(View v)
{
try
{

File f=new File(Environment.getExternalStorageDirectory()+"/myfile.txt"); FileOutputStream fos= new


FileOutputStream(f);
String s= et.getText().toString();
fos.write(s.getBytes()); fos.close();
Toast.makeText(getApplicationContext(),"Written to
File",Toast.LENGTH_LONG).show();

}
catch(Exception e)
{
e.printStackTrace();
}
}

40
public void onReadClicked(View v)
{
try
{

File f = new File(Environment.getExternalStorageDirectory() + "/myfile.txt"); FileInputStream fis = new


FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fis)); tv.setText(br.readLine());
Toast.makeText(getApplicationContext(), "Read from file.",
Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void onClearClicked(View v)


{
try
{

File f = new File(Environment.getExternalStorageDirectory() + "/myfile.txt"); f.delete();


tv.setText("");
et.setText("");
Toast.makeText(getApplicationContext(), "Cleared.",
Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void onExitClicked(View v)


{
System.exit(0);}
}Android Manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

41
OUTPUT:

RESULT:
Thus the developed application that writes data to the SD card.

42
EX.NO: 9 Develop a mobile application that creates an alert upon receiving a message.
DATE:

AIM:
To develop a mobile application that creates an alert upon receiving a message.

PROCEDURE:

13. Start the application by


14. Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
15. Configure the project by choosing the workspace location and then click the next button.
16. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
17. Create an activity by selecting blank activity and then click next.
18. A blank activity will be created and finally click finish button.
19. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
20. Type the following XML program in layout code and java to create an alert upon
receiving a message application.
21. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
22. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
23. Once Eclipse has done preparing for the deployment, you will see a popup which ask
for adding a new android virtual device by clicking the yes button.
24. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

43
CODING:
activity_main.xml
<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"

tools:context=".BroadcastPhoneStates">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="When new SMS will come it will show a alert message."/>

</RelativeLayout>
BroadcastNewSms.java:
package com.androidexample.broadcastreceiver;

import android.os.Bundle;
import android.app.Activity;

public class BroadcastNewSms extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.androidexample_broadcast_newsms);
}
}
IncomingSms.java:
package com.androidexample.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import
android.content.Intent;
import android.os.Bundle;

import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

44
public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager


final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the intent.


final Bundle bundle = intent.getExtras();

try {
if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage =
SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber =
currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;


String message = currentMessage.getDisplayMessageBody();

Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " +


message);

Toast.makeText(context, "senderNum: "+ senderNum + ",


message: " + message, Toast.LENGTH_LONG).show();
} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);

}
}
}

Android Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
45
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

OUTPUT:

RESULT:
Thus the developed application that creates an alert upon receiving a message.

46
EX.NO:10 Develop application that makes use of RSS Feed
DATE:

AIM:
To develop an application that makes use of a RSS Feed using Eclipse.

PROCEDURE:

1. Start the application by


2. Click on File -> New and choose Android Application Project and type the application
name for the project and click on the next button.
3. Configure the project by choosing the workspace location and then click the next button.
4. Configure the Launcher Icon by choosing the attributes of icon sets like foreground
image, scaling, shape and its background color and then click on to next button.
5. Create an activity by selecting blank activity and then click next.
6. A blank activity will be created and finally click finish button.
7. On clicking the finish button a new project will be created from which the java code and
layout code can be opened.
8. Type the following XML program in layout code and java to Fetch and display the web
content using XML Parser.
9. Activate the INTERNET Permission in AndroidManifest.xml file.
10. Run the project by right clicking on your project in Package Explorer window on the
left and choose Run As => Android Application.
11. Watch the deployment preparation for the app by checking the bottom-right of Eclipse
for deployment progress.
12. Once Eclipse has done preparing for the deployment, you will see a popup which ask
for adding a new android virtual device by clicking the yes button.
13. A device chooser popup will be opened, select the device and click OK. Now the app will
be deployed on the device.

47
CODING:
activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/imageView"
android:hint="Title"
android:textColorHint="#ff69ff0e"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:textColorHint="#ff21ff11"
android:hint="Link"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"

48
android:hint="Description"
android:textColorHint="#ff33ff20"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fetch"
android:id="@+id/button"
android:layout_below="@+id/editText3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"

android:layout_toLeftOf="@+id/imageView"
android:layout_toStartOf="@+id/imageView"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_alignRight="@+id/editText3"
android:layout_alignEnd="@+id/editText3"/
>
</RelativeLayout>

activity_second.xml
<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_gravity="center_horizontal"/>
</LinearLayout>

49
MainActivity.java

package com.example.rssfeed;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;

importandroid.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
importandroid.widget.TextView;
importjava.util.Set;

publicclass MainActivity extends Activity {


EditText title,link,description;
Button b1,b2;
private String finalUrl="http://tutorialspoint.com/android/sampleXML.xml";
private HandleXML obj;

@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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


link = (EditText) findViewById(R.id.editText2);
description = (EditText) findViewById(R.id.editText3);

b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
obj = new
HandleXML(finalUrl);
obj.fetchXML();

50
while(obj.parsingComplete);
title.setText(obj.getTitle());
link.setText(obj.getLink());
description.setText(obj.getDescription());
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
Intent in=newIntent(MainActivity.this,Second.class);
startActivity(in);
}
});
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
returntrue;
}

@Override
publicboolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
}

HandleXML.java

package com.example.rssfeed;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import
org.xmlpull.v1.XmlPullParserFactory;
importandroid.util.Log;

51
publicclass HandleXML {
private String title = "title";
private String link = "link";
private String description = "description";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
publicvolatilebooleanparsingComplete = true;

public HandleXML(String url){


this.urlString = url;
}

public String getTitle(){


returntitle;
}

public String getLink(){


returnlink;
}

public String getDescription(){


returndescription;
}

publicvoid parseXMLAndStoreIt(XmlPullParser myParser) {


int event;
String text=null;

try {
event = myParser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {


String name=myParser.getName();

switch (event){
case XmlPullParser.START_TAG:
break;

case XmlPullParser.TEXT:
text = myParser.getText();
break;

52
case XmlPullParser.END_TAG:

if(name.equals("title")){
title = text;
}
elseif(name.equals("link")){
link = text;
}
elseif(name.equals("description")){
description = text;
}
else{
}
break;
}
event = myParser.next();
}
parsingComplete = false;
}
catch (Exception e) {
e.printStackTrace();
}
}

publicvoid fetchXML(){
Thread thread = newThread(new Runnable(){
@Override
publicvoid run() {

try {

URL url = newURL(urlString);


HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");

53
conn.setDoInput(true);

// Starts the query


conn.connect();
InputStream stream = conn.getInputStream();

xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();

myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);

parseXMLAndStoreIt(myparser);
stream.close();
}
catch (Exception e) {
}
}
});
thread.start();
}
}

Second.java

package com.example.rssfeed;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

publicclass Second extends Activity {


@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
WebView w1=(WebView)findViewById(R.id.webView);
w1.loadUrl("http://tutorialspoint.com/android/sampleXML.xml");
}
}

54
Menu --> second.xml

<menuxmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>

strings.xml

<?xmlversion="1.0"encoding="utf-8"?>

<resources>
<stringname="app_name">RssFeed</string>
<stringname="action_settings">Settings</string>
<stringname="hello_world">Hello world!</string>
<stringname="title_activity_second">Second</string>
</resources>

AndroidManifest.xml

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rssfeed"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>

<application
android:allowBackup="true"

55
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name="com.example.rssfeed.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.rssfeed.Second"
android:label="@string/title_activity_second">
</activity>
</application><uses-permissionandroid:name="android.permission.INTERNET"/>
</manifest>

56
OUTPUT:

RESULT:
Thus the application that uses RSS Feed was successfully created using Eclipse.

57
EX.NO:11 Develop a mobile application to send an email
DATE:

AIM:

To develop a mobile application to send an email.

PROCEDURE:

1. You will use Android studio to create an Android application and name it as Appunder a
package com.example.app.
2. Modify src/MainActivity.java file and add required code to take care of sending email.
3. Modify layout XML file res/layout/activity_main.xml add any GUI component if
required. I'm adding a simple button to launch Email Client.
4. Modify res/values/strings.xml to define required constant values
5. Modify AndroidManifest.xml as shown below
6. Run the application to launch Android emulator and verify the result of the changes
done in the application.

CODING:
activity_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" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sending Mail Example"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:text="Tutorials point "
58
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_alignRight="@+id/imageButton"

android:layout_alignEnd="@+id/imageButton" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<Button
android:id="@+id/sendEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content
"
android:text="@string/compose_email"/>

</LinearLayout>

MainActivity.java

package com.example.app;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button startBtn = (Button) findViewById(R.id.sendEmail);


startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendEmail();
59
}
});}

protected void sendEmail() { Log.i("Sendemail", "");


String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);

emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");

try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There is no email client installed.",
Toast.LENGTH_SHORT).show();
}
}
}

strings.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">App</string>
<string name="compose_email">Compose Email</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.App" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

60
<activity
android:name="com.example.app.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>

61
OUTPUT:

RESULT:
Thus the application that send an email is executed successfully.

62

You might also like