0% found this document useful (0 votes)
18 views81 pages

MAD Lab Manual

The document outlines a series of exercises for developing Android applications, focusing on various components such as GUI elements, intents, layout managers, and event listeners. Each exercise includes an aim, a step-by-step procedure, and sample code for implementation. The document serves as a guide for creating functional Android applications with specific features and functionalities.

Uploaded by

lavanya naren007
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)
18 views81 pages

MAD Lab Manual

The document outlines a series of exercises for developing Android applications, focusing on various components such as GUI elements, intents, layout managers, and event listeners. Each exercise includes an aim, a step-by-step procedure, and sample code for implementation. The document serves as a guide for creating functional Android applications with specific features and functionalities.

Uploaded by

lavanya naren007
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/ 81

INDEX

SNO DATE TITLE PAGE NO

01 Develop an Application that uses GUI


Component, Fonts and Colors

02 Develop an Application that uses Intent


and Activity

03 Develop an Application that uses


Layout Managers and Event Listeners

04 Write an Application that Draw Basics


Graphical Primitive on the Screen

05 Develop an Application that Make ofRSS


Feed

06 Implement an Application that uses Multi


Threading

07 Develop an Application that Create Alarm


Clock

08 Develop an Application that uses Widgets

09 Implement an Application that uses data to


the SD Card

10 Implements an Application that uses Alert


upon Receiving a Message

11 Develop an Application that makes use


of Database
Ex No :01 Develop an application that uses GUI component

Date : Fonts and Colors.

AIM:

To develop an Android Application that uses GUI components , fonts and colors.

PROCEDURE:

Step 1: Start Android Developer Tools


Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. EnterApplication
Name and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controls in the
design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
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" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:gravity="center"
android:text="HELLO WORLD"
android:textSize="20sp"
android:textStyle="bold" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change font size"
android:textSize="20sp" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change color"
android:textSize="20sp" />

</LinearLayout>
Main Activity Coding:

import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
float font =24;
int i=1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t1=(TextView) findViewById(R.id.textView);
Button b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
t1.setTextSize(font);
font=font+4;
font=20;
if(font==40)
}
});

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


b2.setOnClickListener(new View.OnClickListener()
{

public void onClick(View view)

switch(i)

{
case 1:
t1.setTextColor(Color.parseColor("#0000FF"));
break;
case 2:
t1.setTextColor(Color.parseColor("#00FF00"));
break;
case 3:
t1.setTextColor(Color.parseColor("#FF0000"));
break;
case 4:
t1.setTextColor(Color.parseColor("#800000"));
break;
}
i++;
if(i==5)
i=1;
}
});
}
}
OUTPUT:
Result:

The application for GUI components, fonts and colors has been created successfully and
the result is verified.
EX NO :02

Date : Develop an application that uses Intent and Activity

AIM:
To develop an Android Application for an Intent and Activity.

PROCEDURE:

Step 1: Open the ADT for creating an android application.


Step 2: Click File → new (ALT+SHIFT+N) →Android Application project.
Step 3: New Android app will be open.
Step 4: Give the Application name as Intent which has first letter in capital in the Application
name field.
Step 5: Click next for 3 times.
Step 6: Choose the blank Activity and click next.
Step 7: Give the Activity name and click Finish.
Step 8: An Android app project will be created.
Step 9: Mean while start the emulator to run the android application.

A. Click on AVD Manager.


B. AVD Manager will get open.
C. Click new Button.
D. Give the AVD name as Intent.
E. Choose the devices as 3.7”WVGA(480×800:hdpi).
F. Target will be automatically selected as android 4.4.
G. Click ok.
H. Click start.
I. The emulator will get open separately.

Step 10: Choose a graphical Layout in activity_main.xml for design an application.


Step 11: In the form Widget drag and drop the Plain text in the Layout.
Step 12: Then Next drag Button and drop it in the Layout and give right click and Change the id:
as“Go” from Button.
Step 13: Open the another Java file with Main_activity.java.
Step 14: Give right click copy and paste it in the resourse and name it as Second.java.
Step 15: Design the graphical Layout of activity_second.xml.
Step 16: In the packet drag and drop the LargeText in the Layout.
Step 17: From the form Widget drag and drop Button and name”FINISH”.
Step 18: Write a java coding both in the MainActivity java and second.java.
Step 19: Save the coding.
Step 20: Right click on the Layout→androidManifest.xml→again right click open with Xml
editor→source.
Step 21: Then write some coding in</activity>→click next and write<activity android Name
“second”`>
Step 22: Save the coding.
Step 23: Go to the res- then double click on Android Mainfest.xml – After wards add the coding
in </activity>
<activity android:name="SecondMainActivity"></activity>
</application>
Step 23: Run as→Android Application.
Activity_main.xml

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

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp"
android:ems="10" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="GO" />

</RelativeLayout>

Main Activity:
Packagecom.example.intent;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
public class MainActivity extends Activity {
EditTextet;
Button btn;
@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0){
Intent myintent=new Intent(getApplicationContext(),SecondMainActivity.class);
myintent.putExtra("myname", et.getText().toString());
startActivity(myintent);
}
});
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Second.xml coding

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


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

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FINISH" />

</LinearLayout>
Second MainActivity:

packagecom.example.intent;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
public class SecondMainActivity extends Activity {
TextViewtv;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_main);
tv=(TextView)findViewById(R.id.textView1);
btn=(Button)findViewById(R.id.button1);
tv.setText(getIntent().getExtras().getString("myname").toString());
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
finish();
}
});
}
}

Android Manifest.xml:

</manifest>
OUTPUT:
RESULT:

Hence the android application that uses for an Intent and Activity has been created,
deployed and the output is verified.
EX No : 03 Develop an application that uses Layout Manager

Date : and Event Listeners

AIM :
To develop a simple android application that uses Layout Manager and Event Listeners

PROCEDURE:

Step 1: Open Eclipse IDE.


Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. EnterApplication
Name and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controls
in the design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application
Activity_main.xml

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

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

<TextView
android:id="@+id/fstnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="60dp"
android:text="First Num" />

<ImageButton android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/secnum"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/secnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fstnum"
android:layout_below="@+id/fstnum"
android:layout_marginTop="32dp"
android:text="Second Num" />

<EditText android:id="@+id/secondnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/secnum"
android:layout_alignBottom="@+id/secnum"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/secnum"
android:ems="10" />

<EditText
android:id="@+id/firstnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/fstnum"
android:layout_alignBottom="@+id/fstnum"
android:layout_alignParentRight="true"
android:ems="10" />

</RelativeLayout>
Main Activity.java

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText firstnum=(EditText)findViewById(R.id.firstnum);
final EditText secondnum=(EditText)findViewById(R.id.secondnum);
ImageButton imgbtn=(ImageButton)findViewById(R.id.imageButton1);
imgbtn.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(firstnum.getText().toString().isEmpty()||
secondnum.getText().toString().isEmpty());
{
int num1=Integer.parseInt(firstnum.getText().toString());
int num2=Integer.parseInt(secondnum.getText().toString());
Toast.makeText(getApplicationContext(), "sum of " + (num1 + num2),
Toast.LENGTH_SHORT).show();
}
}

});

}
}
OUTPUT :
RESULT :
Hence the android application that uses for an Intent and Activity has been created,
deployed and the output is verified.
EX NO :04 Write an application that draws basic graphical primitives

Date : on the screen

AIM:
To develop a simple application that draws basic graphicalprimitives on
the screen.

PROCEDURE:
Step1: Open eclipse or android studio and create new project
Step2: Select our project in the project explorer
Step3: Go to res folder and select layout Double click the main xml file
Step4: Type the code for main.xml or drag and drop various componentsused in our program
Step5: Drag and drop relative layout and change its properties
Step6 : Drag and drop image view and change its properties according to our programs
Step7: Screen layout can be viewed by clicking graphics layout tab
Step8: Include necessary files
Step9: Override OnCreate() function
Step10: Create image view and initialize its using id for somecomponents
used in the xml program
Step11: Save the program
Step12: Run the program
Step13: Output can be viewed in the android emulator.
Activity_main.xml:

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

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

<ImageView android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:src="@drawable/ic_launcher" />

</RelativeLayout>
MainActivity.java

package com.example.graphical29;
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.imageView1);
i.setBackgroundDrawable(new BitmapDrawable(bg));

//Creating the Canvas Object Canvas


canvas = new Canvas(bg);

//Creating the Paint Object and set its color & TextSizePaint 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);

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


canvas.drawLine(520, 850, 520, 1150, paint);
}
}
OUTPUT:
RESULT:
Hence the above program is verified and executed successfully
EX NO : 05 Develop an application that makes use of RSS Feed
Date :

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

PROCEDURE:

Step 1: Start Android Developer Tools


Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. EnterApplication
Name and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed ClickFinish.
Step 8: Android app design area will be displayed. Place necessary controls in the
design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
activity_main.XML:

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

MainActivity.Java:

package com.example.rssfeed;

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 ArrayAdapterdoInBackground(Object[] params)

{
headlines = new ArrayList();links = new ArrayList();

try
{

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");
booleaninsideItem = false;
inteventType = 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());
}
}
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)
{
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());

Intentintent = new Intent(Intent.ACTION_VIEW, uri);

startActivity(intent);

public InputStream getInputStream(URL url)


{
try
{
return url.openConnection().getInputStream();
}
catch (IOException e)
{
return null;
}
}
}
OUTPUT:
RESULT:
Thus Android Application that makes use of RSS Feed is developed and executed
successfully.
Ex.NO:6 Implement an application that implements Multi threading

Date:

AIM:

To implement an application that implements multi threading.

PROCEDURE:

Step 1: Open Eclipse IDE.


Step 2: Create the project Multithreading
Step 3: Go to package explorer in the left hand side. Select the project Multithreading.
Step 4: Go to res folder and select layout. Double click the activity_main.xml file.
Step 5: Now you can see the Graphical layout window.
Step 6: Drag and drop the following components:

a. One Progress Bar (Horizontal)


b. One Button with labeled as Start Progress
c. One TextView without any texts
Step 7: Again go to package explorer in the left hand side. Select the project Ex_No_9.
Step 8: Go to src folder. Double click the MainActivity.java file.
Step 9: In java file write the activities done by the application such as action ofbutton.
Step 10:Finally run the android application.
Procedure:

Activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main thread" />

<Button android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:onClick="fetchData" android:text="Start
MULTITHREAD" />

</RelativeLayout>
Main_Activity.java

package com.example.multithearding;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
@SuppressLint("HandlerLeak") public class MainActivity extends Activity {
private TextView tvOutput; private static
final int t1 = 1;private static final int t2 = 2;
private static final int t3 = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOutput = (TextView) findViewById(R.id.textView1);
}
public void fetchData(View v)
{
tvOutput.setText("Main thread");
thread1.start();
thread2.start();
thread3.start();
}
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try { Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t1);
}
}
});
Thread thread2 = new Thread(new Runnable()
{
@Override
public void run()
{
for (int i = 0; i < 5; i++)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(t2);
}

}
Result:

Thus the program to implement the multithreading was executedsuccessfully.


EX NO: 07 Develop an application that creates Alarm Clock
DATE:

AIM:

To develop an Android Application for Alarm clock.

PROCEDURE:

Step 1: Start Android Developer Tools


Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. Enter ApplicationName and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controlsin the
design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
Activity_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"
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>
MainActivity.Java:

package com.example.exno07;

import android.app.AlarmManager;
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.TimePicker;
import android.widget.Toast;
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();
}
}
}
AlarmReceiver.Java:

package com.example.exno07;

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

The application for alarm clock has been created successfully and the result is verified.
Ex No :08 Develop an application Using Widgets
Date :

AIM:

To develop an Android Application for Widget.

PROCEDURE:

Step 1: Start Android Developer Tools


Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. Enter ApplicationName and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controlsin the
design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
Activity_XML:

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

<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="Talking ANALOG CLOCK" />

<AnalogClock android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="38dp" />

<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/analogClock1"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"/>

</RelativeLayout>
MainActivity.java

MainActivity.java

package

com.example.Widget;

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

import java.util.Calendar;

import android.speech.tts.TextToSpeech;
import

android.speech.tts.TextToSpeech.OnInitListener;

import android.view.View;
import

android.widget.AnalogClock;

import android.widget.TextView;
public class MainActivity extends Activity implements OnInitListener
{
TextToSpeech
Talktome; AnalogClock

Clock2; TextView

ReadText;

private int mHour, mMinute;


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

Talktome = new TextToSpeech(this,

this);

Clock2 = (AnalogClock)findViewById(R.id.AnalogClock);

Clock2.setOnClickListener(MyAnalogClockOnClickListener);

ReadText = (TextView)findViewById(R.id.Text2Read);

}
public void onInit(int status)
{
// TODO Auto-generated method stub
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method

stub super.onDestroy();
Talktome.shutdown();
}
private AnalogClock.OnClickListener MyAnalogClockOnClickListener
= new AnalogClock.OnClickListener()
{
@Override
public void onClick(View v)
{
final Calendar c =

Calendar.getInstance(); mHour =
c.get(Calendar.HOUR_OF_DAY); mMinute

= c.get(Calendar.MINUTE); String

myTime= "The Time is is "

+ String.valueOf(mHour)
+ " Hour "
+String.valueOf(mMinute)
+ " Minute";
ReadText.setText(myTime);
Talktome.speak(myTime, TextToSpeech.QUEUE_FLUSH, null);
}
};
}
OUTPUT:
RESULT :

Hence the application using Widget for Talking clock has been created,deployed and the
output is verified.
Ex.no : 09 Implement an application that writes data to the SD card
Date:

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

PROCEDURE:

Step 1: Start Android Developer Tools


Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. Enter ApplicationName and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controls in the design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
activity_main.xml

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

<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="145dp"
android:orientation="vertical" >
</LinearLayout>

<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentLeft="true"
android:text="Save Data" />

<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout1"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp" android:text="Show Data" />

<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:hint="Display saved Data" />

<EditText android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:ems="10" android:hint="ENTER THE
MESSAGE" >

<requestFocus />
</EditText>

</RelativeLayout>
MainActivity.java

package com.example.intesd;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button b1,b2;
TextView tv;
EditText et;
String data;
private String file="mydata";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
tv=(TextView)findViewById(R.id.textView2);
et=(EditText)findViewById(R.id.editText1);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
data=et.getText().toString();
try
{
FileOutputStream fOut=openFileOutput(file,MODE_WORLD_READABLE);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
FileInputStream fin=openFileInput(file);
int c;
String temp="";
while((c=fin.read())!=-1)
{
temp=temp+Character.toString((char)c);
}
tv.setText(temp);
Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
}
}
});
}}
});
Output:
Result:

Thus the program to implement the SD card was executedsuccessfully.


Ex.No.10 Implement an application that creates an alert upon receiving a message

Date:

AIM:
To develop an Android Application an alert upon receiving a message

PROCEDURE:
Step 1: Start Android Developer Tools
Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. Enter ApplicationName and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controls in the design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
activity_main.xml

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

<Button

android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:text=" NOTIFICATION" />
</RelativeLayout>
MainActivity.java

package com.example.notify;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
importandroid.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity

@Override protected void onCreate(BundlesavedInstanceState)

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener()

{
@Override public void
onClick(View v)

NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new
Notification(android.R.drawable.stat_notify_more,"You have got an
email",System.currentTimeMillis()); Context context=MainActivity.this;
Intent intent=new Intent(context,MainActivity.class);

PendingIntent pending=PendingIntent.getActivity(getApplicationContext(),0,intent,0);
notification.setLatestEventInfo(context,"E-Mail from Facebook","You have 4friend
requests",pending); nm.notify(0,notification);
}

});

@Override public boolean


onCreateOptionsMenu(Menu menu)

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


true;

}
Output :
RESULT:

Hence the Android application that creates an alert upon receiving amessage has been
created, deployed and the output is verified.
Ex No : 11 Develop an application that makes use of Database
Date:

AIM:
To develop an Android Application that makes use of database.

PROCEDURE:

Step 1: Start Android Developer Tools


Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. Enter ApplicationName and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controls in thedesign area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
MainActivity.java

package com.example.database;

import android.support.v7.app.ActionBarActivity;
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 ActionBarActivity {

EditText name,regno,mark;

Button btnAdd,btnDelete,btn Update,btnView,btnViewAll;

SQLiteDatabase db;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); regno=

(EditText)findViewById(R.id.editText1);name=

(EditText)findViewById(R.id.editText2);

mark=(EditText)findViewById(R.id.editText3);
btnAdd (Button)findViewById(R.id.button1);

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

btnViewAll (Button)findViewById(R.id.button3);

btnUpdate= (Button)findViewById(R.id.button4);

btnDelete= (Button)findViewById(R.id.button5);

Creating database and table

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


db.execSQL("CREATE TABLE IF NOT EXISTS student(regno VARCHAR, name VARCHR,
mark VARCHAR);");
btnAdd.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

// Checking for empty fields


if(regno.getText().toString().trim().length()==0||name.getText().toString().tri
m().length()==0||mark.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student
VALUES(""+regno.getText()+","+name.getText()+","+mark.getText()+"");");
showMessage("Success", "Record added");
clearText();
}
});

// Deleting a record from the Student table


btnDelete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(regno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Reg. No.");

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

// Updating a record in the Student table


btnUpdate.setOnClickListener(new OnClickListener()

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(regno.getText().toString().trim().length()==0)

showMessage("Error", "Please enter Reg. No.");

return;

}
Cursor c=db.rawQuery("SELECT * FROM student WHERE regno="+regno.getText()+"'", null);

if(c.moveToFirst())

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


mark=""+mark.getText()+" WHERE regno=""+regno.getText()+""");

showMessage("Success", "Record Modified");

} else

showMessage("Error", "Invalid Reg. No.");

} clear Text();

});

// Display a record from the Student table

btnView.setOnClickListener(new OnClickListener()

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(regno.getText().toString().trim().length()==0)

showMessage("Error", "Please enter Reg. No.");


return;

Cursor c=db.rawQuery("SELECT * FROM student WHERE regno=""+regno.getText()+"'",null);

if(c.moveToFirst())

name.setText(c.getString(1));

mark.setText(c.getString(2));

} else

showMessage("Error", "Invalid Reg. No.");

clearText();

});

// Displaying all the records

btnViewAll.setOnClickListener(new OnClickListener()

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

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("Reg. No : "+c.getString(0)+"\n");

buffer.append("Name : "+c.getString(1)+"\n");

buffer.append("Mark: "+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();
}

public void clearText()

regno.setText("");

name.setText("");

mark.setText("");

regno.requestFocus();

}
OUTPUT :
RESULT :

The application for creating and using a database is created successfullyand the result is
verified.

You might also like