Professional Documents
Culture Documents
No: 1 Android Application that uses GUI components, Font and Colours
Aim:
To develop a Simple Android Application that uses GUI components, Font and Colours.
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
1 |
Then select the Minimum SDK as shown below and clickNext.
Finally clickFinish.
2 |
It will take some time to build and load theproject.
3 |
Now click on Text as shownbelow.
Then delete the code which is there and type the code as given below
4 |
Code for Activity_main.xml:
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>
5 |
So now the designing part iscompleted.
Then delete the code which is there and type the code as givenbelow.
6 |
Code for
MainActivity.java:package
com.example.exno1; import
android.graphics.Color;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
int ch=1;
float font=30;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
;
final TextView t= (TextView)
findViewById(R.id.textView); Button b1= (Button)
findViewById(R.id.button1); b1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
v) { t.setTextSize(font);
font = font +
5; if (font ==
50) font = 30;
}
});
Button b2= (Button) findViewById(R.id.button2);
b2.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
v) { switch (ch) {
case 1:
t.setTextColor(Color.RED);
7 |
break;
case 2:
t.setTextColor(Color.GREE
N); break;
case 3:
t.setTextColor(Color.BLU
E); break;
case 4:
t.setTextColor(Color.CYA
N); break;
case 5:
t.setTextColor(Color.YELLO
W); break;
case 6:
t.setTextColor(Color.MAGENT
A); break;
}
ch++;
if (ch ==
7) ch =
1;
}
});
}
}
So now the Coding part is alsocompleted.
Now run the application to see theoutput.
8 |
Output:
9 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Thus, a Simple Android Application that uses GUI components, Font and Colours
is developed and executed successfully.
10 |
Ex. No: 2 Develop an application that uses Layout Managers and event listeners.
Aim:
Procedure:
Creating a new project:
Open Android Studio and then click on File -> New -> Newproject.
11 |
Then select the Minimum SDK as shown below and clickNext.
12 |
Then select the Empty Activity and click Next.
Finally clickFinish.
13 |
Creating Second Activity for the Android Application:
14 |
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent
"
android:layout_height="match_parent" tools:context=".MainActivity">
</LinearLayout>
<GridLayoutandroid:id="@+id/gridLayout"
android:layout_width="match_parent" android:layout_height="match_parent" android:lay
out_marginTop="100dp" android:layout_marginBottom="200dp"android:columnCount="2"
android:rowCount="3">
<TextView
android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_heigh
15 |
t="wrap_content"
android:layout_margin="10dp" android:layout_row="2" android:layout_column="0"
android:text="Dept" android:textSize="20sp"android:gravity="center"/>
android:layout_column="1" android:spinnerMode="dropdown"/>
</GridLayout>
</RelativeLayout>
16 |
Then delete the code which is there and type the code as givenbelow.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.devang.exno2.SecondActivity"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
17 |
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
18 |
Java Coding for the Android Application:
Then delete the code which is there and type the code as givenbelow.
packagecom.example.second2;
importandroid.content.Intent;
importandroidx.appcompat.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
importandroid.widget.Spinner;
19 |
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s= (Spinner) findViewById(R.id.spinner);
//Creating Adapter for Spinner for adapting the data from array to
Spinner
ArrayAdapter adapter= new
ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,dept_ar
ray);
s.setAdapter(adapter);
startActivity(i);
}
});
}
}
20 |
Java Coding for Second Activity:
Then delete the code which is there and type the code as givenbelow.
Code forSecondActivity.java:
packagecom.example.exno2;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
TextView t1,t2,t3;
21 |
String name,reg,dept;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1= (TextView)
findViewById(R.id.textView1); t2=
(TextView) findViewById(R.id.textView2);
t3= (TextView)
findViewById(R.id.textView3);
Intent i = getIntent();
//Getting the Values from First Activity using the Intent received
name=i.getStringExtra("name_key");
reg=i.getStringExtra("reg_key");
dept=i.getStringExtra("dept_key");
t1.setText(name);
t2.setText(reg);
t3.setText(dept);
22 |
So now the Coding part of Second Activity is alsocompleted.
Now run the application to see theoutput.
Output:
23 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Thus, a Simple Android Application that uses Layout Managers and Event Listeners
is developed and executed successfully.
24 |
Ex.No:3. Develop a native calculatorapplication.
Aim:
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
25 |
Then select the Minimum SDK as shown below and clickNext.
Finally clickFinish.
26 |
It will take some time to build and load theproject.
27 |
Now click on Text as shownbelow.
Then delete the code which is there and type the code as given below
28 |
Code for Activity_main.xml:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
29 |
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:text="+"
android:textSize="30sp"/>
<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:text="-"
android:textSize="30sp"/>
<Button
android:id="@+id/Mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:text="*"
android:textSize="30sp"/>
<Button
android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:text="/"
android:textSize="30sp"/>
30 |
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is" android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
31 |
Java Coding for the Android Application:
Then delete the code which is there and type the code as givenbelow.
package com.example.devang.exno3;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; import
android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener; import
android.widget.Button;
import android.widget.EditText; import
android.widget.TextView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
32 |
//Referring the Views
Num1 = (EditText) findViewById(R.id.editText1); Num2 =
(EditText) findViewById(R.id.editText2); Add =
(Button)findViewById(R.id.Add);
Sub = (Button) findViewById(R.id.Sub); Mul =
(Button) findViewById(R.id.Mul); Div =
(Button)findViewById(R.id.Div);
Result = (TextView) findViewById(R.id.textView);
// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}
@Override
public void onClick (View v)
{
// defines the button that has been clicked and performs the corresponding operation
33 |
// write operation into oper, we will use it later for output switch (v.getId())
{
case R.id.Add: oper =
"+";
result = num1 + num2; break;
case R.id.Sub: oper =
"-";
result = num1 - num2; break;
case R.id.Mul: oper =
"*";
result = num1 * num2; break;
case R.id.Div: oper =
"/";
result = num1 / num2; break;
default:
break;
}
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}
34 |
Output:
35 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Thus, a Simple Android Application for Native Calculator is developed
and executed successfully.
36 |
Ex.No:4. Write an application that draws basic graphical primitives onthe screen
Aim:
To develop a Simple Android Application that draws basic Graphical Primitives on the
screen.
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
37 |
Then select the Minimum SDK as shown below and clickNext.
Finally clickFinish.
38 |
It will take some time to build and load theproject.
39 |
Now click on Text as shownbelow.
Then delete the code which is there and type the code as given below
40 |
Code for Activity_main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/imageView"/>
</RelativeLayout>
41 |
Java Coding for the Android Application:
Then delete the code which is there and type the code as givenbelow.
packagecom.example.exno4;
android.graphics.Bitmap; import
android.graphics.Canvas; import
android.graphics.Color;
importandroid.graphics.Paint;
importandroid.widget.ImageView;
publicclassMainActivity extendsActivity
@Override
publicvoidonCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating a Bitmap
42 |
Bitmap bg = Bitmap.createBitmap(720, 1280,
Bitmap.Config.ARGB_8888);
(ImageView) findViewById(R.id.imageView);
i.setBackgroundDrawable(newBitmapDrawable(bg));
new Canvas(bg);
//Creating the Paint Object and set its color & TextSize Paint paint = newPaint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);
paint);
43 |
So now the Coding part is alsocompleted.
Now run the application to see theoutput.
Output:
44 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Thus, aAndroid Application an application that draws basic graphical
primitives on the screen is developed and executed successfully.
45 |
Ex.No: 5. Develop an application that makes use of database.
Aim:
To develop a Simple Android Application that makes use of Database
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
46 |
Then select the Empty Activity and clickNext.
Finally clickFinish.
47 |
It will take some time to build and load theproject.
48 |
Now click on Text as shownbelow.
Then delete the code which is there and type the code as given below
49 |
Code for Activity_main.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp" android:layout_y="110dp"
android:text="Enter Rollno:" android:textSize="20sp"
/>
<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp" android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp" android:layout_y="160dp"
50 |
android:text="Enter Name:"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp" android:layout_y="150dp"
android:inputType="text" android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp" android:layout_y="210dp"
android:text="Enter Marks:" android:textSize="20sp"
/>
<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
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"
51 |
android:textSize="30dp" />
<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp" android:layout_y="300dp"
android:text="Delete" android:textSize="30dp" />
<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp" android:layout_y="400dp"
android:text="Update" android:textSize="30dp" />
<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp" android:layout_y="400dp"
android:text="View" 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"
52 |
android:textSize="30dp" />
</AbsoluteLayout>
53 |
Java Coding for the Android Application:
Then delete the code which is there and type the code as givenbelow.
package com.example.exno5;
import android.app.Activity;
import android.app.AlertDialog.Builder;
importandroid.content.Context;
importandroid.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
importandroid.os.Bundle;
importandroid.view.View;
import android.view.View.OnClickListener; import
android.widget.Button;
import android.widget.EditText;
Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
54 |
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);
55 |
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)
{
// 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() +
56 |
"' 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);
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)
{
57 |
showMessage("Error", "No records found"); return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n"); buffer.append("Name:
"+c.getString(1)+"\n"); buffer.append("Marks:
"+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true); builder.setTitle(title);
builder.setMessage(message); builder.show();
}
public voidclearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}
58 |
Output:
59 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
60 |
Ex.No:6. Develop an application that makes use of RSSFeed
Aim:
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
61 |
Then select the Minimum SDK as shown below and clickNext.
Finally clickFinish.
62 |
It will take some time to build and load theproject.
63 |
Now click on Text as shownbelow.
Then delete the code which is there and type the code as given below
64 |
Code for Activity_main.xml:
<?xmlversion="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>
65 |
Adding permissions in Manifest for the Android Application:
<?xmlversion="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.exno6">
<uses-permissionandroid:name="android.permission.INTERNET"/>
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".MainActivity">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
66 |
</application>
</manifest>
Then delete the code which is there and type the code as givenbelow.
packagecom.example.exno6;
android.content.Intent; importandroid.net.Uri;
importandroid.os.AsyncTask; import
android.os.Bundle;
importandroid.view.View;
android.widget.ListView;
importorg.xmlpull.v1.XmlPullParser;
org.xmlpull.v1.XmlPullParserFactory; importjava.io.IOException;
importjava.io.InputStream;
67 |
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.util.ArrayList;
importjava.util.List;
publicclassMainActivity extendsListActivity
List headlines;
List links;
@Override
protectedvoidonCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
newMyAsyncTask().execute();
classMyAsyncTask extendsAsyncTask<Object,Void,ArrayAdapter>
@Override
newArrayList();
try
68 |
URL url = newURL("http://www.codingconnect.net/feed");
XmlPullParserFactory factory =
XmlPullParserFactory.newInstance();
factory.newPullParser();
=false;
if (eventType ==XmlPullParser.START_TAG)
if(xpp.getName().equalsIgnoreCase("item"))
insideItem = true;
elseif(xpp.getName().equalsIgnoreCase("title"))
if (insideItem) headlines.add(xpp.nextText());//extract
the headline
69 |
elseif(xpp.getName().equalsIgnoreCase("link"))
if(insideItem)
else
if(eventType==XmlPullParser.END_TAG&&xpp.getName().equalsIgnoreCase("item"))
insideItem=false;
catch (MalformedURLExceptione)
e.printStackTrace();
catch (XmlPullParserExceptione)
e.printStackTrace();
catch (IOExceptione)
e.printStackTrace();
70 |
}
returnnull;
protectedvoidonPostExecute(ArrayAdapter adapter)
setListAdapter(adapter);
@Override
try
returnurl.openConnection().getInputStream();
catch (IOExceptione)
71 |
{
returnnull;
Output:
72 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Thus Android Application that makes use of RSS Feed is developed and
executed successfully.
73 |
Ex.No:7 Implement an application that implementsMulti-threading
Aim:
To Implement an application that implements multi-threading
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
Finally clickFinish.
Then delete the code which is there and type the code as givenbelow.
74 |
Code for Activity_main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_margin="50dp"
android:layout_gravity="center"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load Image 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load image 2" />
</LinearLayout>
75 |
Java Coding for the Android Application:
Click on app -> java -> com.example.exno7 ->MainActivity.
Then delete the code which is there and type the code as givenbelow.
package com.example.exno7;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{
ImageView img;
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2= (Button) findViewById(R.id.button2);
img =(ImageView)findViewById(R.id.imageView);
bt1.setOnClickListener(newView.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
76 |
img.setImageResource(R.drawable.india1);
}
});
}
}).start();
}
});
bt2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
img.post(new Runnable()
{
@Override
public void run()
{
img.setImageResource(R.drawable.india2);
}
});
}
}).start();
}
});
}
}
Note: Before Running the Application, Copy the Images given below and
Paste it in “app -> res -> drawable” by pressing “right click mouse button
on drawable” and selecting the “Paste” option
77 |
india2 india1
OUTPUT:
78 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
79 |
Ex.No:8 Develop a native application that uses GPS locationinformation
Aim:
To Develop a native application that uses GPS location information
Procedure:
Code for Mainactivity.java:
package com.odyessy.locaz;
importandroidx.annotation.NonNull;
importandroidx.appcompat.app.AppCompatActivity;
importandroidx.core.app.ActivityCompat;
importandroid.Manifest;
importandroid.content.pm.PackageManager;
importandroid.location.Address;
importandroid.location.Geocoder;
importandroid.location.Location;
importandroid.os.Bundle;
importandroid.text.Html;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
importcom.google.android.gms.location.FusedLocationProviderClient;
importcom.google.android.gms.location.LocationServices;
importcom.google.android.gms.tasks.OnCompleteListener;
importcom.google.android.gms.tasks.Task;
importjava.io.IOException;
importjava.util.List;
importjava.util.Locale;
publicclassMainActivityextendsAppCompatActivity{
80 |
FusedLocationProviderClientfusedLocationProviderClient;
Button btLocation;
TextViewtextView1,textView2;
@Override
protected voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btLocation =findViewById(R.id.bt_location);
textView1 =findViewById(R.id.text_view1);
textView2 =findViewById(R.id.text_view2);
fusedLocationProviderClient=
LocationServices.getFusedLocationProviderClient(this);
btLocation.setOnClickListener(newView.OnClickListener(){
@Override
publicvoid onClick(Viewv){
if
(ActivityCompat.checkSelfPermission(MainActivity.this
,Manifest.permission.ACCESS_FINE_LOCATION)
==PackageManager.PERMISSION_GRANTED){
getLocation();
} else {
ActivityCompat.requestPermissions(MainActivity.this
, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},44);
}
}
});
privatevoidgetLocation(){
if(ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)!=
PackageManager.PERMISSION_GRANTED&&
81 |
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION)!=
PackageManager.PERMISSION_GRANTED){
//TODO:Considercalling
// ActivityCompat#requestPermissions
// here to requestthe missing permissions,and then
overriding
// publicvoid onRequestPermissionsResult(int
requestCode,String[]permissions,
// int[]
grantResults)
//to handlethe case wherethe usergrantsthe
permission.See the documentation
//forActivityCompat#requestPermissionsformore
details.
return;
}
fusedLocationProviderClient.getLastLocation().addOnCompleteListener
(newOnCompleteListener<Location>(){
@Override
publicvoid onComplete(@NonNullTask<Location>task){
Location location =task.getResult();
if(location !=null){
try {
Geocodergeocoder=new
Geocoder(MainActivity.this
,Locale.getDefault());
List<Address> addresses =
geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),1
);
textView1.setText(Html.fromHtml(
"<fontcolor='#6200EE'><b>Latitude
:</b><br></font>"
+
addresses.get(0).getLatitude()
));
textView2.setText(Html.fromHtml(
82 |
"<fontcolor='#6200EE'><b>Longitude
:</b><br></font>"
+
addresses.get(0).getLongitude()
));
android:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_location"
android:text="GetLocation"/>
<LinearLayout
android:layout_width="match_parent"
83 |
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="32dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/text_view1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/text_view2"/>
</LinearLayout>
<!--LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/text_view3"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/text_view4"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/text_view5"/-->
</LinearLayout>
84 |
OUTPUT:
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Thus Android Application that uses GPS location informationis developed and executed
successfully.
85 |
Ex.No:9 Implement an application that writes data to the SD card.
Aim:
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
86 |
Then select the Empty Activity and clickNext.
Finally clickFinish.
87 |
It will take some time to build and load theproject.
88 |
Now click on Text as shownbelow.
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical">
89 |
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" android:textSize="30dp"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" android:text="Write
Data" android:textSize="30dp"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" android:text="Read
data" android:textSize="30dp"/>
<Button
android:id="@+id/button3"
90 |
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" android:text="Clear"
android:textSize="30dp"/>
</LinearLayout>
<?xmlversion="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.exno9">
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses- permission>
91 |
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".MainActivity">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
So now the Permissions are added in theManifest.
Code forMainActivity.java:
?
packagecom.example.exno9;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.view.View;
android.widget.EditText;
importandroid.widget.Toast;
92 |
importjava.io.BufferedReader;
importjava.io.File;
java.io.FileOutputStream;
importjava.io.InputStreamReader;
publicclassMainActivity extendsAppCompatActivity
EditText e1;
protectedvoidonCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button3);
write.setOnClickListener(newView.OnClickListener()
@Override
publicvoidonClick(View v)
93 |
{
fout.close();
Toast.makeText(getBaseContext(),"Data Written in
SDCARD",Toast.LENGTH_LONG).show();
catch (Exceptione)
Toast.makeText(getBaseContext(),e.getMessage(),Toast.LE
NGTH_LONG).show();
});
read.setOnClickListener(newView.OnClickListener()
@Override
publicvoidonClick(View v)
String message;
94 |
try
buf += message;
e1.setText(buf); br.close();
fin.close();
catch (Exceptione)
});
clear.setOnClickListener(newView.OnClickListener()
@Override
publicvoidonClick(View v)
e1.setText("");
});
95 |
OUTPUT:
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Aim:
Procedure:
Open Android Studio and then click on File -> New -> Newproject.
97 |
Then select the Minimum SDK as shown below and clickNext.
98 |
Finally clickFinish.
99 |
Designing layout for the Android Application:
100 |
Code for Activity_main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Message"
android:textSize="30sp"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:singleLine="true"
android:textSize="30sp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
101 |
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>
</LinearLayout>
102 |
Code for MainActivity.java:
packagecom.example.exno10;
importandroid.app.Notification;
importandroid.app.NotificationManager; import
android.app.PendingIntent; importandroid.content.Intent;
importandroid.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
importandroid.view.View;
android.widget.EditText;
publicclassMainActivity extendsAppCompatActivity
Button notify;
EditText e;
@Override
protectedvoidonCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.editText);
103 |
notify.setOnClickListener(newView.OnClickListener()
@Override
publicvoidonClick(View v)
=
PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, noti);
});
104 |
OUTPUT:
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
Result:
Thus Android Application that creates an alert upon receiving a message
is developed and executed successfully
105 |
Ex.No:11. Write a mobile application that creates alarmclock
Aim:
Procedure:
Creating a New project:
Open Android Studio and then click on File -> New -> Newproject.
106 |
Then select the Minimum SDK as shown below and clickNext.
107 |
Finally clickFinish.
108 |
Creating Second Activity for the Android Application:
109 |
Then delete the code which is there and type the code as givenbelow.
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ToggleButton
110 |
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:checked="false"
android:onClick="OnToggleClicked"/>
</LinearLayout>
111 |
Now change the activity tag to receiver tag in the AndroidManifest.xml
file as shownbelow
<?xmlversion="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.exno11">
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".MainActivity">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<receiverandroid:name=".AlarmReceiver">
</receiver>
</application>
112 |
</manifest>
packagecom.example.exno11;
import android.app.AlarmManager;
importandroid.app.PendingIntent; import
android.content.Intent;
importandroid.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
importandroid.view.View;
importandroid.widget.TimePicker;
importandroid.widget.Toast;
importandroid.widget.ToggleButton;
importjava.util.Calendar;
publicclassMainActivity extendsAppCompatActivity
TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
113 |
protectedvoidonCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
(AlarmManager) getSystemService(ALARM_SERVICE);
publicvoidOnToggleClicked(View view)
longtime;
if (((ToggleButton)view).isChecked())
calendar.set(Calendar.HOUR_OF_DAY,
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
time=(calendar.getTimeInMillis()-
(calendar.getTimeInMillis()%60000));
if(System.currentTimeMillis()>time)
if (calendar.AM_PM ==0)
114 |
time = time + (1000*60*60*12); else
else
alarmManager.cancel(pendingIntent);
Then delete the code which is there and type the code as givenbelow.
?
packagecom.example.exno11;
115 |
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.media.Ringtone;
import android.media.RingtoneManager;
importandroid.net.Uri;
importandroid.widget.Toast;
publicclassAlarmReceiver extendsBroadcastReceiver
@Override
Uri alarmUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri ==null)
alarmUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone.play();
116 |
So now the Coding part of Alarm Receiver is alsocompleted.
Now run the application to see theoutput.
OUTPUT :
117 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
RESULT:
118 |
Ex.No:12. Develop Mobile application to send an email
Aim:
Procedure:
Step 1. activity_main.xml
activity_main.xml contains a Relative Layout which contains three Edit
texts for receiver mail id, other for the subject of the mail and last one for
the body of the email and three TextViews for the label and a button for
starting intent or sending mail:
activity_main.xml
<!--Relative Layout-->
<RelativeLayout
xmlns:androclass="http://schemas.android.com/apk/res/a
ndroid"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
119 |
android:layout_marginTop="18dp"
/>
120 |
<TextView
android:id="@+id/textView2"
android:textColor="#0F9D58"
android:text="Email Subject:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/textView3"
android:textColor="#0F9D58"
android:text="Email Body:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
/>
Step 2. MainActivity.java
121 |
In Main activity Intent object is created and its action is defined to
ACTION_SEND to send email, with Intent three extra fields are also added
using putExtra function.These fields are:
Email of receiver
Subject of email
Body of email
MainActivity.java
package com.geeksforgeeks.phonecall;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.net.Uri;
import android.widget.Button;
@Override
protected void onCreate(Bundle
122 |
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onClick(View view)
{
String emailsend =
sendto.getText().toString();
String emailsubject =
subject.getText().toString();
String emailbody =
body.getText().toString();
123 |
intent.putExtra(Intent.EXTRA_SUBJECT,
emailsubject);
intent.putExtra(Intent.EXTRA_TEXT,
emailbody);
OUTPUT :
124 |
MAXIMUM MARKS
CRITERIA
MARKS OBTAINED
PROGRAM AND
10
EXECUTION
OUTPUT 5
VIVA 5
TOTAL 25
RESULT
125 |