0% found this document useful (0 votes)
23 views90 pages

AP Lab Manual Updated

The document is a lab manual for the Android Programming course at Dr. Ambedkar Institute of Technology, detailing course objectives, outcomes, required resources, and a list of programming assignments. It outlines the setup for Android Studio and provides instructions for creating various applications, including a visiting card and a text-to-speech app. The manual is intended for students in the Computer Science and Engineering department during the 2020-21 academic year.

Uploaded by

jocox65721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views90 pages

AP Lab Manual Updated

The document is a lab manual for the Android Programming course at Dr. Ambedkar Institute of Technology, detailing course objectives, outcomes, required resources, and a list of programming assignments. It outlines the setup for Android Studio and provides instructions for creating various applications, including a visiting card and a text-to-speech app. The manual is intended for students in the Computer Science and Engineering department during the 2020-21 academic year.

Uploaded by

jocox65721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Dr.

AMBEDKAR INSTITUTE OF TECHNOLOGY


(An Autonomous Institute, Affiliated to Visvesvaraya Technological University, Belagavi, Accredited by NAAC, with ‘A’ Grade)
Near JnanaBharathi Campus, Bengaluru – 560056

DEPARTMENT OF COMPUTER SCIENCE


& ENGINEERING

Lab Manual

on

“Android Programming”
Subject Code: 18CSL76

Staffs - In – Charge

Ms. Uma K M, Assistant Professor, Dr. AIT


Ms. Lavanya Santhosh, Assistant Professor, Dr. AIT
Ms.Veena A, Assistant Professor, Dr. AIT

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.

◦ Java JDK5 or latest version


◦ Java Runtime Environment (JRE) 6
◦ Android SDK
◦ Android Studio
◦ Eclipse IDE for Java Developers
◦ Android Development Tool kit (ADT kit) / Eclipse
For testing purpose you require physical device because as per my experience it run smoothly than
PC Emulator.

5. RELEVANCE OF THE COURSE:


• To carry out Mini project and Main Project work
6. GENERAL INSTRUCTIONS:
• Implement the program in Android Studio.
• External practical examination.
◦ All laboratory experiments are to be included
◦ Students are allowed to pick one experiment from the lot.
◦ Marks distribution: Procedure + Conduction + Viva: 10 + 30 +10 (50)
◦ Change of experiment is allowed only once and marks allotted to the procedure part to be made zero.

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 1: Go to https://developer.android.com website.

Step 2: Click on the Download Android Studio Button.

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’.

Click the OK button.

Step 6: This will start the Android Studio.

Meanwhile, it will be finding the available SDK components.

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.

Step 8: Now it is time to download the SDK components.

Click on Finish. Components begin to download let it complete.


The Android Studio has been successfully configured. Now it’s time to launch and build apps. Click on the
Finish button to launch it.
Step 9: Click on Start a new Android Studio project to build a new app.

To run your first android app in Android Studio you may refer to Running your first Android
app.
Create your First Android Application

Step1: Launch Android Studio.


Step 2: Select File 🡪 New 🡪 New Project
Step 3: In "Choose your project", select "Phone and Tablet" tab 🡪 "Empty Activity" 🡪 Next.
Step 4: In "Configure your project" 🡪 Set "Name" to "Hello Android" (this will be the "Title" in your
phone's application menu) 🡪 The "Package name" and "Save Location" will be updated automatically 🡪 In

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

Setup Emulator (Android Virtual Device (AVD))

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

(e.g., phone/tablet, android version, screen size, and etc.).

Step 1: In Android studio, select "Tools" 🡪 AVD Manager.

Step 2: Click "Create Virtual Device".

Step 3: In "Choose a device definition" 🡪 In "Category", choose "Phone" 🡪 In "Name",

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

🡪 Click "Download" 🡪 Next.

Step 5: In "AVD Name", enter "2.7 QVGA API 27" (default) 🡪Finish.

Running the Android Application on Emulator

Step 1: Select the "Run" menu 🡪 "Run app" 🡪 Under "Available Virtual Devices", select "2.7 QVGA

API 27" 🡪 OK.

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"

menu 🡪 "Run app".


Program -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.

activity_main.xml

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


-<LinearLayout tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

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

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


<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">

<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"

android:text="Convert Text to Speech"


app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

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

public class MainActivity extends AppCompatActivity {

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

t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {


@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.ENGLISH);
}
}
});

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

public void onPause()


{
if(t1 != null)
{
t1.stop();
t1.shutdown();
}
super.onPause(); }
}

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

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Employee id" />

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

public class MainActivity extends Activity implements OnClickListener {


EditText txtid,txtname,txtage,txtaddress;
Button btnsubmit,btnsearch;

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

public class MyDatabase extends SQLiteOpenHelper


{
public static String
DATABASE_NAME="Employee.db"; public static
String EMPLOYEE_TABLE="employee";

public MyDatabase(Context context, String name, CursorFactory


factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

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

public void onCreate(Bundle b)


{
super.onCreate(b);
setContentView(R.layout.search
);

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,

value, null, null, null);


txtdisplay.setText(""
);
if(cu.moveToNext())
{

String id=cu.getString(0); String


name=cu.getString(1); String
age=cu.getString(2); String
address=cu.getString(3);

txtdisplay.append(id+ " " +name+ " "+age+ " "+address+"\n");


}

else
{

Toast.makeText(this, "No Id Exist", 15000).show();

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

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter File Name"/>

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

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

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

public void onActivityResult(int requestCode,int resultCode, Intent data)


{
super.onActivityResult(requestCode, resultCode, data);

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
{

Toast.makeText(this,"Wrong Choice of File",


Toast.LENGTH_LONG).sho
w();
}

public void writeToFile(String path,String filename)


{
try {
FileOutputStream fileout=new
FileOutputStream(new File(path+"/"+filename));
OutputStreamWriter outputWriter=new
OutputStreamWriter(fileout);
outputWriter.write(txtContent.getText().toString());
outputWriter.close();

//display file saved message


Toast.makeText(getBaseContext(), "File Saved
successfully!", Toast.LENGTH_SHORT).show();

} catch (Exception e) {
Toast.makeText(getBaseContext(),
e.getLocalizedMessage(),
Toast.LENGTH_SHORT).show();
}
}

public void readFromFile(String path,String filename)


{
try {
FileInputStream fileIn=new
FileInputStream(path+"/"+filename);
InputStreamReader inputReader= new
InputStreamReader(fileIn);

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

public void onShowCreateDialog()


{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
final View dialogView=getLayoutInflater().inflate(R.layout.dialog_layout,
null); builder.setView(dialogView);
builder.setPositiveButton("Ok", new
DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method
stub EditText
txtFilename=(EditText)dialogView.findViewById(R.id.txt_filename);

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

Content Provider part

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

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

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

public class MainActivity extends Activity implements OnClickListener {

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();

ContentValues values = new ContentValues();


values.put("note_date",sdate);
values.put("content",scontent);

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;

public class NotesProvider extends ContentProvider


{
static final String URL =
"content://com.example.notesprovider/notes"; SQLiteDatabase db;
ProviderDatabase dbHelper;

static final UriMatcher


uriMatcher; static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.example.notesprovider", "notes",1);
}

@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

stub Cursor cursor=null;

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;

public class ProviderDatabase extends SQLiteOpenHelper


{
public static String DATABASE_NAME="noteprovider";
public static String TABLE_NAME="notes";
public static String COLUMN_DATE="note_date";
public static String COLUMN_NOTE="content";

public ProviderDatabase(Context context, String


name, CursorFactory factory, int
version) {
super(context, name, factory, version);

// TODO Auto-generated constructor stub


}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

db.execSQL("create table notes (note_date TEXT,content TEXT)");

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub

}
}

Content Resolver part

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

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

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

public class MainActivity extends Activity implements OnClickListener {


EditText txtSearch;
Button btnSearch;
TextView lblMessage;

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

Handler hand=new Handler()


{
public void handleMessage(Message m)
{
lblcounter.setText(""+m.what);
}
};

@Override
public void run() {
// TODO Auto-generated method stub
//int i=0;

while(i<100 && running)

{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch
block e.printStackTrace();
}
hand.sendEmptyMessage(i)
; i++;
// lblcounter.setText(""+i);

}
Manifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p4"
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.p4.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>
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

package com.example.p5; import


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

public class MainActivity extends


Activity { TextView
lblnumber,lblmessage;

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

public class MySmsReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Object[]
objmessages=(Object[])arg1.getExtras().get("pdus");
for(int i=0; i<objmessages.length;i++)
{
SmsMessage
m=SmsMessage.createFromPdu((byte[])objmessages[i]); Bundle
b1=new Bundle();
b1.putString("number",
m.getOriginatingAddress());
b1.putString("content", m.getMessageBody());
Intent it=new Intent(arg0, MainActivity.class);
it.putExtra("data", b1);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_
TASK); arg0.startActivity(it);
break;
}
}
}

AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p5"
android:versionCode="1"
android:versionName="1.0"
>

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

public class MainActivity extends Activity implements OnClickListener{


Button btnstart,btnstop;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnstart=(Button)findViewById(R.id.btn_start);
btnstart.setOnClickListener(this);
btnstop=(Button)findViewById(R.id.btn_stop);
btnstop.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(btnstart))
{
Intent it=new
Intent(this,ServiceClass.class); Bundle
b=new Bundle(); b.putBoolean("stop",
true); it.putExtra("data", b);
startService(it);
}

else
{

Intent it=new Intent(this,ServiceClass.class);


stopService(it);

}
}

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

public class ServiceClass extends Service{

boolean running=false;
MyThread thread;

public static final String NOTIFICATION_CHANNEL_ID = "10001" ;


public static final int NOTIFICATION_ID = 1 ;
private final static String default_notification_channel_id = "default" ;

public void onCreate()


{
super.onCreate();
Toast.makeText(getBaseContext(), "Service Created",
Toast.LENGTH_LONG).show();
running=true; thread=new MyThread(); thread.start();
}

public int onStartCommand(Intent intent, int flags,int startId)


{
super.onStartCommand(intent, flags, startId);
Toast.makeText(getBaseContext(), "Service started",
Toast.LENGTH_LONG).show();
Bundle b=intent.getBundleExtra("data"); running=b.getBoolean("stop");

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

Handler hand=new Handler()


{
public void handleMessage(Message m)
{
NotificationManager
manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

NotificationCompat.Builder mBuilder = new


NotificationCompat.Builder(getBaseContext(),
default_notification_channel_id )
.setSmallIcon(R.drawable. ic_launcher_foreground )
.setContentTitle( "From Service" )
.setContentText( "Hai " +m.what );

if (android.os.Build.VERSION. SDK_INT >=


android.os.Build.VERSION_CODES. O ) {

int importance = NotificationManager. IMPORTANCE_HIGH ;


NotificationChannel notificationChannel = new
NotificationChannel( NOTIFICATION_CHANNEL_ID ,
"NOTIFICATION_CHANNEL_NAME" , importance) ;
notificationChannel.enableLights( true ) ;
notificationChannel.setLightColor(Color. RED ) ;
notificationChannel.enableVibration( true ) ;
notificationChannel.setVibrationPattern( new long []{ 100 , 200 ,
300 , 400 , 500 , 400 , 300 , 200 , 400 }) ;
mBuilder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
assert manager != null;
manager.createNotificationChannel(notificationChannel) ;
}
assert manager != null;
manager.notify(NOTIFICATION_ID, mBuilder.build()) ;

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

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p6"
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.p6.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="ServiceClass"></service>

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

Part-1 (has an empty layout)

Activity_main.xml

-NA-

Manifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p7"
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.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

Right Click on Java Folder and Create a New AIDL Folder


Right Click on aidl folder and Create a New Package com.example.cal

under aidl folder


Create a New file calculator.aidl under the package com.example.cal

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

After the above step Build Project by calling Make


MainActivity.java -- No changes

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;

public class MyCalService extends

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

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p7_1"
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.p7_1.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>

calculator.aidl

Right Click on Java Folder and Create a New AIDL Folder


Right Click on aidl folder and Create a New Package com.example.cal

under aidl folder


Create a New file calculator.aidl under the package com.example.cal

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

After the above step Build Project by calling Make


MainActivity.java

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;

public class MainActivity extends Activity implements OnClickListener {

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

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.p8"
android:versionCode="1"
android:versionName="1.0"
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE"/>

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

public class MainActivity extends AppCompatActivity {

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();

String json = new String(buffer, "UTF-8");


JSONArray jsonArray = new JSONArray(json);

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy