AP Lab Manual Updated
AP Lab Manual Updated
Lab Manual
on
“Android Programming”
Subject Code: 18CSL76
Staffs - In – Charge
2020-21
A. LAORATORY OVERVIEW
Degree: BE Program: CS & E
Semester: 6 Academic Year: 2020-21
Laboratory Title: Android Programming Laboratory Laboratory Code: 18CSL76
L-T-P-S: 0-0-1-0 Duration of SEE: 3 Hrs
CIE Marks: 50 SEE Marks: 50
B. DESCRIPTION
c
There are no prerequisites for learning Android but I would like to mention that you will be coding in
Java and XML. It would be helpful if you could learn just the core Java concepts.
2. BASE COURSE
• Android Programming Laboratory (18CSL76)
3. COURSE OUTCOMES
At the end of the course, the student will be able to;
CO1: Create, test and debug Android application by setting up Android development
environment.
CO2: Implement adaptive, responsive user interfaces that work across a wide range of devices.
CO3: Infer long running tasks and background work in Android applications.
CO4: Demonstrate methods in storing, sharing and retrieving data in Android applications.
CO5: Infer the role of permissions and security for Android applications.
4. RESOURSES REQUIRED
• Hardware resources
◦ Desktop PC
◦ Microsoft Windows 7/8/10 (32 or 64 bit)
◦ 2 GB RAM minimum, 8 GB recommended
◦ 2 GB of available disk space minimum, 4 GB recommended (500 MB for IDE + 1.5
GB for Android SDK and emulator system image)
◦ 1280 x 800 minimum screen resolution
• Software resources
All the required tools to develop Android applications are freely available and can be downloaded
from the Web. Following is the list of software you will need before you start your Android.
List of Programs
Course objectives:
1) To learn and acquire art of Android programming.
2) To configure initial application, run in emulator.
3) Understand and implement Android’s advanced User interface functions, audio video
applications
4) Create, modify and query on SQlite database.
5) Present different ways of sharing data through the use of services.
Sl. No. Programs
1. i) Create an application to design a Visiting Card. The Visiting card should have a
company logo at the top right corner. The company name should be displayed in Capital
letters, aligned to the center. Information like the name of the employee, job title, phone
number, address, email, fax and the website address is to be displayed. Insert a horizontal
line between the job title and the phone number.
ii) Develop a simple application with one EditText so that the user can write some text in
it. Create a button called “Convert Text to Speech” that converts the user input text into
voice.
2. Write a program to create an Activity to read Employee Details (EmpId, Name, Age,
Address) from user and store to database and create a menu with menu item (Show
Details) on pressing menu details it must go to another activity with employee id search
box and search button and display the employee details on the screen.
3. Write a program to create an activity with a text box and three buttons (save, open and
create) open must allow to browse the text file from sdcard and must display the contents
of the file on textbox, save button must save the contents of text box to file, create button
must allow file user to create a new file and save the entered contents of the textbox.
4. Write a program to create an activity with two text boxes (date /time and note contents).
Create a content provider to store the date and time and note contents to the database.
Create another program with a Button (Fetch Today Notes) on press must access the note
provider and display the notes stored for today’s date.
5. Write a program to create an activity with two buttons start and stop. On pressing start
button the program must start the counter and must keep on counting until stop button is
pressed.
6. Create a program to receive the incoming SMS to the phone and put a notification on
screen, on clicking the notification it must display sender number and message content
on screen.
7. Write a program to create a service that will put a notification on the screen every 5
seconds.
8. Create an .aidl service to do add, subtraction and multiplication and create another
application with two buttons to read the inputs and three button add,subtract and multiply
to call add, subtract and multiply operation on .aidl service.
9. Create an activity like a phone dialer with (1,2,3,4,5,6,7,8,9,0,*,#) buttons including call,
save and delete buttons. On pressing the call button, it must call the phone number and
on pressing the save button it must save the number to the phone contacts.
10. Create a file of JSON type with values for city_name, Latitude, Longitude, Temperature
and Humidity. Develop an application to create an activity with button to parse the JSON
file which when clicked should display the data in the textview.
How to Install and Set up Android Studio on Windows?
Step 3: After the downloading has finished, open the file from downloads and run it. It will prompt the
following dialog box.
Click on next. In the next prompt, it’ll ask for a path for installation. Choose a path and hit next.
Step 4: It will start the installation, and once it is completed, it will be like the image shown below.
Click on next.
Step 5: Once “Finish” is clicked, it will ask whether the previous settings need to be imported [if the
Android studio had been installed earlier], or not. It is better to choose the ‘Don’t import Settings option’.
Step 7: After it has found the SDK components, it will redirect to the Welcome dialog box.
Click on Next.
Choose Standard and click on Next. Now choose the theme, whether the Light theme or
the Dark one.
The light one is called the IntelliJ theme whereas the dark theme is called Darcula. Choose as
required.
Click on the Next button.
To run your first android app in Android Studio you may refer to Running your first Android
app.
Create your First Android Application
"Language", select "Java" 🡪 Leave the "Minimum API Level" and the rest to default 🡪Finish.
It could take a few minutes to set up your first app. Watch the "progress bar" at the bottom status bar.
Once the progress bar indicates completion, a hello-world app is created by default.
To run your Android application under the emulator, you need to first create an Android Virtual Devices
(AVD). An AVD models a specific device. You can create AVDs to emulate different android devices
choose "2.7 QVGA" (the smallest device available - you can try a bigger device later) 🡪 Next.
Step 4: In "System Image: Recommended " 🡪 Select the version with the highest API level
Step 5: In "AVD Name", enter "2.7 QVGA API 27" (default) 🡪Finish.
Step 1: Select the "Run" menu 🡪 "Run app" 🡪 Under "Available Virtual Devices", select "2.7 QVGA
Step 2: It may take a few MINUTES to fire up the app on the emulator. You first see a Google logo 🡪
then "Android" 🡪 then the "wallpaper" 🡪 then the "Hello, world!" message.
Step 3: DO NOT CLOSE THE EMULATOR, as it really takes a long time to start. You could always
re-run the app (or run a new app) on the same emulator. Try re-run the app by selecting "Run"
to the center. Information like the name of the employee, job title, phone number, address,
email, fax and the website address is to be displayed. Insert a horizontal line between the job
activity_main.xml
-<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="38dp"
android:textColor="@color/colorAccent"
android:text=" GOOGLE"
android:id="@+id/textView"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/imageView"
app:srcCompat="@drawable/logo"
android:layout_marginTop="4dp"/>
<View
android:layout_height="4dp"
android:layout_width="wrap_content"
android:id="@+id/view"
android:layout_marginTop="103dp"
android:background="@color/colorAccent"
android:layout_marginBottom="498dp"/>
</LinearLayout>
<TextView
android:layout_height="30dp"
android:layout_width="81dp"
android:text="Dr.B R Ambedkar"
android:id="@+id/textView3"
android:layout_marginBottom="16dp"
android:textStyle="bold"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="The greatest Leader of India"
android:id="@+id/textView4"
android:textStyle="bold"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Ph:Number: 2019201923"
android:id="@+id/textView6"
android:textStyle="bold"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Address: Maharashtra"
android:id="@+id/textView7"
android:textStyle="bold"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Email: ambedkar@gmail.com"
android:id="@+id/textView8"
android:textStyle="bold"/>
</LinearLayout>
OUTPUT
Program -1
ii) Develop a simple application with one EditText so that the user can write some text in
it. Create a button called “Convert Text to Speech” that converts the user input text
into voice.
activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT TO SPEECH APPLICATION"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<EditText
android:id="@+id/txt_input"
android:layout_width="237dp"
android:layout_height="177dp"
android:layout_marginStart="100dp"
android:layout_marginTop="209dp"
android:layout_marginEnd="77dp"
android:layout_marginBottom="437dp"
android:inputType="textMultiLine”/>
<Button
android:id="@+id/btn_txt2spch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="399dp"
android:layout_marginEnd="51dp"
android:layout_marginBottom="284dp"
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.texttospeech;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
TextToSpeech t1;
EditText txtinput;
Button txttospeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtinput = findViewById(R.id.txt_input);
txttospeech = findViewById(R.id.btn_txt2spch);
txttospeech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String tospeak = txtinput.getText().toString();
Toast.makeText(getBaseContext(),tospeak,Toast.LENGTH_SHORT).show();
t1.speak(tospeak,TextToSpeech.QUEUE_FLUSH, null);
}
});
}
OUTPUT
Program -2
Write a program to create an Activity to read Employee Details (EmpId, Name, Age, Address)
from user and store to database and create a menu with menu item (Show Details) on pressing
menu details it must go to another activity with employee id search box and search button and
display the employee details on the screen.
activity_main.xml
<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"
android:orientation="vertical
" android:gravity="center">
<TextView
android:layout_width="match_paren"
android:layout_height="wrap_conten”
android:text="Employee_id"/>
<EditText
android:layout_width="match_paren"
android:layout_height="wrap_conten"
android:id="@+id/txt_id"/>
<TextView
android:layout_width="match_paren"
android:layout_height="wrap_conten"
android:text="Employee_name"/>
<EditText
android:layout_width="match_paren"
android:layout_height="wrap_conten"
android:id="@+id/txt_name"/>
<TextView
android:layout_width="match_paren"
android:layout_height="wrap_conten"
android:text="Employee_age"/>
<EditTextandroid:layout_width="match_parent"
android:layout_height="wrap_conten"
android:id="@+id/txt_age"/>
<TextView
android:layout_width="match_paren"
android:layout_height="wrap_conten"
android:text="Employee_address"/>
<EditText
android:layout_width="match_paren"
android:layout_height="wrap_conten"
android:id="@+id/txt_address"/>
<LinearLayout
android:layout_width="wrap_conten"
android:layout_height="wrap_conten"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_conten"
android:layout_height="wrap_conten"
android:text="Submit"
android:layout_gravity="center"
android:id="@+id/btn_submit"/>
<Button
android:layout_width="wrap_conten"
android:layout_height="wrap_conten"
android:text="Search"
android:layout_gravity="center"
android:id="@+id/btn_search"/>
</LinearLayout>
</LinearLayout>
Search.xml
<EditText
android:layout_width="match_pare”
android:layout_height="wrap_content”
android:id="@+id/txt_empid"/>
<Button
android:layout_width="wrap_conten
android:layout_height="wrap_content”
android:text="Search"
android:layout_gravity="center"
android:id="@+id/txt_search"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text view"
android:id="@+id/txt_display"/>
</LinearLayout>
Main_activity.java
package com.example.employeedetails;
import android.os.Bundle;
import android.app.Activity;
import ndroid.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import ndroid.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtid=(EditText)findViewById(R.id.txt_id);
txtname=(EditText)findViewById(R.id.txt_name);
txtage=(EditText)findViewById(R.id.txt_age);
txtaddress=(EditText)findViewById(R.id.txt_address);
btnsubmit=(Button)findViewById(R.id.btn_submit);
btnsubmit.setOnClickListener(this);
btnsearch=(Button)findViewById(R.id.btn_search);
btnsearch.setOnClickListener(this);
}
@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;
} @Override
public void onClick(View v) {
// TODO
Auto-
generated
method stub
Toast.make
Text(this,
"buttonclic
ked",
15000).show
();
if(v.equals(btnsubmit))
{
String
sid=txtid.getText().toString();
S
t
r
i
n
g
s
n
a
m
e
=
t
x
t
n
a
m
e
.
g
e
t
T
e
x
t e
( .
) g
. e
t t
o T
S e
t x
r t
i (
n )
g .
( t
) o
; S
S t
t r
r i
i n
n g
g (
s )
a ;
g String
e saddress=txtaddress.getText().t
= oString();
t
x
t MyDatabase dat=new
a MyDatabase(this,MyDatabase.DATABAS
g E_NAME, null,1);
SQLiteDatabase
database=dat.getWritableDatabase();
ContentValues cv= new ContentValues();
cv.put("id", sid);
cv.put("name", sname);
cv.put("age",sage );
cv.put("address",
saddress);
database.insert("Employee", null,
cv); database.close();
Toast.makeText(this, "Data Inserted successfully", 15000).show();
}
else if(v.equals(btnsearch))
{
Intent it=new Intent(this,SearchActivity.class);
startActivity(it);
}
}
}
MyDatabase.java
package com.example.employeedetails;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteDatabase.CursorFactor;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table employee (id TEXT,name TEXT,age TEXT,address
TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
Searchactivity.java
package com.example.employeedetails;
import android.app.Activity;
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;
import android.widget.TextView;
import android.widget.Toast;
public class SearchActivity extends Activity implements OnClickListener{
EditText txtempid;
Button btnsearch;
TextView
txtdisplay;
txtempid=(EditText)findViewById(R.id.txt_empid);
btnsearch=(Button)findViewById(R.id.txt_search);
txtdisplay=(TextView)findViewById(R.id.txt_display);
btnsearch.setOnClickListener(this);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(this, "Button clicked",
15000).show(); if(v.equals(btnsearch))
{
String eid=txtempid.getText().toString();
MyDatabase dat=new MyDatabase(this,
MyDatabase.DATABASE_NAME, null,1);
SQLiteDatabase
database=dat.getR
eadableDatabase();
String[]
columns=new
String[]
{"id","name","age
","address"};
String
where="id=?";
String[] value= new String[] { eid.trim() };
Cursor
cu=database.query(MyDatabase.EMPLOY
EE_TABLE, columns, where,
else
{
}
}
}
}
Manifest.xml
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.employeedetails"
android:versionCode="1"
android:versionName="1.0>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.employeedetails.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="SearchActivity"> </activity>
</application>
</manifest>
OUTPUT:
Program -3
Write a program to create an activity with a text box and three buttons (save, ,open and
create) open must allow to browse the text file from sdcard and must display the contents of
the file on textbox, save button must save the contents of text box to file, create button must
allow file user to create a new file and save the entered contents of the textbox.
activity_main.xml
<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"
android:orientation="vertical
" android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create new file"
android:id="@+id/btn_create"
android:layout_gravity="left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open"
android:id="@+id/btn_open"
android:layout_gravity="right"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="File:"
android:id="@+id/lbl_file" />
<EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/txt_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_gravity="center"
android:id="@+id/btn_save"/>
</LinearLayout>
Dialog_layout.xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_filename"/>
</LinearLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p2"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
<application
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.p2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
MainActivity.java
packagecom.example.p2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
TextView lblFile;
EditText txtContent;
Button
btnCreate,btnSave,btnOpen; int
FILE_CHOOSE_REQUEST=1;
String filepath;
String
filename;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblFile=(TextView)findViewById(R.id.lbl_file);
txtContent=(EditText)findViewById(R.id.txt_content);
btnCreate=(Button)findViewById(R.id.btn_create);
btnCreate.setOnClickListener(this);
btnSave=(Button)findViewById(R.id.btn_save);
btnSave.setOnClickListener(this);
btnOpen=(Button)findViewById(R.id.btn_open);
btnOpen.setOnClickListener(this);
}
@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;
}
if(resultCode==RESULT_OK)
{
filepath=data.getData().getPath();
filename=filepath.substring(filepath.lastIndexOf("/")+1);
filepath=filepath.substring(0,filepath.lastIndexOf("/"));
readFromFile(filepath,filename);
lblFile.setText(filepath+"/"+filename)
} ;
else
{
} catch (Exception e) {
Toast.makeText(getBaseContext(),
e.getLocalizedMessage(),
Toast.LENGTH_SHORT).show();
}
}
BufferedReader
br=new
BufferedReader(
inputReader);
String
data=br.readLin
e();
while(data!=null)
{
txtContent.append(data)
; data=br.readLine();
}
br.close();
} catch (Exception e) {
Toast.makeText(getBaseContext(),e.getLocalizedMessage(),
Toast.LENGTH_SHORT).show();
}
}
filepath=Environment.getExternalStorageDirectory().getAbsolutePath();
filename=txtFilename.getText().toString();
File f=new File(filepath+"/"+filename);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch
block
Toast.makeText(getBaseContext(),
""+e.getLocalizedMessage(),
Toast.LENGTH_LONG).show();
}
lblFile.setText(filepath+"/"+filename);
}
});
builder.setNegativeButton("Cancel",
null); AlertDialog
dialog=builder.create(); dialog.show();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(btnOpen))
{
Intent it=new Intent(Intent.ACTION_GET_CONTENT);
//it.setType("*.*");
it.setType("file/*");
startActivityForResult(it,
0);
}
else if(v.equals(btnCreate))
{
onShowCreateDialog();
}
else if(v.equals(btnSave))
{
writeToFile(filepath, filename);
}
}
OUTPUT:
Program -4
Write a program to create an activity with two text boxes (date /time and note contents).
Create a content provider to store the date and time and note contents to the database. Create
another program with a Button (Fetch Today Notes) on press must access the note provider
and display the notes stored for today’s date.
activity_main.xml
<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=".MainActivity"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Date:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_date" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Note Content:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_Content"
android:height="200dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Note"
android:id="@+id/btn_add_note" />
</LinearLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p3noteprovider"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.p3noteprovider.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<provider android:name="NotesProvider"
android:authorities="com.example.notesprovider"
android:exported="true"/>
</application>
</manifest>
MainActivity.java
package com.example.p3noteprovider;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.view.Menu;
import android.view.View;
import ndroid.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
EditText txtDate,txtContent;
Button btnAddNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDate=(EditText)findViewById(R.id.txt_date);
txtContent=(EditText)findViewById(R.id.txt_Content);
btnAddNote=(Button)findViewById(R.id.btn_add_note);
btnAddNote.setOnClickListener(this);
}
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(btnAddNote))
{
String sdate=txtDate.getText().toString();
String scontent=txtContent.getText().toString();
getContentResolver().insert(Uri.parse("content://com.example.notesprovider/notes"),
values);
Toast.makeText(getBaseContext(),"Data Inserted
Successfully",
Toast.LENGTH_LONG).show();
}
NotesProvider.java
package com.example.p3noteprovider;
import
android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import
android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method
stub return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method
stub return null;
}
@Override
public Uri insert(Uri arg0, ContentValues cv) {
// TODO Auto-generated method
stub db =
dbHelper.getWritableDatabase();
db.insert(ProviderDatabase.TABLE_NAME,null,cv
); db.close();
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
dbHelper=new
ProviderDatabase(getContext(),ProviderDatabase.DATABASE_NAME+".db",null,1);
return (db == null)? false:true;
@Override
public Cursor query(Uri uri, String[] arg1, String arg2, String[]
arg3, String arg4) {
// TODO Auto-generated method
db = dbHelper.getReadableDatabase();
cursor=db.query(ProviderDatabase.TABLE_NAME,a
rg1,arg2,arg3,arg4,null,null);
return cursor;
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
}
ProviderDatabase.java
package com.example.p3noteprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<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=".MainActivity"
android:orientation="vertical
" android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Date toSearch"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_search"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_search"
android:text="Search"/>
<TextView
android:layout_width="wrap_conten"
android:layout_height="wrap_content
" android:text="Data To Show"
android:id="@+id/lbl_message"/>
</LinearLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p3providerclient"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.p3providerclient.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
MainActivity.java
package com.example.p3providerclient;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSearch=(EditText)findViewById(R.id.txt_search);
lblMessage=(TextView)findViewById(R.id.lbl_message);
btnSearch=(Button)findViewById(R.id.btn_search);
btnSearch.setOnClickListener(this);
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(btnSearch))
{
String
searchDate=txtSearch.getText().toString(); String
where="note_date=?";
Cursor cursor
=getContentResolver().query(Uri.parse("content://com.example.notesprovider/notes")
,new String[]{"note_date","content"},where, new String[]{searchDate},null);
if(cursor!=null&&cursor.moveToNext())
{
String ndate=cursor.getString(0);
String content=cursor.getString(1);
lblMessage.setText(ndate+" "+content+"\n");
}
else
{
Toast.makeText(getBaseContext(),"No Data
Available",
Toast.LENGTH_LONG).show();
}
}
OUTPUT
Program -5
Create a program to create an activity with two buttons start and stop. On pressing start
button the program must start the counter and must keep on counting until stop button
is pressed.
activity_main.xml
<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"
android:orientation="vertical
" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter"
android:layout_gravity="center"
android:id="@+id/lbl_counter"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:layout_gravity="center"
android:id="@+id/btn_start"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:layout_gravity="center"
android:id="@+id/btn_stop"/>
</LinearLayout>
MainActivity.java
package com.example.p4;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener,
Runnable{ int i=0;
TextView lblcounter;
Button btnstart,btnstop;
Thread thread;
boolean running=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnstart=(Button)findViewById(R.id.btn_start);
btnstop=(Button)findViewById(R.id.btn_stop);
btnstart.setOnClickListener(this);
btnstop.setOnClickListener(this);
lblcounter=(TextView)findViewById(R.id.lbl_counter);
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method
stub if(v.equals(btnstart))
{
running=true;
thread=new
Thread(this);
thread.start();
}
else if(v.equals(btnstop))
{
//thread.interrupt();
running=false;
}
@Override
public void run() {
// TODO Auto-generated method stub
//int i=0;
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch
block e.printStackTrace();
}
hand.sendEmptyMessage(i)
; i++;
// lblcounter.setText(""+i);
}
Manifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.p4.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
OUTPUT:
Program -6
Create a program to receive the incoming SMS to the phone and put a notification on screen,
on clicking the notification it must display sender number and message content on screen.
activity_main.xml
<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"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sender Number"
android:paddingBottom="50px"
android:id="@+id/lbl_number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message content"
android:id="@+id/lbl_message"/>
</LinearLayout>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblnumber=(TextView)findViewById(R.id.lbl_number);
lblmessage=(TextView)findViewById(R.id.lbl_message);
Bundle b= getIntent().getBundleExtra("data");
if(b!=null)
{
String number=b.getString("number");
String content=b.getString("content");
lblnumber.setText(number)
;
lblmessage.setText(content);
}
}
@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;
}
}
MySmsReceiver.java
package com.example.p5;
import
android.content.BroadcastReceiver;
import android.content.Context;
import
android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<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.p5.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>
<receiver android:name="com.example.p5.MySmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
OUTPUT:
Program -7
Create a program to create a service that will put a notification on the screen every 5
seconds.
activity_main.xml
<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"
android:orientation="vertical"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Notification"
android:layout_gravity="center"
android:id="@+id/btn_start"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Notification"
android:layout_gravity="center"
android:id="@+id/btn_stop"/>
</LinearLayout>
MainActivity.java
package com.example.p6;
import android.os.Bundle;
import android.app.Activity;
import
android.content.Intent;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
if(v.equals(btnstart))
{
Intent it=new
Intent(this,ServiceClass.class); Bundle
b=new Bundle(); b.putBoolean("stop",
true); it.putExtra("data", b);
startService(it);
}
else
{
}
}
}
ServiceClass.Java
package com.example.p6;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
boolean running=false;
MyThread thread;
if(!thread.isAlive())
{
thread=new MyThread(); thread.start();
}
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onDestroy()
{
running=false;
Toast.makeText(getBaseContext(), "Service stopped",
Toast.LENGTH_LONG).show();
super.onDestroy();
}
};
class MyThread extends Thread
{
public void run()
{
int i=0; while(running)
{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
hand.sendEmptyMessage(i++);
}
}
}
}
AndroidManifest.xml
</application>
</manifest>
OUTPUT:
Program -8
Create an .aidl service to do add, subtraction and multiplication and create another application
with two buttons to read the inputs and three button add,subtract and multiply to call
add,subtract and multiply operation on .aidl service.
activity_main.xml
Activity_main.xml
-NA-
Manifest.xml
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.p7.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>
<service
android:name="MyCalService">
<intent-filter>
<action android:name="com.simple.cal"/>
</intent-filter>
</service>
</application>
</manifest>
calculator.aidl
package
Add Below Code in the Aidl folder
package com.example.cal;
interface calculator
{
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
}
MyCalService.java
package com.example.p7;
import com.example.cal.calculator;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method
stub return stub;
}
calculator.Stub stub=new
calculator.Stub() { @Override
public int sub(int a, int b) throws RemoteException {
// TODO Auto-generated method
stub return a-b;
}
@Override
public int mul(int a, int b) throws RemoteException {
// TODO Auto-generated method
stub return a*b;
}
@Override
public int add(int a, int b) throws RemoteException {
// TODO Auto-generated method
stub return a+b;
}
};
}
Part-2
Activity_main.xml
<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"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter first number" />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_first"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter second number" />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_second"/>
<TextView
android:layout_width="wrap_content
"
android:layout_height="wrap_content
" android:text="result"
android:id="@+id/txt_result"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:layout_gravity="center"
android:id="@+id/btn_add"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub"
android:layout_gravity="center"
android:id="@+id/btn_sub"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mul"
android:layout_gravity="center"
android:id="@+id/btn_mul"/>
</LinearLayout>
</LinearLayout>
Manifest.xml
calculator.aidl
package
Add Below Code in the Aidl folder
package com.example.cal;
interface calculator
{
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
}
package com.example.p7_1;
import com.example.cal.calculator;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
importandroid.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener,
ServiceConnection{ EditText txtfirst, txtsecond;
Button
btnadd,btnsub,btnmul;
TextView txtresult;
calculator cal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtfirst=(EditText)findViewById(R.id.txt_first);
txtsecond=(EditText)findViewById(R.id.txt_second);
txtresult=(TextView)findViewById(R.id.txt_result);
btnadd=(Button)findViewById(R.id.btn_add);
btnadd.setOnClickListener(this);
btnsub=(Button)findViewById(R.id.btn_sub);
btnsub.setOnClickListener(this);
btnmul=(Button)findViewById(R.id.btn_mul);
btnmul.setOnClickListener(this);
Intent intent=new Intent("com.simple.cal");
intent.setPackage("com.example.p7");
bindService(intent, this, BIND_AUTO_CREATE);
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s1=txtfirst.getText().toString();
String
s2=txtsecond.getText().toString();
int
a=Integer.parseInt(s1);
int
b=Integer.parseInt(s2);
if(v.equals(btnadd))
{
try {
int result=cal.add(a,b); txtresult.setText(""+result);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}
else if(v.equals(btnsub))
{
try {
int result=cal.sub(a,b);
txtresult.setText(""+result);
} catch (RemoteException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
}
else if(v.equals(btnmul))
{ try
{int result=cal.mul(a,b); txtresult.setText(""+result);
} catch (RemoteException e) {
// TODO Auto-generated catch
block e.printStackTrace();
}
} }
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Service Connected",
Toast.LENGTH_LONG).show();
cal=(calculator)calculator.Stub.asInterface(arg1);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
OUTPUT:
Addition
Subtraction Multiplication
Program -9
Create an activity like a phone dialer with (1,2,3,4,5,6,7,8,9,0,*,#) buttons and call and save
button on pressing the call button, it must call the phone number and on pressing the save
button it must save the number to the phone contacts.
activity_main.xml
<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"
android:gravity="center"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content>
<Button
android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del"
android:layout_alignParentRight="true"
/>
<EditText android:id="@+id/txt_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn_del"
android:layout_alignBaseline="@id/btn_del" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="1" />
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="2" />
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="3" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="4" />
<Button
android:id="@+id/btn_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="5" />
<Button
android:id="@+id/btn_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="7" />
<Button
android:id="@+id/btn_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="8" />
<Button
android:id="@+id/btn_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="9" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="*" />
<Button
android:id="@+id/btn_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0" />
<Button
android:id="@+id/btn_ash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content
" android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="call" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.p8;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import
android.widget.EditText;
EditText txtNumber;
Button
btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine,btnZero,btnCall,
btnSave,btnDel,btnStar,btnHash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNumber=(EditText)findViewById(R.id.txt_display);
btnOne=(Button)findViewById(R.id.btn_one);
btnOne.setOnClickListener(this);
btnTwo=(Button)findViewById(R.id.btn_two);
btnTwo.setOnClickListener(this);
btnThree=(Button)findViewById(R.id.btn_three);
btnThree.setOnClickListener(this);
btnFour=(Button)findViewById(R.id.btn_four);
btnFour.setOnClickListener(this);
btnFive=(Button)findViewById(R.id.btn_five);
btnFive.setOnClickListener(this);
btnSix=(Button)findViewById(R.id.btn_six);
btnSix.setOnClickListener(this);
btnSeven=(Button)findViewById(R.id.btn_seven);
btnSeven.setOnClickListener(this);
btnEight=(Button)findViewById(R.id.btn_eight);
btnEight.setOnClickListener(this);
btnNine=(Button)findViewById(R.id.btn_nine);
btnNine.setOnClickListener(this);
btnZero=(Button)findViewById(R.id.btn_zero);
btnZero.setOnClickListener(this);
btnSave=(Button)findViewById(R.id.btn_save);
btnSave.setOnClickListener(this);
btnCall=(Button)findViewById(R.id.btn_call);
btnCall.setOnClickListener(this);
btnStar=(Button)findViewById(R.id.btn_star);
btnStar.setOnClickListener(this);
btnHash=(Button)findViewById(R.id.btn_ash);
btnHash.setOnClickListener(this);
btnDel=(Button)findViewById(R.id.btn_del);
btnDel.setOnClickListener(this);
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(btnOne))
{
txtNumber.append("1");
}
else if(v.equals(btnTwo))
{
txtNumber.append("2");
}
else if(v.equals(btnThree))
{
txtNumber.append("3");
}
else if(v.equals(btnFour))
{
txtNumber.append("4");
}
else if(v.equals(btnFive))
{
txtNumber.append("5");
}
else if(v.equals(btnSix))
{
txtNumber.append("6");
}
else if(v.equals(btnSeven))
{
txtNumber.append("7");
}
else if(v.equals(btnEight))
{
txtNumber.append("8");
}
else if(v.equals(btnNine))
{
txtNumber.append("9");
}
else if(v.equals(btnZero))
{
txtNumber.append("0");
}
else if(v.equals(btnStar))
{
txtNumber.append("*");
}
else if(v.equals(btnHash))
{
txtNumber.append("#");
}
else if(v.equals(btnDel))
{
String
num=txtNumber.getText().toString();
if(num.length()>0)
{
num=num.substring(0,num.length()-1);
}
txtNumber.setText(num);
}
else if(v.equals(btnCall))
{
hoose
the Standard
Dark
called and
one. The
Darcula. click
light
Choose asonrequired.
one isNext. Now
called the choose
IntelliJ the theme,
theme whether
whereas the the
darkLight
themetheme
is or
String
num=txtNumber.getText().toString();
Intent it=new
Intent(Intent.ACTION_CALL);
it.setData(Uri.parse("tel:"+num));
startActivity(it);
}
else if(v.equals(btnSave))
{
String num=txtNumber.getText().toString();
Intent intent = new
Intent(Intent.ACTION_INSERT,
ContactsContract.Contacts.CONTENT_URI);
intent.putExtra(ContactsContract.Intents.Insert.PHONE,num);
startActivity(intent);
}
Manifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.p8.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>
85
hoose
the Standard
Dark
called and
one. The
Darcula. click
light
Choose asonrequired.
one isNext. Now
called the choose
IntelliJ the theme,
theme whether
whereas the the
darkLight
themetheme
is or
OUTPUT:
Program -10
Create a file of JSON type with values for city_name, Latitude, Longitude, Temperature and
86
hoose
the Standard
Dark
called and
one. The
Darcula. click
light
Choose asonrequired.
one isNext. Now
called the choose
IntelliJ the theme,
theme whether
whereas the the
darkLight
themetheme
is or
Humidity. Develop an application to create an activity with button to parse the JSON file which
when clicked should display the data in the textview.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Parsing JSON File"
android:textColor="@android:color/holo_red_dark"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PARSE JSON FILE"
android:layout_gravity="center"
android:id="@+id/btn_parsejson" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Display Results"
android:id="@+id/txt_resultdisplay"/>
</LinearLayout>
MainActivity.java
package com.example.labmanualjsonparse;
import androidx.appcompat.app.AppCompatActivity;
87
hoose
the Standard
Dark
called and
one. The
Darcula. click
light
Choose asonrequired.
one isNext. Now
called the choose
IntelliJ the theme,
theme whether
whereas the the
darkLight
themetheme
is or
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Button btnjson;
TextView txtdisplayresults;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnjson = findViewById(R.id.btn_parsejson);
txtdisplayresults = findViewById(R.id.txt_resultdisplay);
btnjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
InputStream is = getAssets().open("samplecity.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
txtdisplayresults.setText(" ");
for(int i = 0;i<jsonArray.length();i++)
{
JSONObject obj =jsonArray.getJSONObject(i);
txtdisplayresults.setText(txtdisplayresults.getText() + "\n Name: " + obj.getString("name")+ "\n");
txtdisplayresults.setText(txtdisplayresults.getText() + " Latitude: " + obj.getString("lat")+ "\n");
txtdisplayresults.setText(txtdisplayresults.getText() + " Longitude: " +
88
hoose
the Standard
Dark
called and
one. The
Darcula. click
light
Choose asonrequired.
one isNext. Now
called the choose
IntelliJ the theme,
theme whether
whereas the the
darkLight
themetheme
is or
obj.getString("long")+ "\n");
txtdisplayresults.setText(txtdisplayresults.getText() + " Temperature: " +
obj.getString("temperature")+ "\n");
txtdisplayresults.setText(txtdisplayresults.getText() + " Humidity: " +
obj.getString("humidity")+ "\n");
txtdisplayresults.setText(txtdisplayresults.getText() + "---------------------------- ");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
samplecity.json
[
{ "name": "Mysore ",
"lat": "12.295 ",
"long": "76.639 ",
"temperature":"22 ",
"humidity": "92 %"
},
{ "name": "Bangalore",
"lat": "12.97165 ",
"long": "77.5946 ",
"temperature":"25 ",
"humidity": "74 %"
}
]
OUTPUT:
89
hoose
the Standard
Dark
called and
one. The
Darcula. click
light
Choose asonrequired.
one isNext. Now
called the choose
IntelliJ the theme,
theme whether
whereas the the
darkLight
themetheme
is or
90