Beruflich Dokumente
Kultur Dokumente
Index
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;
@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
<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;
@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
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));
}
25 | P a g e
private class myView extends View{
@Override
protected void onDraw(Canvas canvas) {
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
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;
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
<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>
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;
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);
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
39 | P a g e
(ii) After Insertion (iv) After Delete
40 | P a g e
(vi) After update (viii) View
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
<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;
@Override
protected void onCreate(Bundle savedInstanceState)
43 | P a g e
{
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
}
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);
}
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
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;
50 | P a g e
private long fileSize = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
@Override
public void onClick(View v) {
51 | P a g e
// performing operation
progressBarStatus = doOperation();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
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;
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;
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@Override
public void onClick(View arg0) {
62 | P a g e
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
}
});
}
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;
63 | P a g e
boolean canGetLocation = false;
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
.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);
@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;
Button b1,b2;
70 | P a g e
EditText e;
TextView tv;
String s;
@Override
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(){
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(){
s=e.getText().toString();
try{
fin=openFileInput("file 1");
int c;
while((c=fin.read())!=-1){
temp=temp+Character.toString((char)c);
tv.setText(temp);
catch(Exception e){
e.printStackTrace();
});
@Override
// 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;
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 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];
}
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;
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>
<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>
<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" />
</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:
Procedure:
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;
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