67% found this document useful (3 votes)
5K views

CS8662 - Mobile Application Development Lab Manual

The document is a lab manual for the course CS8662 - Mobile Application Development. It contains 12 experiments related to developing mobile applications using various Android concepts like GUI components, layout managers, event handlers, databases, notifications, threading, GPS, SD card access, alerts and RSS feeds. For each experiment, it provides the aim, procedure and sample code to implement the concept.

Uploaded by

Bharath kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
67% found this document useful (3 votes)
5K views

CS8662 - Mobile Application Development Lab Manual

The document is a lab manual for the course CS8662 - Mobile Application Development. It contains 12 experiments related to developing mobile applications using various Android concepts like GUI components, layout managers, event handlers, databases, notifications, threading, GPS, SD card access, alerts and RSS feeds. For each experiment, it provides the aim, procedure and sample code to implement the concept.

Uploaded by

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

https://www.ptrcet.

org Department of Computer Science and Engineering

Thanapandiyan Nagar, AIIMS Road, Satellite Town,


Madurai – 8.

LAB MANUAL
Regulation : 2017
Branch : B.E., (CSE)
Year & Semester : III Year / VI Semester

CS8662 – MOBILE APPLICATION DEVELOPMENT 1


https://www.ptrcet.org Department of Computer Science and Engineering

ANNA UNIVERSITY, CHENNAI.

AFFILIATED INSTITUTIONS

REGULATIONS – 2017
CHOICE BASED CREDIT SYSTEM
CS8662 – MOBILE APPLICATION DEVELOPMENT LABORATORY

LIST OF EXPERIMENTS
1. Develop an application that uses GUI components, Font and Colours.
2. Develop an application that uses Layout managers and event Handlers.
3. Write an application that draws basic graphical primitives on the screen.
4. Develop an application that makes use of Database.
5. Develop an application that makes use of Notification manager.
6. Implement an application that uses Multi – threading.
7. Develop a native application that uses GPS location information.
8. Implement an application that writes data to the SD card.
9. Implement an application that creates an alert upon receiving a message.
10. Write a mobile application that makes use of RSS feed.
11. Develop a mobile application to send an email.

TOTAL : 60 PERIODS

CS8662 – MOBILE APPLICATION DEVELOPMENT 2


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 1
DEVELOP AN APPLICATION THAT USES GUI
COMPONENTS, FONT AND COLOURS

AIM :
To write an Android application program that uses GUI Components,
Fonts, and Colors.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new android project
3. Select and double click your project name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter your activity_main.xml code
6. Then, go to src folder and double click your MainActivity.java file
7. Enter your MainActivity.java code
8. Finally go to run configuration select your avd and run your android
program.

PROGRAM :
activity_main.xml
<RelativeLayout
xmlns: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_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="PTRCSE-CS8662”
android:textStyle="bold"
android:textSize="20sp"/>
CS8662 – MOBILE APPLICATION DEVELOPMENT 3
https://www.ptrcet.org Department of Computer Science and Engineering

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="Change Colour"
android:textSize="20sp"/>

<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="35dp"
android:text="Change Font"
android:textSize="20sp" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="Change Font Size"
android:textSize="20sp" />

</RelativeLayout>

MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Color;
import android.graphics.Typeface;

public class MainActivity extends Activity {


float font=24;
int i=1;
int j=1;

CS8662 – MOBILE APPLICATION DEVELOPMENT 4


https://www.ptrcet.org Department of Computer Science and Engineering

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t1=(TextView) findViewById(R.id.textView1);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view1) {
t1.setTextSize(font);
font=font+4;
if(font==40)
font=20;

}
});
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
switch(i)
{
case 1:
t1.setTextColor(Color.parseColor("#0000FF"));
break;
case 2:
t1.setTextColor(Color.parseColor("#00FF00"));
break;
case 3:
t1.setTextColor(Color.parseColor("#FF0000"));
break;
case 4:
t1.setTextColor(Color.parseColor("#800000"));
break;
}
i++;
if(i==5)
i=1;
}
});
Button b3 = (Button) findViewById(R.id.button3);
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
switch(j)
{
case 1:
t1.setTypeface(Typeface.SANS_SERIF);
break;
case 2:
t1.setTypeface(Typeface.SERIF);
CS8662 – MOBILE APPLICATION DEVELOPMENT 5
https://www.ptrcet.org Department of Computer Science and Engineering

break;
case 3:
t1.setTypeface(Typeface.MONOSPACE);
break;
case 4:
t1.setTypeface(Typeface.DEFAULT_BOLD);
break;
}
j++;
if(j==5)
j=1;
}
});
}

RESULT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 6


https://www.ptrcet.org Department of Computer Science and Engineering

Thus, the Android application program was executed successfully and


verified.
OUTPUT :

Changing Font Size, by clicking Change Font Size

CS8662 – MOBILE APPLICATION DEVELOPMENT 7


https://www.ptrcet.org Department of Computer Science and Engineering

Changing Font Colour, by clicking Change Colour

CS8662 – MOBILE APPLICATION DEVELOPMENT 8


https://www.ptrcet.org Department of Computer Science and Engineering

Changing Font, by clicking Change Font

CS8662 – MOBILE APPLICATION DEVELOPMENT 9


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 2 DEVELOP AN APPLICATION THAT USES


LAYOUT MANAGERS AND EVENT
LISTENERS

AIM :
To write an Android application program that uses Layout managers
and Event listeners.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new android project
3. Select and double click your project name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter your activity_main.xml code
6. Then, go to src folder and double click your MainActivity.java file
7. Enter your MainActivity.java code
8. Finally go to run configuration select your avd and run your android
program.

PROGRAM :
activity_main.xml
<RelativeLayout
xmlns: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=".MainActivity" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LAYOUT and EVENT"

CS8662 – MOBILE APPLICATION DEVELOPMENT 10


https://www.ptrcet.org Department of Computer Science and Engineering

android:textSize="20dp" />

</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENTER NO 1 :" >
</TextView>

<EditText
android:id="@+id/edittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.19"
android:inputType="number" >

</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENTER NO 2 :" >
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:id="@+id/edittext2"
android:inputType="number">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
CS8662 – MOBILE APPLICATION DEVELOPMENT 11
https://www.ptrcet.org Department of Computer Science and Engineering

android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout3" >
<Button
android:layout_width="wrap_content"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="ADD"
android:layout_weight="0.50" />
<Button
android:layout_width="wrap_content"
android:id="@+id/button3"
android:layout_height="wrap_content"
android:text="SUB"
android:layout_weight="0.50" />
<Button
android:layout_width="wrap_content"
android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="CLEAR"
android:layout_weight="0.50" />
</LinearLayout>
<View
android:layout_height="2px"
android:layout_width="fill_parent"
android:layout_below="@+id/linearLayout4"
android:background="#DDFFDD"/>
</RelativeLayout>

MainActivity.java
package com.example.second;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


/** Called when the activity is first created. */
EditText txtData1,txtData2;
float num1,num2,result1,result2;

@Override
protected void onCreate(Bundle savedInstanceState) {
CS8662 – MOBILE APPLICATION DEVELOPMENT 12
https://www.ptrcet.org Department of Computer Science and Engineering

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button add = (Button) findViewById(R.id.button1);


add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
txtData1 = (EditText) findViewById(R.id.edittext1);
txtData2 = (EditText) findViewById(R.id.edittext2);
num1 = Float.parseFloat(txtData1.getText().toString());
num2 = Float.parseFloat(txtData2.getText().toString());
result1=num1+num2;
Toast.makeText(getBaseContext()," YOUR ANSWER is : "+result1,
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});

Button sub = (Button) findViewById(R.id.button3);


sub.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
txtData1 = (EditText) findViewById(R.id.edittext1);
txtData2 = (EditText) findViewById(R.id.edittext2);
num1 = Float.parseFloat(txtData1.getText().toString());
num2 = Float.parseFloat(txtData2.getText().toString());
result2=num1-num2;
Toast.makeText(getBaseContext()," YOUR ANSWER is : "+result2,
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});

Button clear = (Button) findViewById(R.id.button2);


clear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
CS8662 – MOBILE APPLICATION DEVELOPMENT 13
https://www.ptrcet.org Department of Computer Science and Engineering

{
txtData1.setText("");
txtData2.setText("");
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
}

RESULT :
Thus, the Android application program was executed successfully and
verified.

CS8662 – MOBILE APPLICATION DEVELOPMENT 14


https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

We give two inputs in the EditText control, then click on ADD

CS8662 – MOBILE APPLICATION DEVELOPMENT 15


https://www.ptrcet.org Department of Computer Science and Engineering

We give two inputs in the EditText control, then click on SUB

If we click on CLEAR, then all the inputs are given in the EditText are cleared.

CS8662 – MOBILE APPLICATION DEVELOPMENT 16


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 3
AN APPLICATION THAT DRAWS BASIC
GRAPHICAL PRIMITIVES ON THE SCREEN

AIM :
To write an Android application program that draws basic graphical
primitives on the screen.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new android project
3. Select and double click your project name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter your activity_main.xml code
6. Then, go to src folder and double click your MainActivity.java file
7. Enter your MainActivity.java code
8. Finally go to run configuration select your avd and run your android
program.

PROGRAM :
activity_main.xml
<RelativeLayout
xmlns: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="${relativePackage}.${activityClass}">

</RelativeLayout>

MainActivity.java
package com.example.three;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
CS8662 – MOBILE APPLICATION DEVELOPMENT 17
https://www.ptrcet.org Department of Computer Science and Engineering

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {


myview mv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mv = new myview(this);
setContentView(mv);
}
private class myview extends View
{
public myview(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint=new Paint();
paint.setTextSize(20);
paint.setColor(Color.GREEN);
canvas.drawText("Circle", 55, 30, paint);
paint.setColor(Color.RED);
canvas.drawCircle(100, 150,100, paint);
paint.setColor(Color.GREEN);
canvas.drawText("Rectangle", 255, 30, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(250, 50,400,350, paint);
paint.setColor(Color.GREEN);
canvas.drawText("SQUARE", 55, 430, paint);
paint.setColor(Color.BLUE);
canvas.drawRect(50, 450,150,550, paint);
paint.setColor(Color.GREEN);
canvas.drawText("LINE", 255, 430, paint);
paint.setColor(Color.BLACK);
canvas.drawLine(250, 500, 350, 500, paint);
}
}
RESULT :
Thus, the Android application program was executed successfully and
verified.

CS8662 – MOBILE APPLICATION DEVELOPMENT 18


https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 19


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 4
IMPLEMENT AN APPLICATION THAT USES
MULTI - THREADING

AIM :
To write an Android application program that implements Multi-
threading.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new android project
3. Select and double click your project name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter your activity_main.xml code
6. Then, go to src folder and double click your MainActivity.java file
7. Enter your MainActivity.java code
8. Finally go to run configuration select your avd and run your android
program.

PROGRAM :
activity_main.xml
<RelativeLayout
xmlns: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_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="PTRCSE - CS8662"
android:textStyle="bold"
android:textSize="10pt" />

CS8662 – MOBILE APPLICATION DEVELOPMENT 20


https://www.ptrcet.org Department of Computer Science and Engineering

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Start MULTITHREAD"
android:textStyle="bold"
android:onClick="fetchData" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="16dp"
android:text="Main Thread" />

</RelativeLayout>

MainActivity.java
package com.example.four;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {


private TextView tvOutput;
private static final int t1 = 1;
private static final int t2 = 2;
private static final int t3 = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOutput = (TextView) findViewById(R.id.textView2);
}
public void fetchData(View v) {
tvOutput.setText("Main PTRCSE - thread");
thread1.start();
CS8662 – MOBILE APPLICATION DEVELOPMENT 21
https://www.ptrcet.org Department of Computer Science and Engineering

thread2.start();
thread3.start();
}
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++)
{
try {
Thread.sleep(1005);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(t1);
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++)
{
try {
Thread.sleep(1002);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(t2);
}
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(t3);
}
}
});
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
CS8662 – MOBILE APPLICATION DEVELOPMENT 22
https://www.ptrcet.org Department of Computer Science and Engineering

if(msg.what == t1) {
tvOutput.append("\nIn PTRCSE - thread 1");
}
if(msg.what == t2) {
tvOutput.append("\nIn PTRCSE - thread 2");
}
if(msg.what == t3) {
tvOutput.append("\nIn PTRCSE - thread 3");
}
}
};
}

RESULT :
Thus, the Android application program was executed successfully and
verified.
CS8662 – MOBILE APPLICATION DEVELOPMENT 23
https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 24


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 5
DEVELOP AN APPLICATION THAT MAKES
USE OF DATABASE

AIM :
To write an Android application program that makes use of database.
PROCEDURE :
1.
Open eclipse or android studio
2.
Create a new Android project
3.
Select and double click our project Name
4.
Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter our activity_main.xml code
6. Then, go to res folder and select values double click the string.xml
file
7. Enter our string.xml code
8. Then, go to src folder and double click our MainActivity.java file
9. Enter our MainActivity.java code.
10. Finally go to run configuration select our AVD and run our Android
program.

PROGRAM :

activity_main.xml

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


<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="17dp"
android:text="@string/ptrcse_student_form"
android:textSize="10pt"
android:textStyle="bold" />

CS8662 – MOBILE APPLICATION DEVELOPMENT 25


https://www.ptrcet.org Department of Computer Science and Engineering

<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_x="19dp"
android:layout_y="60dp"
android:text="Registration No" />

<EditText
android:id="@+id/editRegistrationNo"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_x="122dp"
android:layout_y="55dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>

<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_x="17dp"
android:layout_y="112dp"
android:text="Student Name"
android:textAlignment="center" />

<EditText
android:id="@+id/editStudentName"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_x="119dp"
android:layout_y="109dp"
android:ems="10"
android:inputType="text" />

<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_x="19dp"
android:layout_y="164dp"
android:text="Year" />

<EditText
android:id="@+id/editYear"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_x="119dp"
android:layout_y="160dp"
android:ems="10"
CS8662 – MOBILE APPLICATION DEVELOPMENT 26
https://www.ptrcet.org Department of Computer Science and Engineering

android:inputType="text" />

<Button
android:id="@+id/butAdd"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="207dp"
android:text="ADD"
android:textStyle="bold" />

<Button
android:id="@+id/butDelete"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="207dp"
android:text="DELETE"
android:textStyle="bold" />

<Button
android:id="@+id/butModify"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_x="17dp"
android:layout_y="264dp"
android:text="MODIFY"
android:textStyle="bold" />

<Button
android:id="@+id/butView"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="264dp"
android:text="VIEW"
android:textStyle="bold" />

<Button
android:id="@+id/butViewAll"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_x="81dp"
android:layout_y="324dp"
android:text="VIEW ALL"
android:textStyle="bold" />

</AbsoluteLayout>

CS8662 – MOBILE APPLICATION DEVELOPMENT 27


https://www.ptrcet.org Department of Computer Science and Engineering

MainActivity.java

package com.example.five;

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

public class MainActivity extends Activity implements OnClickListener {


EditText editRegistrationNo,editStudentName,editYear;
Button butAdd,butDelete,butModify,butView,butViewAll;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editRegistrationNo=(EditText)findViewById(R.id.editRegistrationNo);
editStudentName=(EditText)findViewById(R.id.editStudentName);
editYear=(EditText)findViewById(R.id.editYear);
butAdd=(Button)findViewById(R.id.butAdd);
butDelete=(Button)findViewById(R.id.butDelete);
butModify=(Button)findViewById(R.id.butModify);
butView=(Button)findViewById(R.id.butView);
butViewAll=(Button)findViewById(R.id.butViewAll);

butAdd.setOnClickListener(this);
butDelete.setOnClickListener(this);
butModify.setOnClickListener(this);
butView.setOnClickListener(this);
butViewAll.setOnClickListener(this);

db=openOrCreateDatabase("studentDB", Context.MODE_PRIVATE,
null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(regno
VARCHAR,studname VARCHAR,year VARCHAR);");
}
public void onClick(View view)
{
CS8662 – MOBILE APPLICATION DEVELOPMENT 28
https://www.ptrcet.org Department of Computer Science and Engineering

if(view==butAdd)
{
if(editRegistrationNo.getText().toString().trim().length()==0 ||
editStudentName.getText().toString().trim().length()==0 ||
editYear.getText().toString().trim().length()==0)
{
showMessage("PTRCSE - Error", "Please enter All Details");
return;
}
db.execSQL("INSERT INTO student
VALUES('"+editRegistrationNo.getText()+"','"+editStudentName.getText()+
"','"+editYear.getText()+"');");
showMessage("Success", "Record added");
clearText();
}

if(view==butDelete)
{
if(editRegistrationNo.getText().toString().trim().length()==0)
{
showMessage("PTRCSE - Error", "Please enter Registration
No");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
regno='"+editRegistrationNo.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE
regno='"+editRegistrationNo.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("PTRCSE - Error", "Invalid Registration No");
}
clearText();
}

if(view==butModify)
{
if(editRegistrationNo.getText().toString().trim().length()==0)
{
showMessage("PTRCSE - Error", "Please enter Registration
No");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
regno='"+editRegistrationNo.getText()+"'", null);
CS8662 – MOBILE APPLICATION DEVELOPMENT 29
https://www.ptrcet.org Department of Computer Science and Engineering

if(c.moveToFirst())
{
db.execSQL("UPDATE student SET
studname='"+editStudentName.getText()+"',year='"+editYear.getText()+ "'
WHERE regno='"+editRegistrationNo.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("PTRCSE - Error", "Invalid Rollno");
}
clearText();
}

if(view==butView)
{
if(editRegistrationNo.getText().toString().trim().length()==0)
{
showMessage("PTRCSE - Error", "Please enter Registration
No");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
regno='"+editRegistrationNo.getText()+"'", null);
if(c.moveToFirst())
{
editStudentName.setText(c.getString(1));
editYear.setText(c.getString(2));
}
else
{
showMessage("PTRCSE - Error", "Invalid Registration No");
clearText();
}
}

if(view==butViewAll)
{
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("PTRCSE - Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Registration No: "+c.getString(0)+"\n");
buffer.append("Student Name: "+c.getString(1)+"\n");
CS8662 – MOBILE APPLICATION DEVELOPMENT 30
https://www.ptrcet.org Department of Computer Science and Engineering

buffer.append("Year: "+c.getString(2)+"\n\n");
}
showMessage("PTRCSE Student Details", buffer.toString());
}
}

public void showMessage(String title,String message) {


Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}

public void clearText() {


editRegistrationNo.setText("");
editStudentName.setText("");
editYear.setText("");
editRegistrationNo.requestFocus();
}
}

string.xml

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


<resources>
<string name="app_name">Student Detail</string>
<string name="ptrcse_student_form">PTRCSE - STUDENT
FORM</string>
<string name="hello">Hello World, Student detail Activity!</string>
<string name="title">Student Details</string>
<string name="RegistrationNo">Enter Registration No: </string>
<string name="StudentName">Enter Student Name: </string>
<string name="Year">Enter Year: </string>
<string name="Add">Add Student</string>
<string name="Delete">Delete Student</string>
<string name="Modify">Modify Student</string>
<string name="View">View Student</string>
<string name="ViewAll">View All Students</string>
</resources>

RESULT :
Thus, the Android application program was executed successfully and
verified.
CS8662 – MOBILE APPLICATION DEVELOPMENT 31
https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 32


https://www.ptrcet.org Department of Computer Science and Engineering

After inserting all records… then we want to display all records, click on
‘VIEW ALL’

CS8662 – MOBILE APPLICATION DEVELOPMENT 33


https://www.ptrcet.org Department of Computer Science and Engineering

If we want to modify the existing record… give the inputs in the EditText, then
click on ‘MODIFY’

CS8662 – MOBILE APPLICATION DEVELOPMENT 34


https://www.ptrcet.org Department of Computer Science and Engineering

If we want to delete the existing record, First we give the Registration No, then
click on ‘DELETE’

CS8662 – MOBILE APPLICATION DEVELOPMENT 35


https://www.ptrcet.org Department of Computer Science and Engineering

If we want to display the existing record individually, First give the


Registration No, then click on ‘VIEW’

CS8662 – MOBILE APPLICATION DEVELOPMENT 36


https://www.ptrcet.org Department of Computer Science and Engineering

If we want to insert a new record, but we didn’t give any values of any
column, it shows error message as,

If we try to UPDATE, DELETE, VIEW non-existing record, it shows error


message as,

CS8662 – MOBILE APPLICATION DEVELOPMENT 37


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 6
DEVELOP A NATIVE APPLICATION THAT
USES GPS LOCATION INFORMATION

AIM :
To write an Android application program that uses GPS Location
information.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new Android project
3. Select and double click our project Name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter our activity_main.xml code
6. Then, go to src folder and double click our MainActivity.java file
7. Enter our MainActivity.java code
8. Enter our AndroidManifest.xml file
9. Finally go to run configuration select our AVD and run our Android
program.

PROGRAM:

activity_main.xml
<RelativeLayout
xmlns: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" >

<Button
android:id="@+id/butShowLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="154dp"
android:text="SHOW LOCATION"
android:textStyle="bold" />

</RelativeLayout>
CS8662 – MOBILE APPLICATION DEVELOPMENT 38
https://www.ptrcet.org Department of Computer Science and Engineering

MainActivity.java

package com.example.six;

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

public class MainActivity extends Activity {


Button butShowLocation;
GPStrace gps;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butShowLocation=(Button)findViewById(R.id.butShowLocation);
butShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
gps=new GPStrace(MainActivity.this);
if(gps.canGetLocation()){
double latitude=gps.getLatitude();
double longitude=gps.getLongtiude();
Toast.makeText(getApplicationContext(),"Your
Location is \nLat:"+latitude+"\nLong:"+longitude,
Toast.LENGTH_LONG).show();
}
else
{
gps.showSettingAlert();
}
}
});
}
}

GPStrace.java

package com.example.six;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
CS8662 – MOBILE APPLICATION DEVELOPMENT 39
https://www.ptrcet.org Department of Computer Science and Engineering

import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;

public class GPStrace extends Service implements LocationListener{


private final Context context;
boolean isGPSEnabled=false;
boolean canGetLocation=false;
boolean isNetworkEnabled=false;
Location location;
double latitude;
double longtitude;
private static final long
MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
private static final long MIN_TIME_BW_UPDATES=1000*60*1;
protected LocationManager locationManager;
public GPStrace(Context context)
{
this.context=context;
getLocation();
}
public Location getLocation()
{
try{
locationManager=(LocationManager)
context.getSystemService(LOCATION_SERVICE);

isGPSEnabled=locationManager.isProviderEnabled(LocationManager.G
PS_PROVIDER);

isNetworkEnabled=locationManager.isProviderEnabled(LocationManag
er.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled)
{
}
else{
this.canGetLocation=true;
if(isNetworkEnabled){

locationManager.requestLocationUpdates(LocationManager.NETWORK
_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPD
ATES,this);
}
if(locationManager!=null){

location=locationManager.getLastKnownLocation(LocationManager.NE
TWORK_PROVIDER);
CS8662 – MOBILE APPLICATION DEVELOPMENT 40
https://www.ptrcet.org Department of Computer Science and Engineering

if(location !=null){
latitude=location.getLatitude();

longtitude=location.getLongitude();
}
}
}
if(isGPSEnabled){
if(location==null){

locationManager.requestLocationUpdates(LocationManager.GPS_PRO
VIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES
, this);
if(locationManager!=null){

location=locationManager.getLastKnownLocation(LocationManager.GP
S_PROVIDER);
if(location!=null){

latitude=location.getLatitude();

longtitude=location.getLongitude();
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return location;
}

public void stopUsingGPS(){


if(locationManager!=null){
locationManager.removeUpdates(GPStrace.this);
}
}

public double getLatitude(){


if(location!=null){
latitude=location.getLatitude();
}
return latitude;
}

public double getLongtiude(){


if(location!=null){
CS8662 – MOBILE APPLICATION DEVELOPMENT 41
https://www.ptrcet.org Department of Computer Science and Engineering

longtitude=location.getLatitude();
}
return longtitude;
}

public boolean canGetLocation(){


return this.canGetLocation;
}

public void showSettingAlert(){


AlertDialog.Builder alertDialog=new
AlertDialog.Builder(context);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled.Do you want to
go to setting menu?");
alertDialog.setPositiveButton("settings", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which){
Intent intent=new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override

CS8662 – MOBILE APPLICATION DEVELOPMENT 42


https://www.ptrcet.org Department of Computer Science and Engineering

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


{
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}

RESULT :
Thus, the Android application program was executed successfully and
verified.
CS8662 – MOBILE APPLICATION DEVELOPMENT 43
https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 44


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 7
IMPLEMENT AN APPLICATION THAT
WRITES DATA TO THE SD CARD

AIM :
To write an Android application program that writes the data to the SD
card.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new Android project
3. Select and double click our project Name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter our activity_main.xml code
6. Then, go to src folder and double click our MainActivity.java file
7. Enter our MainActivity.java code
8. Enter our AndroidManifest.xml file
9. Finally go to run configuration select our AVD and run our Android
program.

PROGRAM:

activity_main.xml

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


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

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVE DATA"
android:textStyle="bold" />

CS8662 – MOBILE APPLICATION DEVELOPMENT 45


https://www.ptrcet.org Department of Computer Science and Engineering

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SHOW DATA"
android:textStyle="bold" />

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

</LinearLayout>

ActivityMain.java

package com.example.seven;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Environment;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


Button Save,Load;
EditText message;
TextView t1;
String Message1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Save=(Button)findViewById(R.id.button1);
Load=(Button)findViewById(R.id.button2);
message=(EditText)findViewById(R.id.editText1);
t1=(TextView)findViewById(R.id.textView1);

CS8662 – MOBILE APPLICATION DEVELOPMENT 46


https://www.ptrcet.org Department of Computer Science and Engineering

Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message1=message.getText().toString();
try{

//Create a new folder called MyDirectory in SDCard


File
sdcard=Environment.getExternalStorageDirectory();
File directory=new
File(sdcard.getAbsolutePath()+"/MyDirectory");
directory.mkdir();

//Create a new file name textfile.txt inside MyDirectory


File file=new File(directory,"textfile.txt");
//Create File Outputstream to read the file
FileOutputStream fou=new
FileOutputStream(file);
OutputStreamWriter osw=new
OutputStreamWriter(fou);
try{
//write a user data to file
osw.append(Message1);
osw.flush();
osw.close();

Toast.makeText(getBaseContext(),"PTRCSE - Data
Saved",Toast.LENGTH_LONG).show();
}catch(IOException e){
e.printStackTrace();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
});
Load.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
try{
File sdcard=Environment.getExternalStorageDirectory();
File directory=new
File(sdcard.getAbsolutePath()+"/MyDirectory");
File file=new File(directory,"textfile.txt");
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
char[] data=new char[100];
String final_data="";
int size;
try{
CS8662 – MOBILE APPLICATION DEVELOPMENT 47
https://www.ptrcet.org Department of Computer Science and Engineering

while((size=isr.read(data))>0)
{
//read a data from file
String read_data=String.copyValueOf(data,0,size);
final_data+=read_data;
data=new char[100];
}
//display the data in output
Toast.makeText(getBaseContext(),"PTRCSE - Message :
"+final_data,Toast.LENGTH_LONG).show();
}catch(IOException e){
e.printStackTrace();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
});
}
}

AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.seven"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

<activity
android:name="com.example.seven.MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
CS8662 – MOBILE APPLICATION DEVELOPMENT 48
https://www.ptrcet.org Department of Computer Science and Engineering

</intent-filter>

</activity>

</application>

</manifest>

RESULT :
Thus, the Android application program was executed successfully and
verified.

CS8662 – MOBILE APPLICATION DEVELOPMENT 49


https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 50


https://www.ptrcet.org Department of Computer Science and Engineering

CS8662 – MOBILE APPLICATION DEVELOPMENT 51


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 8 IMPLEMENT AN APPLICATION THAT


CREATES AN ALERT UPON
RECEIVING A MESSAGE

AIM :
To write an Android application program that creates an alert upon
receiving a message.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new android project
3. Select and double click your project name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter your activity_main.xml code
6. Then, go to src folder and double click your MainActivity.java file
7. Enter your MainActivity.java code
8. Finally go to run configuration select your avd and run your android
program.

PROGRAM :
activity_main.xml
<RelativeLayout
xmlns: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=".MainActivity" >

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/text"
android:layout_marginTop="32dp"
android:text="SHOW ALERT BOX"
android:textSize="20sp" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"

CS8662 – MOBILE APPLICATION DEVELOPMENT 52


https://www.ptrcet.org Department of Computer Science and Engineering

android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="PTRCSE - CS8662"
android:textSize="30sp"
android:textStyle="bold" />

</RelativeLayout>

MainActivity.java
package com.example.eight;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {


private Button mainBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBtn = (Button) findViewById(R.id.button);
mainBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openAlert(v);
}
});
}
private void openAlert(View view) {
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle("PTRCSE - CS8662");
alertDialogBuilder.setMessage("Are you sure?");

// set positive button: Yes message


alertDialogBuilder.setPositiveButton("YES",new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// go to a new activity of the app
Toast.makeText(getApplicationContext(),
"WELCOME TO PTRCSE - Android APP..!",
CS8662 – MOBILE APPLICATION DEVELOPMENT 53
https://www.ptrcet.org Department of Computer Science and Engineering

Toast.LENGTH_LONG).show();
}
});

// set negative button: No message


alertDialogBuilder.setNegativeButton("NO",new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// cancel the alert box and put a Toast to the
user
dialog.cancel();
Toast.makeText(getApplicationContext(), "You
choose a Negative answer..!",
Toast.LENGTH_LONG).show();
}
});

// set neutral button: Exit the app message


alertDialogBuilder.setNeutralButton("Exit ( ).?",new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// exit the app and go to the HOME
MainActivity.this.finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show alert
alertDialog.show();
}
}

RESULT :
Thus, the Android application program was executed successfully and
verified.
CS8662 – MOBILE APPLICATION DEVELOPMENT 54
https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

If we click ‘SHOW ALERT BOX’, then it shows alert box as,

CS8662 – MOBILE APPLICATION DEVELOPMENT 55


https://www.ptrcet.org Department of Computer Science and Engineering

If we click ‘YES’, then it shows as,

If we click ‘NO’, then it shows as,

CS8662 – MOBILE APPLICATION DEVELOPMENT 56


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 9
DEVELOP A MOBILE APPLICATION
TO SEND AN e-MAIL

AIM :
To write an Android application program to send an e-Mail.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new android project
3. Select and double click your project name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter your activity_main.xml code
6. Then, go to src folder and double click your MainActivity.java file
7. Enter your MainActivity.java code
8. Finally go to run configuration select your avd and run your android
program.

PROGRAM :
activity_main.xml
<LinearLayout
xmlns: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:orientation="vertical"
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:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="E-mail">
<requestFocus />
</EditText>

<EditText

CS8662 – MOBILE APPLICATION DEVELOPMENT 57


https://www.ptrcet.org Department of Computer Science and Engineering

android:id="@+id/et_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="Subject"
android:layout_marginTop="10dp" />

<EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:inputType="textMultiLine"
android:ems="10"
android:hint="Message"
android:gravity="left" />

<Button
android:id="@+id/b_send"
android:layout_width="282dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="SEND"
android:textSize="20sp" />

</LinearLayout>

MainActivity.java
package com.example.nine;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;

public class MainActivity extends Activity {


EditText et_email,et_subject,et_message;
Button b_send;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CS8662 – MOBILE APPLICATION DEVELOPMENT 58
https://www.ptrcet.org Department of Computer Science and Engineering

et_email=(EditText)findViewById(R.id.et_email);
et_subject=(EditText)findViewById(R.id.et_subject);
et_message=(EditText)findViewById(R.id.et_message);

b_send=(Button)findViewById(R.id.b_send);
b_send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
String to=et_email.getText().toString();
String subject=et_subject.getText().toString();
String message=et_message.getText().toString();

Intent intent=new Intent(Intent.ACTION_SEND);


intent.putExtra(Intent.EXTRA_EMAIL,new
String[]{to});
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);

intent.setType("message/rfc822");

startActivity(Intent.createChooser(intent, "PTRCSE -
Select Email App"));

}
});

}
}

RESULT :
Thus, the Android application program was executed successfully and
verified.

CS8662 – MOBILE APPLICATION DEVELOPMENT 59


https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 60


https://www.ptrcet.org Department of Computer Science and Engineering

If we click on ‘SEND’, it navigates to Select an app to send an e-mail.


But in our AVD no app to perform email, so it shows as,

CS8662 – MOBILE APPLICATION DEVELOPMENT 61


https://www.ptrcet.org Department of Computer Science and Engineering

Ex No : 10
DEVELOP AN APPLICATION THAT MAKES
USE OF NOTIFICATION MANAGER

AIM :
To write an Android application program that makes use of Notification
Manager.
PROCEDURE :
1. Open eclipse or android studio
2. Create a new android project
3. Select and double click your project name
4. Then, go to res folder and select layout double click the
activity_main.xml file
5. Enter your activity_main.xml code
6. Then, go to src folder and double click your MainActivity.java file
7. Enter your MainActivity.java code
8. Finally go to run configuration select your avd and run your android
program.

PROGRAM :
activity_main.xml
<RelativeLayout
xmlns: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_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="PTRCSE - CS8662"
android:textStyle="bold"
android:textSize="30sp" />

CS8662 – MOBILE APPLICATION DEVELOPMENT 62


https://www.ptrcet.org Department of Computer Science and Engineering

<Button
android:id="@+id/bt_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:text="NOTIFICATION"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

activity_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns: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=".NotificationActivity" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/notifymessagetextview"
android:textSize="25sp"
android:gravity="center" />

</RelativeLayout>

MainActivity.java
package com.example.ten;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {


Button btNotification;

CS8662 – MOBILE APPLICATION DEVELOPMENT 63


https://www.ptrcet.org Department of Computer Science and Engineering

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btNotification=(Button)findViewById(R.id.bt_notification);

btNotification.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
String message="This is a PTRCSE - CS8662
Notification Demo";
NotificationCompat.Builder builder=new
NotificationCompat.Builder
(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("PTRCSE - New Notification")
.setContentText(message)
.setAutoCancel(true);

Intent intent=new
Intent(MainActivity.this,NotificationActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("message", message);

PendingIntent
pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager
notificationmanager=(NotificationManager)getSystemService(Context.NOTIFI
CATION_SERVICE);
notificationmanager.notify(0,builder.build());
}
});
}
}

NotificationActivity.java
package com.example.ten;

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

public class NotificationActivity extends Activity{


CS8662 – MOBILE APPLICATION DEVELOPMENT 64
https://www.ptrcet.org Department of Computer Science and Engineering

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

textview=(TextView)findViewById(R.id.notifymessagetextview);

String message=getIntent().getStringExtra("message");
textview.setText(message);
}
}

RESULT :
Thus, the Android application program was executed successfully and
verified.
CS8662 – MOBILE APPLICATION DEVELOPMENT 65
https://www.ptrcet.org Department of Computer Science and Engineering

OUTPUT :

CS8662 – MOBILE APPLICATION DEVELOPMENT 66

You might also like