Mad Lab Manual
Mad Lab Manual
AND ENGINEERING
Lab Manual
Regulation 2017
TABLE OF CONTENTS
Page
S. No Particulars
No
1 College Vision and Mission Statement 3
2 Department Vision, Mission, POs, PEOs, PSOs 4
3 Lab Syllabus 5
4 List of Experiments Mapping with COs, POs &PSOs 6
5 University Prescribed Lab Experiments
1. GUI Components, Fonts and Colors 8
2. Layout Managers and Event Listeners 13
3. Graphical Primitives 22
4. Database Application 24
5. Notification Manager 35
6. Multithreading 42
7. GPS Location Information 46
8. Interfacing SD Card 56
9. Alert Message 63
10. RSS Feed 67
11. Email 73
12. Mini Project – Simple Calculator 77
6 Content Beyond Syllabus
13. Alarm Clock 83
14. Send SMS 88
2
NPR COLLEGE OF ENGINEERING & TECHNOLOGY,
NATHAM
VISION
MISSION
3
NPR College of Engineering & Technology, Natham.
VISION
• To produce globally competent technical professionals for digitized society.
MISSION
• To establish conducive academic environment by imparting quality education
and value added training.
• To encourage students to develop innovative projects to optimally resolve the
challenging social problems.
Course Outcomes
After completion of the course, Students are able to learn the listed
Course Outcomes.
Knowledge
COs Course Code Course Outcomes
Level
CO1 C317.1 Illustrate mobile applications using GUI and Layouts. K3
CO2 C317.2 Demonstrate mobile applications using Event Listener. K3
CO3 C317.3 Experiment with mobile applications using Databases. K3
Make use of mobile applications using RSS Feed, Internal/
CO4 C317.4 K3
External Storage, SMS, Multithreading and GPS.
CO5 C317.5 Build their own mobile app for simple needs. K3
Model various mobile applications using different application
CO6 C317.6 K3
development frameworks.
Exp.
Name of the Experiment COs POs PSOs
No.
1 GUI Components, Fonts and Colors CO1 1,2,3 & 12 1,2
2 Layout Managers and Event Listeners CO1, CO2 1,2,3 & 12 1,2
3 Graphical Primitives CO1, CO2, CO3 1,2,3 & 12 1,2
4 Database Application CO2, CO3, CO4 1,2,3,5,6 &7 1,2
5 Notification Manager CO3, CO4 1,2,3,5,6 &7 1,2
6 Multithreading CO3, CO4 1,2,3,5,6 &7 1,2
7 GPS Location Information CO2, CO3, CO5 1,2,3,5 & 6 1,2
8 Interfacing SD Card CO4, CO5 1,2,3,5 & 6 1,2
9 Alert Message CO4, CO5 1,2,3,5,6 &7 1,2
10 RSS Feed CO4, CO5 1,2,3,5,10 & 12 1,2
11 Email CO4, CO5 1,2,3,5,10 & 12 1,2
12 Mini Project – Simple Calculator CO5 1,2,3,5,10 & 12 1,2
Content Beyond Syllabus
13 Alarm Clock CO5 1,2,3,5,10 & 12 1,2
6
Program Outcomes
7
Ex. No: 01 GUI components, Font and Colors
Date:
Aim:
To develop a Simple Android Application that uses GUI components, Font and Colors.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
8
Designing layout for the Android Application:
• Click on app -> res -> layout -> activity_main.xml.
• Now click on Text as shown below.
• Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
9
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>
• Now click on Design and your application will look as given below.
11
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
Output:
Result:
Thus a Simple Android Application that uses GUI components, Font and Colors is developed and
executed successfully.
12
Ex. No. 02 Layout Managers and Event Listeners
Date:
Aim:
To develop a Simple Android Application that uses Layout Managers and Event Listeners.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
13
• Type the Activity Name as SecondActivity and click Finish button.
• Thus Second Activity For the application is created.
Designing Layout for Main Activity:
• Click on app -> res -> layout -> activity_main.xml.
• Now click on Text as shown below.
• Then delete the code which is there and type the code as given below.
Code for 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Details Form"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
14
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="200dp"
android:columnCount="2"
android:rowCount="3">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="0"
android:text="Name"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="1"
android:ems="10"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="0"
android:text="Reg.No"
android:textSize="20sp"
android:gravity="center"/>
15
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="1"
android:inputType="number"
android:ems="10"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="0"
android:text="Dept"
android:textSize="20sp"
android:gravity="center"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="1"
android:spinnerMode="dropdown"/>
</GridLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="150dp"
android:text="Submit"/>
16
</RelativeLayout>
Designing Layout for Second Activity:
• Click on app -> res -> layout -> activity_second.xml.
• Now click on Text as shown below.
• Then delete the code which is there and type the code as given below.
Code for Activity_second.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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="com.example.devang.exno2.SecondActivity"
android:orientation="vertical"
android:gravity="center">
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
17
• Now click on Design and your activity will look as given below.
• So now the designing part of Second Activity is also completed.
Java Coding for the Android Application:
• Java Coidng for Main Activity:
• Click on app -> java -> com.example.exno2 -> MainActivity.
• Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
package com.example.exno2;
import android.content.Intent;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
18
E2= (EditText) findViewById(R.id.editText2);
s= (Spinner) findViewById(R.id.spinner);
//Creating Adapter for Spinner for adapting the data from array to Spinner
ArrayAdapter adapter= new
ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,dept_array);
s.setAdapter(adapter);
startActivity(i);
}
});
}
}
Java Coding for Second Activity:
• Click on app -> java -> com.example.exno2 -> SecondActivity.
• Then delete the code which is there and type the code as given below.
19
Code for SecondActivity.java:
package com.example.exno2;
import android.content.Intent;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
20
Output:
Result:
Thus a Simple Android Application that uses Layout Managers and Event Listeners is developed and
executed successfully.
21
Ex No. 03 Graphical Primitives
Date:
Aim:
To develop a Simple Android Application that draws basic Graphical Primitives on the screen.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno3″ and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
Designing layout for the Android Application:
• Click on app -> res -> layout -> activity_main.xml.
• Now click on Text as shown below.
• Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
22
Code for MainActivity.java:
package com.example.exno3;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
//Creating a Bitmap
Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);
//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);
23
canvas.drawText("Circle", 120, 150, paint);
canvas.drawCirclE(200, 350, 150, paint);
//To draw a Square
canvas.drawText("Square", 120, 800, paint);
canvas.drawRect(50, 850, 350, 1150, paint);
Output:
Result:
Thus a Simple Android Application that draws basic Graphical Primitives on the screen is developed
and executed successfully.
24
Ex. No. 04 Database Application
Date:
Aim:
To develop a Simple Android Application that makes use of Database.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno4″ and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
Designing layout for the Android Application:
• Click on app -> res -> layout -> activity_main.xml.
• Now click on Text as shown below.
• Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />
25
<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" />
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
26
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" />
<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" />
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
27
android:text="View"
android:textSize="30dp" />
<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />
</AbsoluteLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
Java Coding for the Android Application:
• Click on app -> java -> com.example.exno4 -> MainActivity.
• Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
packagecom.example.exno4;
import android.app.Activity;
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;
Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);
ViewAll=(Button)findViewById(R.id.ViewAll);
Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);
30
if(view==View)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
Name.setText(c.getString(1));
Marks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Displaying all the records
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("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
31
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}
Output:
32
33
Result:
Thus a Simple Android Application that makes use of Database is developed and executed
successfully.
34
Ex. No. 05 Notification Manager
Date:
Aim:
To develop an Android Application that makes use of Notification Manager.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno5″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<Button
android:id="@+id/btnSimpleNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Notification" />
<Button
android:id="@+id/btnNotificationIcon"
android:layout_width="wrap_content"
35
android:layout_height="wrap_content"
android:text="Notification With Icon" />
<Button
android:id="@+id/btnNotificationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification With Image" />
<Button
android:id="@+id/btnNotificationWithGroupConvo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification With Group Conversation" />
<Button
android:id="@+id/btnNotificationSemantic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Semantic Action" />
</LinearLayout>
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.core.app.NotificationCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.Person;
import androidx.core.graphics.drawable.IconCompat;
import android.os.Bundle;
36
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
NotificationManager notificationManager;
NotificationCompat.Builder builder;
NotificationChannel channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
charSequence = btnNotificationIcon.getText();
btnSimpleNotification.setOnClickListener(this);
btnNotificationIcon.setOnClickListener(this);
btnNotificationImage.setOnClickListener(this);
btnNotificationWithGroupConvo.setOnClickListener(this);
btnNotificationSemantic.setOnClickListener(this);
37
notificationManager.createNotificationChannel(channel);
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSimpleNotification:
simpleNotification();
break;
case R.id.btnNotificationIcon:
notificationWithIcon();
break;
case R.id.btnNotificationImage:
notificationWithImage();
break;
case R.id.btnNotificationWithGroupConvo:
notificationWithGroupConvo();
break;
case R.id.btnNotificationSemantic:
notificationSemantic();
break;
}
}
private void simpleNotification() {
Person jd = new Person.Builder().setName("JournalDev ") .setImportant(true) .build();
new NotificationCompat.MessagingStyle(jd)
.addMessage("Check me out", new Date().getTime(), jd) .setBuilder(builder);
notificationManager.notiFY(1, builder.build());
}
private void notificationWithIcon() {
Person anupam = new Person.Builder()
.setName("Anupam")
.setIcon(IconCompat.createWithResource(this, R.drawable.index))
.setImportant(true) .build();
new NotificationCompat.MessagingStyle(anupam)
38
.addMessage("Check out my latest article!", new Date().getTime(), anupam)
.setBuilder(builder);
notificationManager.notiFY(2, builder.build());
}
private void notificationWithImage() {
Person bot = new Person.Builder()
.setName("Bot") .setImportant(true)
.setBot(true) .build();
new NotificationCompat.MessagingStyle(bot)
.addMessage(message) .setGroupConversation(true).setBuilder(builder);
notificationManager.notify(3, builder.build());
}
private void notificationWithGroupConvo()
{
Person jd = new Person.Builder()
.setName("JournalDev") .build();
notificationManager.notify(4, builder.build());
}
private void notificationSemantic()
{
Person jd = new Person.Builder()
.setName("JournalDev")
.build();
Result:
Thus Android Application that makes use of notification manager is developed and executed
successfully.
41
Ex. No. 06 Multithreading
Date:
Aim:
To develop an Android Application that implements Multi threading.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno6″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_margin="50dp"
android:layout_gravity="center" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
42
android:text="Load Image 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load image 2" />
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
bt1 = (Button)findViewById(R.id.button);
bt2= (Button) findViewById(R.id.button2);
img = (ImageView)findViewById(R.id.imageView);
43
bt1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
img.setImageResource(R.drawable.india1);
}
});
}
}).start();
}
});
bt2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
img.setImageResource(R.drawable.india2);
}
});
}
44
}).start();
}
});
}
}
Output:
Result:
Thus Android Application that implements Multi threading is developed and executed successfully.
45
Ex. No. 07 GPS Location Information
Date:
Aim:
To develop an Android Application that uses GPS location information.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno7″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<Button
android:id = "@+id/button"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "getlocation"/>
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
46
Following will be the content of res/values/strings.xml to define two new constants −
<?xml version = "1.0" encoding = "utf-8"?>
<resources>
<string name = "app_name">Tutorialspoint</string>
</resources>
</manifest>
47
Code for MainActivity.java:
packagecom.example.exno7;
import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.test.mock.MockPackageManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Button btnShowLocation;
private static final int REQUEST_CODE_PERMISSION = 2;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if (ActivityCompat.checkSelfPermission(this, mPermission)
!= MockPackageManager.PERMISSION_GRANTED) {
48
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(MainActivity.this);
}
});
}
}
• Following is the content of the modified main activity file GPSTracker.java.
Code for GPDTracker.Java
packagecom.example.exno7;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
49
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener {
50
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
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 (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
51
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
/**
* Function to get latitude
* */
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
@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;
}
}
• So now the Coding part is also completed.
• Now run the application to see the output.
54
Output:
Result:
Thus Android Application that implements GPS Location Information is developed and executed
successfully.
55
Ex. No. 08 Interfacing SD Card
Date:
Aim:
To develop an Android Application that writes data to the SD Card.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno8″ and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
56
android:layout_margin="10dp"
android:text="Write Data"
android:textSize="30dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Read data"
android:textSize="30dp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Clear"
android:textSize="30dp" />
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
57
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import androidx.appcompat.app.AppCompatActivity;
58
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
write.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String message=E1.getText().toString();
try
{
File f=new File("/sdcard/myfile.txt");
f.createNewFile();
FileOutputStream fout=new FileOutputStream(f);
fout.write(message.getBytes());
fout.close();
Toast.makeText(getBaseContext(),"Data Written in SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
59
read.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String message;
String buf = "";
try
{
File f = new File("/sdcard/myfile.txt");
FileInputStream fin = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while ((message = br.readLine()) != null)
{
buf += message;
}
E1.setText(buf);
br.close();
fin.close();
Toast.makeText(getBaseContext(),"Data Recived from SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
60
clear.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
E1.setText("");
}
});
}
}
61
Output:
Result:
Thus Android Application that writes data to the SD Card is developed and executed successfully.
62
Ex. No. 09 Alert Message
Date:
Aim:
To develop an Android Application that creates an alert upon receiving a message.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “ex.no9″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />
63
<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"
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>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
imp0rt androidx.appcompat.app.AppCompatActivity;
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).setContentIntent(pe
nding).build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, noti);
}
});
}
}
65
Output:
Result:
Thus Android Application that creates an alert upon receiving a message is developed and executed
successfully.
66
Ex. No. 10 RSS Feed
Date:
Aim:
To develop an Android Application that makes use of RSS Feed.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno10″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
• So now the Permissions are added in the Manifest.
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
68
import android.widget.ListView;
import org.xmlpull.V1.XmlPullParser;
import org.xmlpull.V1.XmlPullParserException;
import org.xmlpull.V1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
69
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}
else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
70
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Uri uri = Uri.parse((links.get(position)).toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
public InputStream getInputStream(URL url)
{
try
{
return url.openConnection().getInputStream();
}
catch (IOException e)
{
return null;
}
}
}
• So now the Coding part is also completed.
• Now run the application to see the output.
71
Output:
Result:
Thus Android Application that makes use of RSS Feed is developed and executed successfully.
72
Ex. No. 11 Email
Date:
Aim:
To develop an Android Application to send an Email.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno11″ and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<activity
android:name="com.example.exno11.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
74
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
import android.content.Intent;
//import android.support.v7.app.AppCompatActivity;
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 {
Output:
Result:
Thus Android Application for sending an email is developed and executed successfully.
76
Ex. No. 12 Mini Project – Alarm Clock
Date:
Aim:
To develop a Simple Android Application for Native Calculator.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno12″and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select the Empty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
77
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>
<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>
<Button
android:id="@+id/Mul"
android:layout_width="match_parent"
78
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="30sp"/>
<Button
android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="30sp"/>
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
79
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements OnClickListener
{
//Defining the Views
EditText Num1;
EditText Num2;
Button Add;
Button Sub;
Button Mul;
Button Div;
TextView Result;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}
@Override
public void onClick (View v)
80
{
float num1 = 0;
float num2 = 0;
float result = 0;
String oper = "";
// check if the fields are empty
if (TextUtils.isEmpty(Num1.getText().toString()) || TextUtils.isEmpty(Num2.getText().toString()))
return;
// read EditText and fill variables with numbers
num1 = Float.parseFloat(Num1.getText().toString());
num2 = Float.parseFloat(Num2.getText().toString());
// defines the button that has been clicked and performs the corresponding operation
// write operation into oper, we will use it later for output
switch (v.getId())
{
case R.id.Add:
oper = "+";
result = num1 + num2;
break;
case R.id.Sub:
oper = "-";
result = num1 - num2;
break;
case R.id.Mul:
oper = "*";
result = num1 * num2;
break;
case R.id.Div:
oper = "/";
result = num1 / num2;
break;
default:
break;
}
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}}
81
• So now the Coding part is also completed.
• Now run the application to see the output.
Output:
Result:
Thus a Simple Android Application for Native Calculator is developed and executed successfully.
82
Ex. No. 13 Alarm Clock
Date:
Aim:
To develop a Android Application that creates Alarm Clock.
Procedure:
Creating a New project:
• Open Android Studio and then click on File -> New -> New project.
• Then type the Application name as “exno13″ and click Next.
• Then select the Minimum SDK as shown below and click Next.
• Then select theEmpty Activity and click Next.
• Finally click Finish.
• It will take some time to build and load the project.
• After completion it will look as given below.
Creating Second Activity for the Android Application:
• Click on File -> New -> Activity -> Empty Activity.
• Type the Activity Name as AlarmReceiver and click Finish button.
• Thus Second Activityfor the application is created.
Designing layout for the Android Application:
• Click on app -> res -> layout -> activity_main.xml.
• Now click on Text as shown below.
• Then delete the code which is there and type the code as given below.
Code for 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:orientation="vertical">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ToggleButton
android:id="@+id/toggleButton"
83
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:checked="false"
android:onClick="OnToggleClicked" />
</LinearLayout>
• Now click on Design and your application will look as given below.
• So now the designing part is completed.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
import java.util.Calendar;
import androidx.appcompat.app.AppCompatActivity;
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);
}
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.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
85
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
if(System.currentTimeMillis()>time)
{
if (calendar.AM_PM == 0)
time = time + (1000*60*60*12);
else
time = time + (1000*60*60*24);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
}
else
{
alarmManager.cancel(pendingIntent);
Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
}
}
86
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
• So now the Coding part of Alarm Receiver is also completed.
• Now run the application to see the output.
Output:
Result:
Thus Android Application that creates Alarm Clock is developed and executed successfully.
87
Ex. No. 14 Send SMS
Date:
AIM :
To design an android application Send SMS using Intent.
MainActivity.java
package com.example.sms;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage("5554", null, "hai", null, null);
}
});
}
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MainActivity.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"
88
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="54dp"
android:layout_marginTop="166dp"
android:text="send" />
</RelativeLayout>
Output:
Result:
Thus Android Application that send SMS is developed and executed successfully.
89