0% found this document useful (0 votes)
108 views72 pages

MAD Lab Manual Modified (2017)

The document describes creating an Android application that: 1. Uses GUI components like TextViews, EditTexts, Buttons, and a GridLayout to collect user input like name, registration number, and department in a form. 2. Implements an event listener on a submit button to navigate to a welcome screen on click. 3. The welcome screen displays the user's inputted name using TextViews in a LinearLayout.

Uploaded by

Sofiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views72 pages

MAD Lab Manual Modified (2017)

The document describes creating an Android application that: 1. Uses GUI components like TextViews, EditTexts, Buttons, and a GridLayout to collect user input like name, registration number, and department in a form. 2. Implements an event listener on a submit button to navigate to a welcome screen on click. 3. The welcome screen displays the user's inputted name using TextViews in a LinearLayout.

Uploaded by

Sofiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Ex: No: 1 APPLICATION USING GUI COMPONENTS FONTS AND COLORS

AIM:
To create an application using GUI components and to change the font and color of the
text in its properties.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page to get Student details like
name, date of birth, gender, mobile number, email id, etc..
Step 4. Use the GUI components like
i. Layout -> Linear Layout(Vertical)
ii. Widgets -> Plain TextView (or) LargeText (or) MediumText (or)
SmallText, Radio button, Check Box to display the information.
iii. Text Fields ->Plain Text, Person Name, Date, Phone, E-mail to get the
user information.
Step 5. Click a component, its properties will be displayed on the right hand side. Change
its background, Text Color and Typeface.
Step 6. Execute the program

PROGRAM:

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"

1
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
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>

ActivityMain.java

package com.example.myapplication;

import android.graphics.Color;
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 ch=1;
float font=30;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t= (TextView) findViewById(R.id.textView);
Button b1= (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setTextSize(font);
font = font + 5;
if (font == 50)
font = 30;
}
});
Button b2= (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override

2
public void onClick(View v) {
switch (ch) {
case 1:
t.setTextColor(Color.RED);
break;
case 2:
t.setTextColor(Color.GREEN);
break;
case 3:
t.setTextColor(Color.BLUE);
break;
case 4:
t.setTextColor(Color.CYAN);
break;
case 5:
t.setTextColor(Color.YELLOW);
break;
case 6:
t.setTextColor(Color.MAGENTA);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}

3
OUTPUT

RESULT:
Thus to create an application using GUI components and to change the font and color of the text
has been developed successfully.

4
Ex: No: 2 APPLICATION USING LAYOUT MANAGER AND EVENT LISTENERS

AIM:
To create an application using layout manager and event listeners.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page to get User’s first and last
name.
Step 4. Use the GUI components like
i. Layout -> Linear Layout(Vertical)
ii. Widgets -> Plain TextView (or) LargeText (or) MediumText (or)
SmallText, Button to display the information.
iii. Text Fields -> Plain Text or Person Name to get the user information

Step 5. Under ‘Package Explorer’ (On the left hand side) double click on java and expand
it.
Step 6. In ‘Design View’ of ‘activity_welcome.xml’, design a page to Welcome the user
Step 7. Use the GUI components like
i. Layout -> Linear Layout(Horizontal)
ii. Widgets ->LargeText to display the information
Step 8. For the <Button> element, add the android:onClick attribute. (Under
content_main.xml)

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"
tools:context=".MainActivity">

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

5
<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>

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

6
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="0"
android:text="Reg.No"
android:textSize="20sp"
android:gravity="center"/>

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

7
android:layout_centerInParent="true"
android:layout_marginBottom="150dp"
android:text="Submit"/>

Activity_second.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="com.example.myapplication.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"/>

8
MainActivity.java

package com.example.myapplication;
import android.content.Intent;
import androidx.appcompat.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;
public class MainActivity extends AppCompatActivity {
//Defining the Views
EditText e1,e2;
Button bt;
Spinner s;
//Data for populating in Spinner
String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Referring the Views
e1= (EditText) findViewById(R.id.editText);
e2= (EditText) findViewById(R.id.editText2);
bt= (Button) findViewById(R.id.button);
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);
//Creating Listener for Button
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Getting the Values from Views(Edittext & Spinner)
name=e1.getText().toString();
reg=e2.getText().toString();
dept=s.getSelectedItem().toString();
//Intent For Navigating to Second Activity
Intent i = new Intent(MainActivity.this,SecondActivity.class);
//For Passing the Values to Second Activity

9
i.putExtra("name_key", name);
i.putExtra("reg_key",reg);
i.putExtra("dept_key", dept);
startActivity(i);
}
});
}
}

SecondActivity.java:

package com.example.myapplication;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView t1,t2,t3;
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1= (TextView) findViewById(R.id.textView1);
t2= (TextView) findViewById(R.id.textView2);
t3= (TextView) findViewById(R.id.textView3);
//Getting the Intent
Intent i = getIntent();
//Getting the Values from First Activity using the Intent received
name=i.getStringExtra("name_key");
reg=i.getStringExtra("reg_key");
dept=i.getStringExtra("dept_key");
//Setting the Values to Intent
t1.setText(name);
t2.setText(reg);
t3.setText(dept);
}
}

10
OUTPUT:

RESULT:
Thus to create an application using layout manager and event listeners has been
developed successfully.

11
Ex: No: 3 APPLICATION TO DRAW BASIC GRAPHICAL PRIMITIVES ON
SCREEN

AIM:
To create an application to draw basic graphical primitives on screen

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. In activity_main.xml file. Don't change anything in the layout. Leave it as default.
Step 4. Now select MainActivity.java file and edit the java code (Given below).
Step 5. Execute the program.

PROGRAM:

MainActivity.java

package com.example.exno4;

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;

public class MainActivity extends Activity


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

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

//Setting the Bitmap as background for the ImageView


ImageView i = (ImageView) findViewById(R.id.imageView);

12
i.setBackgroundDrawable(new BitmapDrawable(bg));

//Creating the Canvas Object


Canvas canvas = new Canvas(bg);

//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);

//To draw a Rectangle


canvas.drawText("Rectangle", 420, 150, paint);
canvas.drawRect(400, 200, 650, 700, paint);

//To draw a Circle


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);
//To draw a Line
canvas.drawText("Line", 480, 800, paint);
canvas.drawLine(520, 850, 520, 1150, paint);
}
}
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>

13
OUTPUT:

RESULT:
Thus to create an application to draw basic graphical primitives has been developed
successfully.

14
Ex: No: 4 DEVELOP AN APPLICATION THAT MAKES USE OF DATABASE

AIM:
To create an application that makes use of database.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. Under ‘Package Explorer’ (On the left hand side) double click on java and expand
it.
Right click on ‘com.eg.adminpc.exno5’
Step 4. In ‘Design View’ of ‘activity_main.xml’, design a database to get student detail.
Step 5. Use the GUI components like
i. Layout -> Linear Layout(Vertical) and Linear Layout(Horizontal) to
place the button.
ii. Widgets -> Plain TextView (or) LargeText (or) MediumText (or)
SmallText, Button to display the information.
iii. Text Fields ->Plain Text or Person Name to get the Student information
Step 6. For the <Button> element, add the android:onClick attribute. (Under
content_main.xml)
Step 7. A notification symbol(bulb) will be displayed on the LHS, click on it and Choose
“Create ‘insert(View)’, ‘clear (View)’ & ‘viewdata(View)’in ‘MainActicity’”
Step 8. Under MainActivity.java include the code (below).
Step 9. Execute the program.

PROGRAM:

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

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

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

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

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

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

<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="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"
android:text="View"

17
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>

MainActivity.java

package com.example.exno5;

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;

public class MainActivity extends Activity implements OnClickListener


{
EditText Rollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);

18
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);

// Creating database and table


db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name
VARCHAR,marks VARCHAR);");
}
public void onClick(View view)
{
// Inserting a record to the Student table
if(view==Insert)
{
// Checking for empty fields
if(Rollno.getText().toString().trim().length()==0||
Name.getText().toString().trim().length()==0||
Marks.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('"+Rollno.getText()
+"','"+Name.getText()+
"','"+Marks.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table
if(view==Delete)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");

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

20
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);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}

21
public void clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}

22
OUTPUT:

RESULT:
Thus to create an application using database to store the student detail has been
developed successfully.

23
Ex: No: 5 APPLICATION THAT IMPLEMENTS MULTITHREADING

AIM:
To create an application that implements multithreading.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for multithreading.
Step 4. Use the GUI components like
i. Layout -> Linear Layout(Vertical) and Linear Layout(Horizontal) to
place the button.
ii. Widgets -> Button and LargeText to display the information.
Step 5. For the <Button> element, add the android:onClick attribute. (Under
content_main.xml)
Step 6. A notification symbol(bulb) will be displayed on the LHS, click on it and Choose
“Create ‘fetchData(View)’in ‘MainActicity’”
Step 7. Under MainActivity.java include the code (below).
Step 8. Execute the program.

Program:

Activity_main.xml

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


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

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

24
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
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>

MainActivity.java

package com.example.exno7;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{
ImageView img;
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt1 = (Button)findViewById(R.id.button);
bt2= (Button) findViewById(R.id.button2);
img = (ImageView)findViewById(R.id.imageView);

bt1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{

25
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);
}
});
}
}).start();
}
});
}
}

26
RESULT:
Thus to create an application to implement multithreading has been successfully done.

27
EX. No: 6 DEVELOP A NATIVE APPLICATION THAT USES GPS LOCATION

INFORMATION

AIM
To write a mobile application that create a nature application that uses GPS Location
information

ALGORITHM
Step 1. Open android studio and select android project.
Step 2. Give project name and select next.
Step 3. Configure your project,select Minimum SDK, add an Activity and customize the
activity.
Step 4. In design view of Activity_main.xml design a page for setting GPS Location.
Step 5. Now select the MainActivity.Java file edit the java code.
Step 6. Add Android Manifest.xml code below.
Step 7. Next step is to set permission to uses GPS Location information.
Step 8. To run the program click run button and choose emulator under target device and
select your virtual device from the list and click Run button.
i. After running the AVD Manager,
Select Tools->Android->Android device monitor.
Choose open perspective->DDM .
Step 9. Then Android device monitor that window then click Send Button.
Step 10. The Latitude and Longitude window will be displayed.
Step 11. Stop the program.

MainActivity.java

package com.example.admin.gps;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements LocationListener {


private Context context;

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

28
setContentView(R.layout.activity_main);
context = this;

LocationManager locationManager;
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String str = "Latitude: " + location.getLatitude() + "Longitude: " + location.getLongitude();

Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();


}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
</RelativeLayout>

29
AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.gps" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

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


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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

30
OUTPUT:

RESULT:

Thus the implementation of an application that that uses GPS location information using
Android studio was completed successfully.

31
EX.No:7 IMPLEMENT AN APPLICATION THAT WRITES DATA TO THE SD CARD

AIM:
To implement an application that writes data to the SD data using Android studio.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for multithreading.
Step 4. Use the GUI components like
i. Layout -> Linear Layout (Vertical).
ii. Widgets -> Button and LargeText to display the information.

Step 5. In main.xml file add the code below. Now select MainActivity.java file and edit
the java code (Given below).
Step 6. Next step is to set permission to write data in SD card.
Step 7. To run the program click run button and choose emulator under target device and
select your virtual device from the list and click Run button

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="20dp"
android:orientation="vertical">

<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"
android:layout_margin="10dp"
android:text="Write Data"

32
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>

MainActivity.java

package com.example.exno9;

import android.os.Bundle;
import androidx.appcompat.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;

public class MainActivity extends AppCompatActivity


{
EditText e1;
Button write,read,clear;
@Override
protected void onCreate(Bundle savedInstanceState)

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

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


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

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();
}
}
});

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);

34
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();
}
}
});

clear.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
e1.setText(""); }
});
}
}

AndroidManifest.xml

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


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

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

35
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>

36
OUTPUT:

RESULT:
Thus the implementation of an application that writes data to the SD data using Android
studio was completed successfully.

37
Ex: No: 8 IMPLEMENT AN APPLICATION THAT CREATES
AN ALERT UPON RECEIVING A MESSAGE
AIM:
To implement an application that creates an alert upon receiving a message.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for multithreading.
Step 4. Use the GUI components like
i. Layout -> Linear Layout (Vertical) and Linear Layout (Horizontal) to
place the button.
ii. Widgets -> Button and LargeText to display the information.
iii. Text Fields -> PlainText to get the information
Step 5. Under ‘Package Explorer’ (On the left hand side) double click on java and expand
it.
Step 6. For the <Button> element, add the android:onClick attribute. (Under
activity_main.xml)
Step 7. A notification symbol(bulb) will be displayed on the LHS, click on it and Choose
“Create ‘notify(View)’in ‘MainActicity’”
Step 8. Under MainActivity.java include the code (below).
Step 9. Execute the program.

PROGRAM:

ActivityMain.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"

38
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>

MainActivity.java

package com.example.exno10;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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);

39
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).setConte
ntIntent(pending).build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, noti);
}
});
}
}

40
OUTPUT:

RESULT:
Thus the implementation of an application that creates an alert upon receiving a message
using Android studio was completed successfully.

41
Ex: No: 9 RSS FEEDER

AIM:
To develop an application for RSS reader.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for RSS Feed.
Step 4. Use the GUI components like
i. TextField -> PlainText
ii. Widgets a Button.

Step 5. Under MainActivity.java include the code (below).


Step 6. Under ‘Package Explorer’ (On the left hand side) double click on java and expand
it.
Step 7. Under activity_second.java include the code (below).
Step 8. Design a page for WebView
Step 9. Use the GUI components like
i. Widgets -> WebView to display the web information.

Step 10. Generate ‘HandleXML.java’ and write the code.


Step 11. Next step is to set permission to access internet. Go to AndroidManifest.xml file.
Step 12. Execute the program

Activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

42
MainActivity.java

package com.example.exno6;

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;
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;

public class MainActivity extends ListActivity


{
List headlines;
List links;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
}

class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>


{
@Override
protected ArrayAdapter doInBackground(Object[] params)
{
headlines = new ArrayList();

links = new ArrayList();


try

43
{
URL url = new URL("https://codingconnect.net/feed");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();

// We will get the XML from an input stream


xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;

// 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();

44
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
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;
}
}
}

45
AndroidManifest.xml

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


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

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

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

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>

46
OUTPUT:

RESULT:

Thus Android Application that makes use of RSS Feed is developed and
executed successfully.

47
Ex.No.10 APPLICATION THAT MAKES USE OF NOTIFICATION MANAGER

AIM:
To create an application that makes use of Notification Manager.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for notification manager
Step 4. Use the GUI components like
i. Layout ->Linear Layout (Vertical).
ii. Widgets ->Button and LargeText to display the information.
Step 5. Under ‘Package Explorer’ (On the left hand side) double click on java and expand
it.
Step 6. Execute the program.

MainActivity.java

package com.example.notificationdemo;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

48
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {

private NotificationManager mNotificationManager;


private int notificationID = 100;
private int totalMessages = 0;
private Button btnStartNotif, btnUpdateNotif, btnCancelNotif, btnInBoxNotification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartNotif = (Button) findViewById(R.id.start);
btnUpdateNotif = (Button) findViewById(R.id.update);
btnCancelNotif = (Button) findViewById(R.id.cancel);
btnInBoxNotification = (Button) findViewById(R.id.bigNotification);
btnStartNotif.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
displayNotification();
}
});
btnUpdateNotif.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateNotification();
}
});
btnCancelNotif.setOnClickListener(new OnClickListener() {
@Override

49
public void onClick(View v) {
cancelNotification();
}
});
btnInBoxNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
displayInBoxNotification();
}
});

@SuppressLint("NewApi")

void displayNotification() {

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(

this);

nBuilder.setContentTitle("Notification");

nBuilder.setContentText("You have received a new Notification");

nBuilder.setTicker("New Message");

nBuilder.setAutoCancel(true);

nBuilder.setSmallIcon(R.drawable.ic_tag_logo);

nBuilder.setNumber(++totalMessages);

Intent intent = new Intent(this, NotificationClass.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(NotificationClass.class);

stackBuilder.addNextIntent(intent);

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,

PendingIntent.FLAG_UPDATE_CURRENT);

50
nBuilder.setContentIntent(pendingIntent);

mNotificationManager = (NotificationManager)

getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(notificationID, nBuilder.build());

void cancelNotification() {

if (mNotificationManager != null) {

mNotificationManager.cancel(notificationID);

@SuppressLint("NewApi")

void updateNotification() {

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(

this);

mBuilder.setContentTitle("Updated Notification");

mBuilder.setContentText("You've got updated Notification.");

mBuilder.setTicker("Updated Notification Alert!");

mBuilder.setAutoCancel(true);

mBuilder.setSmallIcon(R.drawable.ic_tag_logo);

mBuilder.setNumber(++totalMessages);

Intent resultIntent = new Intent(this, NotificationClass.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(NotificationClass.class);

stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,

PendingIntent.FLAG_UPDATE_CURRENT);

51
mBuilder.setContentIntent(resultPendingIntent);

mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(notificationID, mBuilder.build());

void displayInBoxNotification() {

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(

this);

mBuilder.setContentTitle("New Message");

mBuilder.setContentText("You've received new message.");

mBuilder.setTicker("New Message Alert!");

mBuilder.setAutoCancel(true);

mBuilder.setSmallIcon(R.drawable.ic_tag_logo);

mBuilder.setNumber(++totalMessages);

NotificationCompat.InboxStyle inboxStyle = new

NotificationCompat.InboxStyle();

String[] notificationArray = new String[6];

notificationArray[0] = new String("First Notification...");

notificationArray[1] = new String("Second Notification...");

notificationArray[2] = new String("Third Notification ....");

notificationArray[3] = new String("Fourth Notification.....");

notificationArray[4] = new String("Fifth Notification....");

notificationArray[5] = new String("Sixth Notification....");

inboxStyle.setBigContentTitle("Notification Details.");

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

inboxStyle.addLine(notificationArray[i]);

52
mBuilder.setStyle(inboxStyle);

Intent resultIntent = new Intent(this, NotificationClass.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(NotificationClass.class);

stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,

PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);

mNotificationManager = (NotificationManager)

getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(notificationID, mBuilder.build());

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

return super.onOptionsItemSelected(item);

53
Android Manifest

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.notificationdemo"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="21" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_tag_logo"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name="com.example.notificationdemo.NotificationClass" />

</application>

</manifest>

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

<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_note"/>
<Button
android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cancel_note"/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/update_note"/>
<Button
android:id="@+id/bigNotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/big_notification" />
</LinearLayout>

55
OUTPUT:

RESULT:
Thus an application to create notification manager has been developed successfully.

56
Ex. No.11 APPLICATION TO SEND AN EMAIL

AIM:
To create an application that send an email

ALGORITHM:
Step 1: Open a new project
Step 2: Configure your project, select Minimum SDK, add an activity and
customize the
activity.
Step 3: In ‘Design View’ of ‘activity_main.xml’, design a page for sending email
Step 4: Use the GUI components like
i. Layout -> Linear Layout (Vertical).
ii. Widgets -> Button and LargeText to display the information.
Step 5: Under ‘Package Explorer’ (On the left hand side) double click on java and
expand it.
Step 6: Execute the 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:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"

57
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send"
android:id="@+id/btnSend"/>
</LinearLayout>

MainActivity.java

package com.tutlane.sendmailexample;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

private EditText eTo;


private EditText eSubject;
private EditText eMsg;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTo = (EditText)findViewById(R.id.txtTo);
eSubject = (EditText)findViewById(R.id.txtSub);
eMsg = (EditText)findViewById(R.id.txtMsg);
btn = (Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.getText().toString()});
it.putExtra(Intent.EXTRA_SUBJECT,eSubject.getText().toString());
it.putExtra(Intent.EXTRA_TEXT,eMsg.getText());
it.setType("message/rfc822");

58
startActivity(Intent.createChooser(it,"Choose Mail App"));
}
});
}
}

AndroidManifest.xml

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


<manifest
xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutlane.sendmailex
ample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>

59
OUTPUT

RESULT:
Thus an application to send email has been developed successfully.

60
Ex: No: 12 APPLICATION TO CREATE NATIVE CALCULATOR

AIM:
To create a native calculator application.

ALGORITHM:
Step 1: Open a new project
Step 2: Configure your project, select Minimum SDK, add an activity and customize the
activity.
Step 3: In ‘Design View’ of ‘activity_main.xml’, design a calculator page
Step 4: Use the GUI components like
iii. Widgets -> Button to display the number and operators.
iv. Text Fields -> Plain Text to get the user data.

Step 5: Under ‘Package Explorer’ (On the left hand side) double click on java and
expand it.
Step 6: Execute the program.

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"
android:layout_margin="20dp">

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

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

62
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>

</LinearLayout>

MainActivity.java

package com.example.devang.exno3;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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);

//Referring the Views


Num1 = (EditText) findViewById(R.id.editText1);
Num2 = (EditText) findViewById(R.id.editText2);
Add = (Button) findViewById(R.id.Add);
Sub = (Button) findViewById(R.id.Sub);
Mul = (Button) findViewById(R.id.Mul);

63
Div = (Button) findViewById(R.id.Div);
Result = (TextView) findViewById(R.id.textView);

// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}

@Override
public void onClick (View v)
{

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;

64
break;
case R.id.Div:
oper = "/";
result = num1 / num2;
break;
default:
break;
}
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}

65
OUTPUT:

66
RESULT:
Thus an application to create native calculator has been developed successfully.

67
Ex: No: 13 TO WRITE AN APPLICATION THAT CREATES ALARM
CLOCK

AIM:
To write a mobile application that creates alarm clock.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the
activity as ‘AlarmActivity’.
Step 3. In ‘Design View’ of ‘activity_alarm.xml’, design a page for setting alarm.
Step 4. Use the GUI components like
i. Layout -> Linear Layout (Vertical)
ii. Widgets -> ToggleButton and LargeText.
iii. Date & Time -> TimePicker
Step 5. For the <ToggleButton> element, add the android:onClick attribute.
(activity_alarm.xml)

Step 6. Under AlarmActivity.java include the code (below).


Step 7. Under ‘Package Explorer’ (On the left hand side) double click on java and expand
it.
Step 8. Under AlarmReceiver.java include the code (below).
Step 9. Under AlarmService.java include the code (below).
Step 10. Under ‘Package Explorer’ (On the left hand side) double click on manifests &
expand it. Double click on ‘AndroidManifest.xml’
Step 11. Execute the program.

PROGRAM:

Activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="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"

68
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>

Androidmanifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication" >
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" >
</receiver>
</application>
</manifest>

MainActivity.java

package com.example.myapplication;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;

69
import android.widget.ToggleButton;
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);
}
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());
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();
}

70
}
}

AlarmReceiver.java

package com.example.myapplication;

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.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
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);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}

71
OUTPUT:

RESULT:
Thus the implementation of an application that creates an alert upon receiving a message
using Android studio was completed successfully.

72

You might also like