Sie sind auf Seite 1von 87

1|Page

Index

Ex. No Name of Experiments Page No

1 Implementation of GUI components, Font and Colors 3-6

2 Implementation of Layout Managers and event listeners. 7-11

3 Implementation of native calculator application 12-24

Implementation of Draw a Rectangle using basic graphical primitives


on the screen.
4 25-29
Implementation of Draw a Circle using basic graphical primitives on
the screen.

5 Implementation of Student database Using SQLite. 30-41

6 Implementation and Uses of RSS Feed. 42-49

Multi-threading Implementation Using Progress Bar


7 50-54
Multi-threading Implementation Using Picture Viewer

8 Implementation of GPS location information. 55-61

9 Implementation of an application that writes data to the SD card. 70-75

10 Implementation of an alert upon receiving a message 76-82

11 Implementation of alarm clock 83-86

2|Page
Ex.No.1 Develop an application that uses GUI components, Font and Colors

Aim:
To develop an application that uses GUI components,Fonts and Colors.
Procedure:
Step 1: File New Android Project Application
Step 2: Give the Project Name Next
Step 3: Go to Res Folder and Select Layout Click the Main.xml
Step 4: Design the Graphical Layout
Step 5: Write code the in MainActivity.java
Step 6: Run the code using AVD Emulator

ActivityMain.Java

packagecom.example.text;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;

public class MainActivity extends Activity {


Button b1,b2;
TextView t1;
float font;
inti=1;

@Override

3|Page
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1=(Button)findViewById(R.id.button1);
t1=(TextView)findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t1.setTextSize(font);
font=font+4;
if(font==40)
font=20;

}
});

b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (i) {
case 1:
t1.setTextColor(Color.parseColor("#0000FF"));
break;

case 2:
t1.setTextColor(Color.parseColor("#00FF00"));
break;

4|Page
case 3:
t1.setTextColor(Color.parseColor("#FF0000"));
break;
default:
t1.setTextColor(Color.parseColor("#800000"));
break;

}
i++;
if(i==5)
i=1;
}
});

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

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"

5|Page
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="140dp"
android:text="@string/hello_world" />

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

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="40dp"
android:text="Change Font Color"
android:textSize="23sp" />
</RelativeLayout>

6|Page
Output

Result:
Thus the program to develop an application that uses GUI components, Fonts
and Colors is executed successfully.

7|Page
Ex.No.2 Develop an application that uses Layout Managers and event listeners.

Aim:
To develop an application that uses Layout Managers and Event listeners.
Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: Design the Acitivity_main.xml with EditText and Command Button
Step 4: In AcitivityMain.java declare the necessary variable and specify the name to the
controls.
Step 5: Get the Input and Convert it to Value
Step6 : Print the result using Toast Class

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

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="56dp"

8|Page
android:layout_marginTop="48dp"
android:ems="10"
android:text="No 1">

<requestFocus/>
</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="41dp"
android:ems="10"
android:text="No 2"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="Add"/>
/RelativeLayout>

Activity Main.java

packagecom.example.addition;
importandroid.R.integer;
importandroid.R.string;
importandroid.app.Activity;
importandroid.os.Bundle;

9|Page
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;

publicclassMainActivityextends Activity {
EditTextt1,t2;
Button b1;

@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(newOnClickListener() {

@Override
publicvoidonClick(View arg0) {
// TODO Auto-generated method stub
String v1=t1.getText().toString();
String v2=t2.getText().toString();

int a = Integer.parseInt(v1);
int b=Integer.parseInt(v2);
int sum=a+b;
Toast.makeText(MainActivity.this, "Sum of Two No"+sum, 5000).show();
}
});
}

10 | P a g e
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}

Output

Result:
Thus the program to develop an application that uses Layout Managers and Event
listeners is executed successfully.

11 | P a g e
Ex.No.3 Develop a native calculator application.

Aim:
To develop a native calculator application.
Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: Design the Acitivity_main.xml with EditText and Command Button
Step 4: In AcitivityMain.java declare the necessary variable and specify the name to the
controls.
Step 5: Get the Input and Convert it to Value
Step6 : Print the result using Toast Class

/* Native Calculator Program-MainActivity.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=".MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Native Calculator Apps"/>

<EditText

12 | P a g e
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="32dp"
android:ems="10">
<requestFocus/>
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="15dp"
android:text="1"/>

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

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"

13 | P a g e
android:layout_toRightOf="@+id/button2"
android:text="3"/>

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

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

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button5"
android:layout_alignBottom="@+id/button5"
android:layout_alignLeft="@+id/button3"
android:text="6"/>

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"

14 | P a g e
android:layout_below="@+id/button4"
android:text="7"/>

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_toRightOf="@+id/button7"
android:text="8"/>

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button8"
android:layout_alignBottom="@+id/button8"
android:layout_toRightOf="@+id/button8"
android:text="9"/>

<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button7"
android:text="0"/>

<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button10"

15 | P a g e
android:layout_alignBottom="@+id/button10"
android:layout_alignLeft="@+id/button8"
android:text="."/>

<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button11"
android:layout_alignBottom="@+id/button11"
android:layout_toRightOf="@+id/button11"
android:text="="/>

<Button
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="+"/>

<Button
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button6"
android:layout_alignBottom="@+id/button6"
android:layout_alignLeft="@+id/button13"
android:text="-"/>

<Button
android:id="@+id/button15"
android:layout_width="wrap_content"

16 | P a g e
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button9"
android:layout_alignBottom="@+id/button9"
android:layout_toRightOf="@+id/button9"
android:text="*"/>

<Button
android:id="@+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button12"
android:layout_alignBottom="@+id/button12"
android:layout_alignLeft="@+id/button15"
android:text="/"/>
<Button
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/editText1"
android:layout_toRightOf="@+id/editText1"
android:text="C"/>
</RelativeLayout>

17 | P a g e
/* Native Calculator Program-MainActivity.java*/

packagecom.example.calculator;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;

public class MainActivity extends Activity {

Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdot,badd,bsub,bmul,bdiv,beq, bC;


EditTextet;
int val1,val2;
booleanadd,sub,div,mul;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b2=(Button) findViewById(R.id.button2);
b3=(Button) findViewById(R.id.button3);
b4=(Button) findViewById(R.id.button4);
b5=(Button) findViewById(R.id.button5);
b6=(Button) findViewById(R.id.button6);
b7=(Button) findViewById(R.id.button7);
b8=(Button) findViewById(R.id.button8);
b9=(Button) findViewById(R.id.button9);
b0=(Button) findViewById(R.id.button10);
bdot=(Button) findViewById(R.id.button11);
beq=(Button) findViewById(R.id.button12);

18 | P a g e
badd=(Button) findViewById(R.id.button13);
bsub=(Button) findViewById(R.id.button14);
bmul=(Button) findViewById(R.id.button15);
bdiv=(Button) findViewById(R.id.button16);
bC=(Button)findViewById(R.id.button17);
et=(EditText) findViewById(R.id.editText1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"3");
}
});
b4.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"4");
}

19 | P a g e
});
b5.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"5");
}
});
b6.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"6");
}
});
b7.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"7");
}
});
b8.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"8");
}
});
b9.setOnClickListener(new View.OnClickListener() {

20 | P a g e
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"9");
}
});
b0.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+"10");
}
});
bdot.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText(et.getText()+".");
}
});
badd.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
val1=Integer.parseInt(et.getText()+"");
add=true;
et.setText(null);
}
});
bsub.setOnClickListener(new View.OnClickListener() {

21 | P a g e
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
val1=Integer.parseInt(et.getText()+"");
sub=true;
et.setText(null);

}
});
bmul.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
val1=Integer.parseInt(et.getText()+"");
mul=true;
et.setText(null);

}
});
bdiv.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
val1=Integer.parseInt(et.getText()+"");
div=true;
et.setText(null);

}
});
beq.setOnClickListener(new View.OnClickListener() {

22 | P a g e
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
val2=Integer.parseInt(et.getText()+"");

if (add==true) {
et.setText(val1+val2+"");
add=false;
}
if (sub==true) {
et.setText(val1-val2+"");
sub=false;
}
if (mul==true) {
et.setText(val1*val2+"");
mul=false;
}
if (div==true) {
et.setText(val1/val2+"");
div=false;
}
}
});
bC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText("");
}
});

}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {

23 | P a g e
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

Output

Result:
Thus the program to develop a native calculator application is executed successfully.

24 | P a g e
Ex. No.4 Graphical Primitives

4. (a) Draw a Rectangle


Aim:
To develop an application to draw basic graphical primitives like rectangle on the screen.
Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: use paint class
Step 4: Specify the stroke size & color
Step 5: Specify Rectangle object size
Step6 : Run the code using AVD Emulator

/* Program to Draw a Rectangle Using the Paint Class in Android */

package com.example.graphics;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));
}

25 | P a g e
private class myView extends View{

public myView(Context context) {


super(context);
}

@Override
protected void onDraw(Canvas canvas) {

Paint myPaint = new Paint();


myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
canvas.drawRect(20, 20, 100, 100, myPaint);
}}}

Output:

Result:

Thus the program to develop an application to draw basic graphical primitives like rectangle on
the screen is executed successfully

26 | P a g e
4. (b) Draw a Circle

Aim:
To develop an application to draw basic graphical primitives like circle on the screen.
Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: use paint class and Specify the style and color
Step 4: Specify the Circle Height, Width and Radios
Step 5: Specify Rectangle object size
Step6 : Run the code using AVD Emulator

/* Program to Draw a Circle Using the Paint Class in Android */

package com.example.circle;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context context) {
super(context);

27 | P a g e
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#00FF00"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
}
}

28 | P a g e
Output:

Result:
Thus the program to develop an application to draw basic graphical primitives like
circle on the screen is executed successfully.

29 | P a g e
Ex. No. 5 Develop an application that makes use of database.

Aim:
To develop an application that makes use of database.
Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: Create a layout using the widgets i.e. use Buttons and also textviews.
Step 4: Write the code to make use of database.
Step 5: Write the code to perform insertion, deletion, updation, viewing files and to
view all files.
Step 6: Run the code as Android Application
Step 7: Check the actions in the database.

activity_main.xml

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


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />

<TextView

30 | P a g e
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"

31 | P a g e
android:textSize="20sp" />

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

<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
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"

32 | P a g e
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"
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"

33 | P a g e
android:textSize="30dp" />

</AbsoluteLayout>

Main Activity .java

package com.example.database;
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);
34 | P a g e
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);
ViewAll=(Button)findViewById(R.id.ViewAll);

Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);

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

35 | P a g e
}
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");
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)

36 | P a g e
{
// 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)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);

37 | P a g e
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)

38 | P a g e
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}

Output

(i) Insert a Record (iii) Delete a Record

39 | P a g e
(ii) After Insertion (iv) After Delete

(v) Before Update (vii) View

40 | P a g e
(vi) After update (viii) View

(ix )View All

Result:
Thus the program to develop an application that makes use of database was
executed successfully.

41 | P a g e
Ex. No. 6 Develop an application that makes use of RSS Feed.

Aim:
To develop an application that makes use of RSS feed.

Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: To develop an RSS feed application internet facility must be required.
Step 4: Here, no need of layout design.
Step 5: Write the code to make use of RSS feed.
Step 6: Check the result of RSS feed.

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 | P a g e
AndroidManifest.xml

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

MainAcitivity.java

package com.example.rss;
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)

43 | P a g e
{
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
{
URL url = new URL("http://www.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"))

44 | P a g e
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}
else if(eventType==XmlPullParser.END_TAG &&
xpp.getName().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = xpp.next(); //move to next element
}

}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)

45 | P a g e
{
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;
}
}
}

46 | P a g e
Output

47 | P a g e
48 | P a g e
Result:
Thus the program to develop an application that makes use of RSS feed was executed
successfully.

49 | P a g e
Ex. No.7 (a) Implement an application that implements Multi-threading

Aim
To implement an application that implements Multi-threading.
Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: Import android.app.ProgressDialog
Step 4: In ProgressDialog class provides methods of
Step 5: setMessage(), setProgressStyle(), setMax(), show()
Step 6: Set the progress range of Progress Dialog is 0 to 10000
Step 7: Implement thread concept with exception handling
Step 8 : Run the code using AVD Emulator

/* Multi-threading Using Progress Bar*/

package com.example.progressbar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {


Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();

50 | P a g e
private long fileSize = 0;

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

public void addListenerOnButtonClick() {

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


btnStartProgress.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {

// creating progress bar dialog


progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();

//reset progress bar and filesize status


progressBarStatus = 0;
fileSize = 0;

new Thread(new Runnable() {


public void run() {
while (progressBarStatus < 100) {

51 | P a g e
// performing operation
progressBarStatus = doOperation();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

// Updating the progress bar


progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}

// performing operation if file is downloaded,


if (progressBarStatus >= 100) {

// sleeping for 1 second after operation completed


try {
Thread.sleep(1000);
} catch (InterruptedException e) {e.printStackTrace();}

// close the progress bar dialog


progressBar.dismiss();
}
}
}).start();
}//end of onClick method
});
}

52 | P a g e
// checking how much file is downloaded and updating the filesize
public int doOperation() {
//The range of ProgressDialog starts from 0 to 10000
while (fileSize <= 10000) {
fileSize++;
if (fileSize == 1000) {
return 10;
} else if (fileSize == 2000) {
return 20;
} else if (fileSize == 3000) {
return 30;
} else if (fileSize == 4000) {
return 40;
} else if (fileSize == 5000) {
return 50;
} else if (fileSize == 6000) {
return 60;
}
else if (fileSize == 7000) {
return 70;
}
else if (fileSize == 8000) {
return 80;
}
else if (fileSize == 9000) {
return 90;
}
else if (fileSize == 10000) {
return 100;
}
}//end of while
return 100;
}//end of doOperation

53 | P a g e
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Output

Result:
Thus the program to implement an application that implements Multi-threading is executed
successfully.

54 | P a g e
Ex. No. 7 (b) Implement an application that implements Multi-threading

Aim:
To implement an application that implements multi-threading.
Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: Import android.app.ProgressDialog
Step 4: In Progress Dialog class provides methods of
Step 5: setMessage(), setProgressStyle(), setMax(), show()
Step 6: Set the progress range of Progress Dialog is 0 to 10000
Step 7: Implement thread concept with exception handling
Step 8 : Run the code using AVD 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" >

<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"

55 | P a g e
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:layout_marginTop="39dp" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="68dp"
android:layout_marginLeft="49dp"
android:text="Image-1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="Image-2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button2"
android:text="Image-3" />

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

56 | P a g e
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:text="Image-4" />

</RelativeLayout>

MainActivity.java

package com.example.multithread_image;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {


ImageView img;
Button bt1,bt2, bt3, bt4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button1);
bt2= (Button) findViewById(R.id.button2);
bt3 = (Button)findViewById(R.id.button3);
bt4= (Button) findViewById(R.id.button4);

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

bt1.setOnClickListener(new View.OnClickListener()

57 | P a g e
{
@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.apj);
}
});
}
}).start();
}
});

bt2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{

58 | P a g e
@Override
public void run()
{
img.setImageResource(R.drawable.ramanujan);
}
});
}
}).start();
}
});

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

59 | P a g e
bt4.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.albert);
}
});
}
}).start();
}
});

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

60 | P a g e
Output

Result:
Thus the program to implements the multi-threading was executed successfully.

61 | P a g e
Ex. No. 8 Develop a native application that uses GPS location information.

Aim:
To develop a native application that uses GPS location Information.

Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: Design the layout using buttons.
Step 4: Update the locations of all devices in Location Control with its latitudes and
longitudes.
Step 5: Run the application using AVD Emulator.
Step 6: Now, Track the Location.

AndroidGPSTrackingActivity.java
package com.example.gpstracking;

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

public class AndroidGPSTrackingActivity extends Activity {

Button btnShowLocation;

// GPSTracker class
GPSTracker gps;

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

btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

// show location button click event


btnShowLocation.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

62 | P a g e
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);

// check if GPS enabled


if(gps.canGetLocation()){

double latitude = gps.getLatitude();


double longitude = gps.getLongitude();

// \n is for new line


Toast.makeText(getApplicationContext(), "Your Location is
- \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}

}
});
}

GPSTracker.java
package com.example.gpstracking;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status


boolean isGPSEnabled = false;

// flag for network status


boolean isNetworkEnabled = false;

// flag for GPS status

63 | P a g e
boolean canGetLocation = false;

Location location; // location


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

// The minimum distance to change Updates in meters


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

// The minimum time between updates in milliseconds


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

// Declaring a Location Manager


protected LocationManager locationManager;

public GPSTracker(Context context) {


this.mContext = context;
getLocation();
}

public Location getLocation() {


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

// getting GPS status


isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status


isNetworkEnabled = locationManager

.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {


// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager

.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services

64 | P a g e
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager

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

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

return location;
}

/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}

/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}

// return latitude
return latitude;
}

/**
* Function to get longitude
* */

65 | P a g e
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}

// return longitude
return longitude;
}

/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}

/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

// Setting Dialog Title


alertDialog.setTitle("GPS is settings");

// Setting Dialog Message


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

// On pressing Settings button


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

// on pressing cancel button


alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

// Showing Alert Message


alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

66 | P a g e
@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

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

Output:

67 | P a g e
68 | P a g e
Result:
Thus the program to track the location using GPS is executed successfully.

69 | P a g e
Ex. No. 9 Implement an application that writes data to the SD card.

Aim:
To implement an application that writes data to the SD card.

Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: Create the layout using the icons present in the widgets.
Step 4: Use Buttons and Text view to design the layout.
Step 5: Write the code to save data to SD card.
Step6 : Perform the actions.

Program:
package com.example.file;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.*;

import android.view.View;

import android.view.View.OnClickListener;

import java.io.*;

import android.content.Context;

public class MainActivity extends Activity {

Button b1,b2;

70 | P a g e
EditText e;

TextView tv;

String s;

protected FileOutputStream fout;

protected FileInputStream fin;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

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

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

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

b1.setOnClickListener(new OnClickListener(){

public void onClick(View arg0){

s=e.getText().toString();

try{

fout=openFileOutput("file 1",Context.MODE_APPEND);

fout.write(s.getBytes());

fout.close();

Toast.makeText(getBaseContext(), "saved",
Toast.LENGTH_LONG).show();

catch(Exception e){

e.printStackTrace();

71 | P a g e
});

b2.setOnClickListener(new OnClickListener(){

public void onClick(View v){

s=e.getText().toString();

try{

fin=openFileInput("file 1");

int c;

String temp=" ";

while((c=fin.read())!=-1){

temp=temp+Character.toString((char)c);

tv.setText(temp);

Toast.makeText(getBaseContext(), "file read successfully",


Toast.LENGTH_LONG).show();

catch(Exception e){

e.printStackTrace();

});

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

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

return true;

72 | P a g e
}

XML File:
<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="vec" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_centerVertical="true"
android:layout_marginLeft="32dp"
android:text="save data" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="36dp"
android:layout_toRightOf="@+id/button1"
android:text="show data" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_marginBottom="50dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >

73 | P a g e
<requestFocus />
</EditText>

</RelativeLayout>

Output:

74 | P a g e
Result:
Thus the application that writes data to the SD card is executed successfully.

75 | P a g e
Ex. No.10 Implement an application that creates an alert upon receiving a message

Aim:
To develop an application that creates an alerts upon receiving a message.

Procedure:
Step 1: File New Android Project Application
Step 2: Specify Application Name
Step 3: window then selects open perspective.
Step 4: Then select DDMS.
Step 5: i.e. WindowsOpen PerspectiveDDMS
Step6: Write the codes for message sending and receiving in separate java classess.
Step7: End the application.

Coding:
Activity Main .java
package com.example.smsnotify;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity implements OnItemClickListener {

private static MainActivity inst;


ArrayList<String> smsMessagesList = new ArrayList<String>();
ListView smsListView;
ArrayAdapter arrayAdapter;

76 | P a g e
public static MainActivity instance() {
return inst;
}

@Override
public void onStart() {
super.onStart();
inst = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smsListView = (ListView) findViewById(R.id.SMSList);
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, smsMessagesList);
smsListView.setAdapter(arrayAdapter);
smsListView.setOnItemClickListener(this);

refreshSmsInbox();
}

public void refreshSmsInbox() {


ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor =
contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
"\n" + smsInboxCursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
} while (smsInboxCursor.moveToNext());
}

public void updateList(final String smsMessage) {


arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
String[] smsMessages = smsMessagesList.get(pos).split("\n");
String address = smsMessages[0];
String smsMessage = "";
for (int i = 1; i < smsMessages.length; ++i) {
smsMessage += smsMessages[i];
}

String smsMessageStr = address + "\n";


smsMessageStr += smsMessage;
Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();

77 | P a g e
} catch (Exception e) {
e.printStackTrace();
}
}
}

SmsBroadcastReceiver .java
package com.example.smsnotify;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsBroadcastReceiver extends BroadcastReceiver {

public static final String SMS_BUNDLE = "pdus";

public void onReceive(Context context, Intent intent) {


Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

String smsBody = smsMessage.getMessageBody().toString();


String address = smsMessage.getOriginatingAddress();

smsMessageStr += "SMS From: " + address + "\n";


smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();

//this will update the UI with message


MainActivity inst = MainActivity.instance();
inst.updateList(smsMessageStr);
}
}
}

ActivityMain .xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/MainLayout"

78 | P a g e
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SMS Inbox"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />

<ListView android:id="@+id/SMSList"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="5dp" />

</LinearLayout>

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


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SMS Inbox"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />

<ListView android:id="@+id/SMSList"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="5dp" />

</LinearLayout>

Android Manifest .xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsnotify"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_SMS" />

79 | P a g e
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smsnotify.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>

80 | P a g e
Output:
SMS Notification Page:

81 | P a g e
SMS Inbox Page:

Result:
Thus the program to create an alert upon receiving a message is executed
successfully.

82 | P a g e
Ex. No.11 Write a mobile application that creates alarm clock

Aim:

To develop a Android Application that creates Alarm Clock.

Procedure:

Step 1: File New Android Project Application


Step 2: Specify Application Name
Step 3: Create a layout using the widgets i.e. use Time picker.
Step 4: Write the code to set alarm.
Step 5: Run the code as Android Application.
Step 6: Check the result.

MainActivity.java
package com.example.alaram_new;

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

import android.os.Bundle;
import android.app.Activity;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

TimePicker tp1;
Button save;
int hour,min,SystemHour,SystemMin,sum,sub,flag=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

83 | P a g e
tp1 = (TimePicker)findViewById(R.id.timePicker1);
save = (Button)findViewById(R.id.button1);

save.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
hour = tp1.getCurrentHour();
min = tp1.getCurrentMinute();
flag=0;
while(flag!=1){
Time time = new Time();
time.setToNow();
SystemHour = time.hour;
SystemMin = time.minute;
sum = SystemHour - hour;
sub = SystemMin - min;

if(sum == 0)
{if(sub == 0)
{
Toast.makeText(MainActivity.this,"Alarm ringing: Wake up !",
Toast.LENGTH_LONG).show();
flag=1; } }
}

}
});

}
}

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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TimePicker
android:id="@+id/timePicker1"

84 | P a g e
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="29dp"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_centerVertical="true"
android:layout_marginLeft="59dp"
android:text="Save" />

</RelativeLayout>

85 | P a g e
Output:

Result:
Thus the program to create an alarm clock is executed successfully.

86 | P a g e
87 | P a g e

Das könnte Ihnen auch gefallen