0% found this document useful (0 votes)
7 views64 pages

Record

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 64

191CSC611L 310619104019

Index

Exp. No. Date Experiment Name Page No. Signature

Develop an application using GUI


1. components, layout manager and event 2–4
listener.

Develop an application to stimulate a


2. 5 – 10
keyboard.

Create an application that uses graphical


3. 11 – 13
primitive.

Develop an application that make use of


4. 14 – 20
database.

Implement an application that uses


5. 21 – 23
multithreading.

Develop an application that uses GPS


6. 24 – 32
location information.

Implement an application that writes data


7. 33 – 36
to SD card.

Implement an application that sends a


8. 37 – 40
SMS and create an alert.

9. Create an app that make use of menu. 41 – 44

Develop an application to build an alarm


10. 45 – 49
clock.

11. Implement a hybrid mobile application. 50 – 52

Develop a Mobile application for simple


12. 53 – 63
needs (Mini project)

1
191CSC611L 310619104019

Exp No: 1 Develop an application using GUI components, layout


manager and event listener.
Date:

AIM
To develop an application that uses GUI components, layout manager and event
listener.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno1” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 7: Click on app  java  com.example.exno1  MainActivity and type the java code.
Step 8: Run the project.
PROGRAM
activity_main.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"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:gravity="center"
android:text="0"
android:textSize="24sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

2
191CSC611L 310619104019

<Button
android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="105dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:text="Next" />

<Button
android:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Prev" />

</LinearLayout>
</LinearLayout>
</RelativeLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


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

TextView tv = (TextView) findViewById(R.id.textView);


Button add = (Button) findViewById(R.id.increase);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View view){
counter++;
tv.setText(Integer.toString(counter));
}
});
Button sub = (Button) findViewById(R.id.decrease);
sub.setOnClickListener(new View.OnClickListener() {

3
191CSC611L 310619104019

@Override
public void onClick (View view){
if(counter!=0) {
counter--;
tv.setText(Integer.toString(counter));
}
}
});
}
}
OUTPUT

RESULT:
Thus a simple android application that uses GUI components, layout manager and
event listener has been developed and executed successfully.

4
191CSC611L 310619104019

Exp No: 2 Develop an application to stimulate a keyboard.


Date:

AIM
To develop an application to stimulate a keyboard.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno2” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 7: Click on app  java  com.example.exno2  MainActivity and type the java code.
Step 8: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:gravity="center"
android:text=""
android:scrollbars="vertical"/>

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"

5
191CSC611L 310619104019

android:layout_marginLeft="75dp"
android:layout_marginTop="50dp">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />

<Button

6
191CSC611L 310619104019

android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
</TableRow>

</TableLayout>
</LinearLayout>
</RelativeLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

7
191CSC611L 310619104019

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("1");
}
});
Button two = (Button) findViewById(R.id.button2);
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("2");
}
});
Button three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("3");
}
});
Button four = (Button) findViewById(R.id.button4);
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("4");
}
});
Button five = (Button) findViewById(R.id.button5);
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("5");
}
});
Button six = (Button) findViewById(R.id.button6);
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("6");
}
});
Button enter = (Button) findViewById(R.id.button7);

8
191CSC611L 310619104019

enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("\n");
}
});
Button zero = (Button) findViewById(R.id.button8);
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.append("0");
}
});
Button del = (Button) findViewById(R.id.button9);
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(tv.getText().toString().length()!=0)
tv.setText(tv.getText().toString().substring(0,tv.getText().toString().length()-1));
}
});
}
}

9
191CSC611L 310619104019

OUTPUT

RESULT:
Thus an android application that stimulates a keyboard has been developed and
executed successfully.

10
191CSC611L 310619104019

Exp No: 3 Create an application that uses graphical primitive.


Date:

AIM
To develop an application that uses graphical primitive.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno3” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 7: Click on app  java  com.example.exno3  MainActivity and type the java code.
Step 8: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

11
191CSC611L 310619104019

setContentView(R.layout.activity_main);

Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);


ImageView iv =(ImageView)findViewById(R.id.imageView);
iv.setBackgroundDrawable(new BitmapDrawable(bg));
Canvas canvas= new Canvas(bg);
Paint paint = new Paint();

paint.setColor(Color.GREEN);
paint.setTextSize(50);

canvas.drawText("Rectangle", 420, 150, paint);


canvas.drawRect(350, 200, 650, 400, paint);

canvas.drawText("Circle", 100, 150, paint);


canvas.drawCircle(150, 300, 100, paint);

canvas.drawText("Square", 100, 500, paint);


canvas.drawRect(50, 520, 350, 820, paint);

canvas.drawText("Line", 480, 500, paint);


canvas.drawLine(520, 540, 520, 900, paint);

canvas.drawText("Triangle", 100, 910, paint);


Path path= new Path();
Point a = new Point(100, 950);
Point b = new Point(100, 1100);
Point c = new Point(400, 1100);
path.lineTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
canvas.drawPath(path, paint);

}
}

12
191CSC611L 310619104019

OUTPUT

RESULT:
Thus an android application that uses graphical primitive has been developed and
executed successfully.

13
191CSC611L 310619104019

Exp No: 4 Develop an application that make use of database.


Date:

AIM
To develop an application that make use of database.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno4” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 7: Click on app  java  com.example.exno4  MainActivity and type the java code.
Step 8: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Details"
android:textSize="30sp" />

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

<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:digits="a-z A-Z"
android:inputType="text"
android:textSize="20sp" />

14
191CSC611L 310619104019

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Register No:"
android:textSize="20sp" />

<EditText
android:id="@+id/RegisterNo"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="20sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter CGPA:"
android:textSize="20sp" />

<EditText
android:id="@+id/CGPA"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="20sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/Insert"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Insert"
android:textSize="30dp" />

<Button
android:id="@+id/Delete"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:text="Delete"
android:textSize="30dp" />

15
191CSC611L 310619104019

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/Update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:textSize="30dp" />

<Button
android:id="@+id/View"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:text="View"
android:textSize="30dp" />

</LinearLayout>

<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="View All"
android:textSize="30dp" />

</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.view.View;

public class MainActivity extends Activity implements OnClickListener {

16
191CSC611L 310619104019

EditText RegisterNo, Name, CGPA;


Button Insert, Delete, Update, View, ViewAll;
SQLiteDatabase db;

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

RegisterNo=(EditText)findViewById(R.id.RegisterNo);
Name=(EditText)findViewById(R.id.Name);
CGPA=(EditText)findViewById(R.id.CGPA);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);
ViewAll=(Button)findViewById(R.id.ViewAll);
Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);

db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);


db.execSQL("CREATE TABLE IF NOT EXISTS student(RegisterNo VARCHAR,
Name VARCHAR, CGPA Varchar);");

public void onClick(View view){


if(view==Insert){
if(RegisterNo.getText().toString().trim().length()==0||
Name.getText().toString().trim().length()==0||
CGPA.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student
VALUES('"+RegisterNo.getText()+"','"+Name.getText()+ "','"+CGPA.getText()+"');");
showMessage("Success", "Record added");
clearText();
}

if(view==Delete){
if(RegisterNo.getText().toString().trim().length()==0)

17
191CSC611L 310619104019

{
showMessage("Error", "Please enter RegisterNo");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
RegisterNo='"+RegisterNo.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE
RegisterNo='"+RegisterNo.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Register No");
}
clearText();
}

if(view==Update){
if(RegisterNo.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter RegisterNo");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
RegisterNo='"+RegisterNo.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE student SET name='" + Name.getText() + "',CGPA='" +
CGPA.getText() +
"' WHERE RegisterNo='"+RegisterNo.getText()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid RegisterNo");
}
clearText();
}

if(view==View){
if(RegisterNo.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter RegisterNo");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE

18
191CSC611L 310619104019

RegisterNo='"+RegisterNo.getText()+"'", null);
if(c.moveToFirst())
{
Name.setText(c.getString(1));
CGPA.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid RegisterNo");
clearText();
}
}

if(view==ViewAll){
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("RegisterNo: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("CGPA: "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}

private void clearText() {


RegisterNo.setText("");
Name.setText("");
CGPA.setText("");
RegisterNo.requestFocus();
}

private void showMessage(String error, String please_enter_all_values) {


AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(error);
builder.setMessage(please_enter_all_values);
builder.show();

19
191CSC611L 310619104019

}
}
OUTPUT

RESULT:
Thus an android application that uses database has been developed and executed
successfully.

20
191CSC611L 310619104019

Exp No: 5 Implement an application that uses multithreading.


Date:

AIM
To develop an application that uses multithreading.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno5” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 7: Click on app  java  com.example.exno5  MainActivity and type the java code.
Step 8: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Load Thread" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ABC" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ABC" />

21
191CSC611L 310619104019

</LinearLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button btn1, btn2, btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn1 = (Button)findViewById(R.id.button);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button)findViewById(R.id.button3);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
btn2.setText("DEF");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
btn3.setText("DEF");
}
}).start();
}
});

}
}

22
191CSC611L 310619104019

OUTPUT

RESULT:
Thus an android application that uses multithreading has been developed and
executed successfully.

23
191CSC611L 310619104019

Exp No: 6 Develop an application that uses GPS location


information.
Date:

AIM
To develop an application that uses GPS location information.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno6” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  manifests  AndroidManifest.xml and give location and internet
permission.
Step 7: Click on app  res  layout  activity_main.xml and design the layout.
Step 8: Click on app  java  com.example.exno6  MainActivity and type the java code.
Step 9: Click on app  java  com.example.exno6  GpsTracker and type the java code.
Step 10: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Latitude"
android:textSize="18sp"
android:textStyle="bold"/>

<TextView
android:id="@+id/latitude"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="-"
android:textSize="20sp"
android:textStyle="bold"/>

24
191CSC611L 310619104019

<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Longitude"
android:textSize="18sp"
android:textStyle="bold"/>

<TextView
android:id="@+id/longitude"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="-"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="getLocation"
android:text="GET LOCATION" />

</LinearLayout>
MainActivity.java
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

private GpsTracker gpsTracker;


private TextView tvLatitude,tvLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvLatitude = (TextView)findViewById(R.id.latitude);
tvLongitude = (TextView)findViewById(R.id.longitude);

25
191CSC611L 310619104019

try {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this, new
String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 101);
}
} catch (Exception e){
e.printStackTrace();
}
}

public void getLocation(View view){


gpsTracker = new GpsTracker(MainActivity.this);
if(gpsTracker.canGetLocation()){
double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
tvLatitude.setText(String.valueOf(latitude));
tvLongitude.setText(String.valueOf(longitude));
}else{
gpsTracker.showSettingsAlert();
}
}
}
GpsTracker.java
import android.Manifest;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;

class GpsTracker extends Service implements LocationListener {


private final Context mContext;

26
191CSC611L 310619104019

// flag for GPS status


boolean isGPSEnabled = false;

// flag for network status


boolean isNetworkEnabled = false;

// flag for GPS status


boolean canGetLocation = false;

Location location; // location


double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters


private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds


private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager


protected LocationManager locationManager;

public GpsTracker(Context context) {


this.mContext = context;
getLocation();
}

public Location getLocation() {


try {
locationManager = (LocationManager)
mContext.getSystemService(LOCATION_SERVICE);

// getting GPS status


isGPSEnabled =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status


isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {


// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider

27
191CSC611L 310619104019

if (isNetworkEnabled) {
//check the network permission
if (ActivityCompat.checkSelfPermission(mContext,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(mContext,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new
String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

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

// if GPS Enabled get lat/long using GPS Services


if (isGPSEnabled) {
if (location == null) {
//check the network permission
if (ActivityCompat.checkSelfPermission(mContext,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(mContext,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new
String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,

28
191CSC611L 310619104019

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d("GPS Enabled", "GPS Enabled");


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

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

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

return location;
}

/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */

public void stopUsingGPS(){


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

/**
* Function to get latitude
* */

public double getLatitude(){


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

// return latitude
return latitude;
}

29
191CSC611L 310619104019

/**
* Function to get longitude
* */

public double getLongitude(){


if(location != null){
longitude = location.getLongitude();
}

// return longitude
return longitude;
}

/**
* Function to check GPS/wifi enabled
* @return boolean
* */

public boolean canGetLocation() {


return this.canGetLocation;
}

/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */

public void showSettingsAlert(){


AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

// Setting Dialog Title


alertDialog.setTitle("GPS is settings");

// Setting Dialog Message


alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

// On pressing Settings button


alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});

// on pressing cancel button

30
191CSC611L 310619104019

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {


public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}
}

31
191CSC611L 310619104019

OUTPUT

RESULT:
Thus an android application that uses GPS location information has been developed
and executed successfully.

32
191CSC611L 310619104019

Exp No: 7 Implement an application that writes data to SD card.


Date:

AIM
To develop an application that writes data to SD card.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno7” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  manifests  AndroidManifest.xml and give SD card permission.
Step 7: Click on app  res  layout  activity_main.xml and design the layout.
Step 8: Click on app  java  com.example.exno7  MainActivity and type the java code.
Step 9: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Write Data" />
<Button
android:id="@+id/read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read Data" />
<Button

33
191CSC611L 310619104019

android:id="@+id/clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear Data" />

</LinearLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.*;
import java.io.*;

public class MainActivity extends AppCompatActivity {

EditText edit;
Button write, read, clear;

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

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


write = (Button) findViewById(R.id.write);
read = (Button) findViewById(R.id.read);
clear = (Button) findViewById(R.id.clear);

write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = edit.getText().toString();
try {
File folder = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.KITKAT) {
folder =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS
);
}
File f = new File(folder, "myfile.txt");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos);

34
191CSC611L 310619104019

osw.append(message);
osw.close();
fos.close();
Toast.makeText(getBaseContext(), "Data written to SD card",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});

read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message;
String buf = "";
try {
File folder = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.KITKAT) {
folder =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS
);
}
File f = new File(folder, "myfile.txt");
FileInputStream fin = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while((message = br.readLine()) != null){
buf += message;
}
edit.setText(buf);
br.close();
fin.close();
Toast.makeText(getBaseContext(), "Data read from SD card",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});

clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

35
191CSC611L 310619104019

edit.setText("");
}
});
}
}
OUTPUT

RESULT:
Thus an android application that writes data to SD card has been developed and
executed successfully.
36
191CSC611L 310619104019

Exp No: 8 Implement an application that sends a SMS and create an


alert.
Date:

AIM
To develop an application that sends a SMS and create an alert.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno8” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 7: Click on app  java  com.example.exno8  MainActivity and type the java code.
Step 8: Click on File  New  Activity  Empty Activity to create a new activity.
Step 9: Click on app  res  layout  activity_second.xml and design the layout.
Step 10: Click on app  java  com.example.exno8  SecondActivity and type the java
code.
Step 11: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />

<Button
android:id="@+id/button"

37
191CSC611L 310619104019

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>

</LinearLayout>
MainActivity.java
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


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

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


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

notify.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification noti = new
Notification.Builder(MainActivity.this).setContentTitle("New
Message").setContentText(e.getText().toString()).setSmallIcon(R.mipmap.ic_launcher).setC
ontentIntent(pending).build();
NotificationManager manager = (NotificationManager)

38
191CSC611L 310619104019

getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, noti);
}
});
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".SecondActivity">

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Notification"/>

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

</LinearLayout>
SecondActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

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

39
191CSC611L 310619104019

OUTPUT

RESULT:
Thus an android application that sends a SMS and creates an alert has been developed
and executed successfully.

40
191CSC611L 310619104019

Exp No: 9 Create an app that make use of menu.


Date:

AIM
To develop an application that make use of menu.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno1” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 7: Click on app  res and create a new folder “menu” and two files inside the menu
folder named “menu_example.xml” and “options_menu.xml”.
Step 8: Click on app  java  com.example.exno9  MainActivity and type the java code.
Step 9: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
menu_example.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

41
191CSC611L 310619104019

<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
<item android:id="@+id/upload"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
app:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/search_item"
android:title="Search" />
<item android:id="@+id/upload_item"
android:title="Upload" />
<item android:id="@+id/copy_item"
android:title="Copy" />
<item android:id="@+id/print_item"
android:title="Print" />
<item android:id="@+id/share_item"
android:title="Share" />
<item android:id="@+id/bookmark_item"
android:title="BookMark" />
</menu>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;

42
191CSC611L 310619104019

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(),
Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

43
191CSC611L 310619104019

OUTPUT

RESULT:
Thus an android application that make use of menu has been developed and executed
successfully.

44
191CSC611L 310619104019

Exp No: 10 Develop an application to build an alarm clock.


Date:

AIM
To develop an application to build an alarm clock.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno10” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  manifests  AndroidManifest.xml and give vibrate and wake lock
permission.
Step 7: Click on app  res  layout  activity_main.xml and design the layout.
Step 8: Click on app  java  com.example.exno10  MainActivity and type the java code.
Step 9: Click on app  java  com.example.exno10  AlarmReceiver and type the java
code.
Step 10: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">

<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:checked="false"
android:onClick="OnToggleClicked" />

45
191CSC611L 310619104019

</LinearLayout>
MainActivity.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;

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

alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);


alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

// OnToggleClicked() method is implemented the time functionality


public void OnToggleClicked(View view) {
long time;
if (((ToggleButton) view).isChecked()) {
Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();

// calendar is called to get current time in hour and minute


calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());

// using intent i have class AlarmReceiver class which inherits


// BroadcastReceiver
Intent intent = new Intent(this, AlarmReceiver.class);

46
191CSC611L 310619104019

// we call broadcast using pendingIntent


pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

time = (calendar.getTimeInMillis() - (calendar.getTimeInMillis() % 60000));


if (System.currentTimeMillis() > time) {
// setting time as AM and PM
if (calendar.AM_PM == 0)
time = time + (1000 * 60 * 60 * 12);
else
time = time + (1000 * 60 * 60 * 24);
}
// Alarm rings continuously until toggle button is turned off
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000,
pendingIntent);
// alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
(time * 1000), pendingIntent);
} else {
alarmManager.cancel(pendingIntent);
Toast.makeText(MainActivity.this, "ALARM OFF",
Toast.LENGTH_SHORT).show();
}
}
}
AlarmReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

public class AlarmReceiver extends BroadcastReceiver {


@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
// implement onReceive() method
public void onReceive(Context context, Intent intent) {

// we will use vibrator first


Vibrator vibrator = (Vibrator)

47
191CSC611L 310619104019

context.getSystemService(context.VIBRATOR_SERVICE);
vibrator.vibrate(4000);

Toast.makeText(context, "Alarm! Wake up! Wake up!",


Toast.LENGTH_LONG).show();
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}

// setting default ringtone


Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);

// play ringtone
ringtone.play();
}
}

48
191CSC611L 310619104019

OUTPUT

RESULT:
Thus an android application to build an alarm clock has been developed and executed
successfully.

49
191CSC611L 310619104019

Exp No: 11 Implement a hybrid mobile application.


Date:

AIM
To develop a hybrid mobile application.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “exno11” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Click on app  res  layout  activity_main.xml and design the layout.
Step 8: Click on app  java  com.example.exno11  MainActivity and type the java
code.
Step 9: Run the project.
PROGRAM
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">

<EditText
android:id="@+id/website_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/go"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go"/>

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

50
191CSC611L 310619104019

MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

Button go;
EditText site;
WebView myWebView;

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

site = (EditText)findViewById(R.id.website_name);
go = (Button)findViewById(R.id.go);

myWebView = (WebView) findViewById(R.id.webview);


myWebView.setWebViewClient(new MyBrowser());

go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = site.getText().toString();
myWebView.loadUrl(url);
}
});
}

private class MyBrowser extends WebViewClient {


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

51
191CSC611L 310619104019

OUTPUT

RESULT:
Thus a hybrid mobile application has been developed and executed successfully.

52
191CSC611L 310619104019

Exp No: 12 Develop a Mobile application for simple needs (Mini


project)
Date:

AIM
To develop an application for Prescription Viewer.
ALGORITHM
Step 1: Open Android Studio and then click on File  New  New Project.
Step 2: Type the Application name as “PrescriptionViewer” and click Next.
Step 3: Select Empty Activity and click Next.
Step 4: Click Finish.
Step 5: It will build and load the project.
Step 6: Get patient’s prescription details from the doctor.
Step 7: Store each patient’s data as a pdf in SD card.
Step 8: Get the data to store it in history from the database.
Step 9: Run the project.
PROGRAM
activity_main.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"
android:layout_marginTop="25sp"
tools:context=".MainActivity">

<EditText
android:id="@+id/name"
android:backgroundTint="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:hint="Name"
android:lines="1"/>

<EditText
android:id="@+id/address"
android:backgroundTint="@color/colorPrimary"
android:lines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_margin="15dp"

53
191CSC611L 310619104019

android:hint="Address" />

<EditText
android:id="@+id/symptoms"
android:backgroundTint="@color/colorPrimary"
android:lines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/address"
android:layout_margin="15dp"
android:hint="Symptoms" />

<EditText
android:id="@+id/prescription"
android:backgroundTint="@color/colorPrimary"
android:lines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/symptoms"
android:layout_margin="15dp"
android:hint="Prescription" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/prescription"
android:layout_marginTop="15dp"
android:gravity="center">

<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/prescription"
android:background="@color/colorPrimary"
android:text="SAVE "
android:textColor="#ffffff" />

<Button
android:id="@+id/btn_print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_save"
android:layout_marginLeft="15dp"
android:background="@color/colorPrimary"
android:text="History"

54
191CSC611L 310619104019

android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
activity_retrieve_previous_prescription.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RetrievePreviousPrescription">

<ir.androidexception.datatable.DataTable
android:id="@+id/data_table"
android:layout_width="403dp"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:corner_radius="8dp"
app:direction="ltr"
app:header_background_color="#fff"
app:header_gravity="center"
app:header_horizontal_padding="0dp"
app:header_text_color="#000"
app:header_text_size="4sp"
app:header_vertical_padding="16dp"
app:persian_number="false"
app:row_background_color="#fff"
app:row_gravity="center"
app:row_text_color="#000"
app:row_text_size="4sp"
app:row_vertical_padding="16dp"
app:shadow="8dp" />

</RelativeLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Canvas;

55
191CSC611L 310619104019

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {


Button save, print;
EditText name,address,news,prescription,symptoms;

DatabaseClass databaseClass;
SQLiteDatabase sqLiteDatabase;
Date date = new Date();

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");


SimpleDateFormat timepatternFormat = new SimpleDateFormat("hh:mm a");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save = findViewById(R.id.btn_save);
print = findViewById(R.id.btn_print);
name = findViewById(R.id.name);
news = findViewById(R.id.on);
prescription = findViewById(R.id.prescription);
symptoms = findViewById(R.id.symptoms);
address = findViewById(R.id.address);

databaseClass = new DatabaseClass(this);


sqLiteDatabase = databaseClass.getWritableDatabase();

save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = String.valueOf(name.getText());
String userAddress = String.valueOf(address.getText());
String userSymptoms = String.valueOf(symptoms.getText());

56
191CSC611L 310619104019

String userPrescription = String.valueOf(prescription.getText());

databaseClass.insert(userName,userAddress,date.getTime(),userSymptoms,userPrescription);

Toast.makeText(getApplicationContext(),"Saved in Storage",
Toast.LENGTH_SHORT).show();

printInvoice();
name.setText("");
address.setText("");
symptoms.setText("");
prescription.setText("");

}
});
print.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,RetrievePreviousPrescription.class);
startActivity(intent);
}
});

private void printInvoice() {


PdfDocument pdfDocument = new PdfDocument();
Paint paint = new Paint();
String[] columns = {"invoiceNo","Name","address","date","symptoms","prescription"};
Cursor cursor = sqLiteDatabase.query("myTable",columns,null,null,null,null,null);
cursor.move(cursor.getCount());

PdfDocument.PageInfo pageInfo = new


PdfDocument.PageInfo.Builder(1000,900,1).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
Canvas canvas = page.getCanvas();

paint.setTextSize(80);
canvas.drawText("Hospital Name",250,80,paint);

paint.setTextSize(30);
canvas.drawText("Dr. XYZ", 30, 150,paint);

paint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText("Invoice No: ",canvas.getWidth()-70,150,paint);
canvas.drawText(String.valueOf(cursor.getInt(0)),canvas.getWidth()-40,150,paint);

57
191CSC611L 310619104019

paint.setTextAlign(Paint.Align.LEFT);

paint.setColor(Color.BLACK);
canvas.drawText("Date:",30,200, paint);
canvas.drawText(simpleDateFormat.format(cursor.getLong(3)),120,200,paint);

canvas.drawText("Time:",750,200,paint);
paint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText(timepatternFormat.format(cursor.getLong(3)),canvas.getWidth()-
40,200,paint);
paint.setTextAlign(Paint.Align.LEFT);

paint.setColor(Color.BLACK);
canvas.drawText("Patient Name:",30,350,paint);

canvas.drawText("Address:",620,350,paint);
paint.setTextAlign(Paint.Align.RIGHT);
paint.setTextAlign(Paint.Align.LEFT);

paint.setColor(Color.BLACK);
canvas.drawText(cursor.getString(1),30,380,paint);
canvas.drawText(cursor.getString(2),620,380,paint);
paint.setTextAlign(Paint.Align.RIGHT);

paint.setTextAlign(Paint.Align.LEFT);
paint.setColor(Color.BLACK);
canvas.drawText("Symptoms:",30,450,paint);
canvas.drawText(cursor.getString(4),30,480,paint);

canvas.drawText("Prescription:",620,450,paint);
canvas.drawText(cursor.getString(5),620,480,paint);

paint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText("STAY HEALTHY",900,800,paint);
pdfDocument.finishPage(page);

File file = new File(this.getExternalFilesDir("/"),cursor.getString(1)+"-


prescription.pdf");

try {
pdfDocument.writeTo(new FileOutputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
pdfDocument.close();

58
191CSC611L 310619104019

}
}
DatabaseClass.java
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

public class DatabaseClass extends SQLiteOpenHelper {


public DatabaseClass(@Nullable Context context) {
super(context, "MyDatabase", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

String createTable = "create table myTable(invoiceNo INTEGER PRIMARY KEY


AUTOINCREMENT, Name TEXT, address STRING, date INTEGER, symptoms STRING,
prescription SRTING);";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

public void insert(String name, String address, Long date, String symptoms, String
prescription) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put("Name", name);
contentValues.put("address", address);
contentValues.put("date", date);
contentValues.put("symptoms",symptoms);
contentValues.put("prescription",prescription);
sqLiteDatabase.insert("myTable",null,contentValues);

}
}

59
191CSC611L 310619104019

RetrievePreviousPrescription.java
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import ir.androidexception.datatable.DataTable;
import ir.androidexception.datatable.model.DataTableHeader;
import ir.androidexception.datatable.model.DataTableRow;

public class RetrievePreviousPrescription extends AppCompatActivity {

DataTable dataTable;
DatabaseClass databaseClass;
SQLiteDatabase sqLiteDatabasel;

Date date = new Date();


SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat timepatternFormat = new SimpleDateFormat("hh:mm a");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_previous_prescription);
dataTable = findViewById(R.id.data_table);
databaseClass = new DatabaseClass(this);
sqLiteDatabasel = databaseClass.getWritableDatabase();
DataTableHeader dataTableHeader = new DataTableHeader.Builder()
.item("Invoice No.", 5)
.item("Patient Name", 5)
.item("Date", 5)
.item("Time", 5)
.build();

ArrayList<DataTableRow> rows = new ArrayList<>();


String[] columns = {"invoiceNo", "Name", "address", "date"};
Cursor cursor = sqLiteDatabasel.query("myTable", columns, null, null, null, null, null);
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToNext();
DataTableRow row = new DataTableRow.Builder()
.value(String.valueOf(cursor.getInt(0)))
.value(cursor.getString(1))
.value(simpleDateFormat.format(cursor.getLong(3)))
.value(timepatternFormat.format(cursor.getLong(3)))

60
191CSC611L 310619104019

.build();
rows.add(row);
}
dataTable.setHeader(dataTableHeader);
dataTable.setRows(rows);
dataTable.inflate(this);
}
}
OUTPUT

61
191CSC611L 310619104019

RESULT:
Thus an android application for Prescription Viewer has been developed and executed
successfully.

62

You might also like