Professional Documents
Culture Documents
Wa0004.
Wa0004.
LAB MANUAL
VI-SEMESTER
2023-2024
Prepared by:
Dr. Ajit Kumar
Contents
S. No Title Page No
1. Syllabus 3-3
5. References 96-96
Syllabus
SEMESTER – VI
IA Marks 25
Exam Marks50
Hours/Week 03 Hours Laboratory
LIST OF PRACTICLES
This course offers a good understanding of cloud computing concepts and challenges faced in
implementation of cloud computing.
1. All the students should sit according to their roll numbers starting from their left to
right.
2. All the students are expected to get at least the algorithm of the program/ concept to
be implement.
3. Keep the lab neat and clean.
4. Do not interchange Mouse/Keyboard of terminals.
5. Do not touch electrical fitting in the lab.
6. Eating is strictly banned in the laboratory.
7. Shut down the computer before leaving.
7 Develop a simple application with context menu and option menu 40-45
16 Write a program to store and fetch data from SQL lite database. 85-95
EXPERIMENT - 1
When an activity is first created its onCreate() method gets called then if the
subsequent onStart() method gets called and then the activity proceeds to its
onResume() method it here where the activity starts to run , now another
activity can come into foreground which forces this app to go into a paused state
, now either this activity can get killed by the os or get resumed or the activity
calls its onStop() method where the activity now goes into its Ondestroy()
method and get killed.
For more info go to –
http://developer.android.com/training/basics/activity-lifecycle/starting.html
Main Activity
package com.example.activity_life;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notify("onCreate");
}
@Override
protected void onPause() {
super.onPause();
notify("onPause");
}
@Override
protected void onResume() {
super.onResume();
notify("onResume");
}
@Override
protected void onStop() {
super.onStop();
notify("onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
notify("onDestroy");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
notify("onRestoreInstanceState");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
notify("onSaveInstanceState");
}
}
OUTPUT –
EXPERIMENT – 2
THEORY – A layout defines how your different components are shown on the
screen, a relative layout lets you arrange a component relative to another, while
Linear Layout lets you add components in a linear fashion. you can also define a
layout with another layout, according to your needs how you need to show
components on your main screen. The following code demonstrates defining a
linear layout within a Relative Layout.
Activity_main -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ADDITION"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:id="@+id/edittext1"
android:inputType="number">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:id="@+id/edittext2"
android:inputType="number">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout3" >
<Button
android:layout_width="wrap_content"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Addition"
android:layout_weight="0.50" />
<Button
android:layout_width="wrap_content"
android:id="@+id/button3"
android:layout_height="wrap_content"
android:text="subtraction"
android:layout_weight="0.50" />
<Button
android:layout_width="wrap_content"
android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="CLEAR"
android:layout_weight="0.50" />
</LinearLayout>
<View
android:layout_height="2px"
android:layout_width="fill_parent"
android:layout_below="@+id/linearLayout4"
android:background="#DDFFDD"/>
</RelativeLayout>
Main Activity -
package layout.ne;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
EditText txtData1,txtData2;
float num1,num2,result1,result2;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add.setOnClickListener(new OnClickListener() {
try
num1 = Float.parseFloat(txtData1.getText().toString());
num2 = Float.parseFloat(txtData2.getText().toString());
result1=num1+num2;
Toast.makeText(getBaseContext(),"ANSWER:"+result1,Toast.LENGTH_SHO
RT).show();
catch(Exception e)
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
});
sub.setOnClickListener(new OnClickListener() {
try
{
num1 = Float.parseFloat(txtData1.getText().toString());
num2 = Float.parseFloat(txtData2.getText().toString());
result2=num1-num2;
Toast.makeText(getBaseContext(),"ANSWER:"+result2,Toast.LENGTH_SHO
RT).show();
catch(Exception e)
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
});
clear.setOnClickListener(new OnClickListener() {
try
txtData1.setText("");
txtData2.setText("");
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
} });
}}
Output:
EXPERIMENT – 3
Main.xml coding
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_marginLeft="10pt"
android:layout_marginRight="10pt"
android:layout_marginTop="3pt">
<EditText
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/etNum1"
android:layout_width="match_parent"
android:inputType="numberDecimal">
</EditText>
<EditText
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5pt"
android:id="@+id/etNum2"
android:layout_width="match_parent"
android:inputType="numberDecimal">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:layout_marginTop="3pt"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt">
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="15pt"
android:id="@+id/btnAdd">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="15pt"
android:id="@+id/btnSub">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="*"
android:textSize="15pt"
android:id="@+id/btnMult">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="/"
android:textSize="15pt"
android:id="@+id/btnDiv">
</Button>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:textSize="12pt"
android:layout_marginTop="3pt"
android:id="@+id/tvResult"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
MainActivity.java coding
package CALCU.CALU;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
EditText input1;
EditText input2;
Button addition;
Button subtraction;
Button multiplication;
Button division;
TextView tvResult;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addition.setOnClickListener(this);
subtraction.setOnClickListener(this);
multiplication.setOnClickListener(this);
division.setOnClickListener(this);
@Override
float num1 = 0;
float num2 = 0;
float result = 0;
if (TextUtils.isEmpty(input1.getText().toString())
|| TextUtils.isEmpty(input2.getText().toString())) {
return;
= Float.parseFloat(input1.getText().toString()); num2
= Float.parseFloat(input2.getText().toString());
// defines the button that has been clicked and performs the corresponding
operation
switch (v.getId()) {
case R.id.btnAdd:
oper = "+";
break;
case R.id.btnSub:
oper = "-";
break;
case R.id.btnMult:
oper = "*";
break;
case R.id.btnDiv:
oper = "/";
break;
default:
break;
tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
OUTPUT:
EXPERIMENT – 4
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
"WebOS","Ubuntu","Windows7","Max OS X"};
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
R.layout.activity_listview, mobileArray);
listView.setAdapter(adapter); }}
Following will be the content of res/layout/activity_main.xml file −
<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"
tools:context=".ListActivity" >
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent
"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<resources>
<string name="app_name">ListDisplay</string>
<string name="action_settings">Settings</string>
</resources>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
OUTPUT-
EXPERIMENT – 5
Let’s we discuss some important methods of Gallery that may be called in order
to manage the Gallery.
Below we set the duration for how long a transition animation should run when
layout has changed.
Below we set the id of the Gallery that is used to uniquely identify it.
<Gallery
android:id="@+id/simpleGallery"
android:layout_width="fill_parent"
2. padding: This attribute is used to set the padding from left, right,
top or bottom side of a Gallery.
paddingRight: This attribute is used to set the padding from the right side of a
Gallery.
paddingLeft: This attribute is used to set the padding from the left side of a
Gallery.
paddingTop: This attribute is used to set the padding from the top side of a
Gallery.
paddingBottom: This attribute is used to set the padding from the bottom side
of a Gallery.
<Gallery
android:id="@+id/simpleGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"/> <!-- 10dp padding from all the sides of Gallery -->
android:id="@+id/simpleGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Below we set the duration for how long a transition animation should run when
layout has changed.
<Gallery
android:id="@+id/simpleGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:animationDuration="3000" />
5. spacing: This attribute is used to set the spacing between items in a Gallery.
We can also set the spacing between items programmatically means
in java class by using setSpacing() method.
android:id="@+id/simpleGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="5dp" />
android:id="@+id/simpleGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:unselectedAlpha="0.25" />
EXPERIMENT – 6
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and
fill all required details to create a new project.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT –
EXPERIMENT – 7
AIM - Develop a simple application with context menu and option menu.
THEORY –
Android Context Menu: - Android context menu appears when user press long
clicks on the element. It is also known as floating menu. It affects the selected
content while doing action on it. It doesn't support item shortcuts and icons.
Android Option Menu: - Android Option Menus are the primary menus of
android. They can be used for settings, search, delete item etc. Here, we are
going to see two examples of option menus. First, the simple option menus and
second, options menus with images. Here, we are inflating the menu by calling
the inflate() method of MenuInflater class. To perform event handling on menu
items, you need to override onOptionsItemSelected() method of Activity
class\7.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.androi
d.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="example.javatpoint.com.contextmenu.MainActivity">
<ListView
android:layout_width="368dp"
android:layout_height="495dp"
android:id="@+id/listView"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Activity class
package example.javatpoint.com.contextmenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
Output:
Android Option Menu Example
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.c
om/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="example.javatpoint.com.optionmenu.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
File: context_main.xml
Activity class
package example.javatpoint.com.optionmenu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1
Selected",Toast.LENGTH_LONG).sh
ow();
r
e
t
ow();
u
r
n
t
ow();
r
u
e
;
}
c
a
s
e
R
.
i
d
.
i
t
e
m
2
:
Toast.makeText(getApplicationContext(),"Item 2
Selected",Toast.LENGTH_LONG).sh
r
e
t
u
r
n
t
r
u
e r
; e
c t
a u
s r
e n
R t
. r
i u
d e
. ;
i d
t e
e f
m a
3 u
: l
Toast.makeText(getApplicationContext(),"Item 3 t
Selected",Toast.LENGTH_LONG).sh
:
return super.onOptionsItemSelected(item);
}
}
Output:
EXPERIMENT – 8
THEORY – Manages SMS operations such as sending data, text, and pdu SMS
messages. Get this object by calling the static method getDefault(). To create an
instance of SMS Manager associated with a specific subscription ID,
call getSmsManagerForSubscriptionId(int). This is typically used for devices
that support multiple active subscriptions at once.
activity_main.xml
<RelativeLayout xmlns:androclass="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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1
" android:layout_below="@+id/editText1"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_toLeftOf="@+id/editText1"
android:text="Mobile No:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignLeft="@+id/textView1"
android:text="Message:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="34dp"
android:layout_marginTop="48dp"
android:text="Send SMS" />
</RelativeLayout>
<uses-permission android:name="android.permission.SEND_SMS"/>
Activity class
package com.example.sendsms;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText mobileno,message;
Button sendsms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mobileno=(EditText)findViewById(R.id.editText1);
message=(EditText)findViewById(R.id.editText2);
sendsms=(Button)findViewById(R.id.button1);
//Performing action on button click
sendsms.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String no=mobileno.getText().toString();
String msg=message.getText().toString();
//Getting intent and PendingIntent instance
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(no, null, msg, pi,null);
Toast.makeText(getApplicationContext(), "Message Sent successfully!",
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}
Output:
EXPERIMENT – 9
try {
} else {
cur.moveToFirst();
}
catch (Exception e) {
e.printStackTrace();
return cur;
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_U
RI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,
null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).with
ValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Stru
cturedName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Str
ucturedName.GIVEN_NAME,firstName).build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).with
ValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Pho
ne.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUM
BER,mobileNumber).withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.
TYPE_MOBILE).build());
try {
contactAdder.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
return false;
return true;
}
ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection
(RawContacts.CONTACT_ID + "=?", args).build());
try {
contactHelper.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
try {
if (cursor.moveToFirst()) {
return cursor.getLong(personID);
}
return -1;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
cursor = null;
return -1;
OUTPUT –
EXPERIMENT-10
Source code -
package com.example.programtosendmail;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;\
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendEmail();
});
String[] TO = {""};
String[] CC = {""};
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
try {
finish();
Toast.LENGTH_SHORT).show();
XML CODE -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/sendEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/compose_email"/>
</LinearLayout>
<resources>
</resources>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher
" android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.
programtosendmail.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT –
EXPERIMENT – 11
<Button
android:id="@+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:text="Start Service" />
<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Stop Service" />
<Button
android:id="@+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:text="Next Page" />
</RelativeLayout>
activity_next.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.androi
d.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="example.javatpoint.com.androidservice.NextPage">
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="200dp"
android:text="Next Page"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Service class
package example.javatpoint.com.androidservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer myPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
myPlayer = MediaPlayer.create(this, R.raw.sun);
myPlayer.setLooping(false); // Set looping
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
myPlayer.start();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
myPlayer.stop();
}
}
Activity class
package example.javatpoint.com.androidservice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListe
ner{
Button buttonStart, buttonStop,buttonNext;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = findViewById(R.id.buttonStart);
buttonStop = findViewById(R.id.buttonStop);
buttonNext = findViewById(R.id.buttonNext);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonNext.setOnClickListener(this);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.javatpoint.com.androidservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NextPage"></activity>
<service
android:name=".MyService"
android:enabled="true" />
</application>
</manifest>
Output:
EXPERIMENT – 12
THEORY - Android WebView is used to display web page in android. The web
page can be loaded from same application or URL. It is used to display online
content in android activity. Android WebView uses webkit engine to display
web page. The android.webkit.WebView is the subclass of AbsoluteLayout
class.
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.and
roid.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.webview.MainActivity">
8.
9. <WebView
10. android:layout_width="match_parent"
11. android:layout_height="match_parent"
12. android:id="@+id/webView"
13. app:layout_constraintBottom_toBottomOf="parent"
14. app:layout_constraintLeft_toLeftOf="parent"
15. app:layout_constraintRight_toRightOf="parent"
18. </android.support.constraint.ConstraintLayout>
To add the web page (.html, .jsp) locally in application, they are required to place in the assets
folder. An assets folder is created as: right click on app -> New -> Folder -> Assets Folder -
>main or simply create an assets directory inside main directory.
Activity class
1. package example.javatpoint.com.webview;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5. import android.webkit.WebView;
6.
AppCompatActivity { 8.
9. @Override
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.activity_main);
14. // mywebview.loadUrl("https://www.javatpoint.com/");
15.
18.
19. mywebview.loadUrl("file:///android_asset/myresource.html");
20. }
21. }
Output:
EXPERIMENT – 13
THEORY – Here are some steps to demonstrate the use of map view –
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and
fill all required details to create a new project.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fetchLocation();
if (ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
if (location != null) {
currentLocation = location;
supportMapFragment.getMapAsync(MainActivity.this);
}); }
@Override
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5));
googleMap.addMarker(markerOptions);
@Override
switch (requestCode) {
case REQUEST_CODE:
fetchLocation();
break;
<string name="app_name">Sample</string>
</resources>
Step 6 – To get the google API key (map_key), kindly follow the steps below
● Click the project drop-down and select or create the project for which you
want to add an API key.
● Click the menu button and select APIs & Services > Credentials.
● On the Credentials page, click Create credentials > API key. The API key
created dialog displays your newly created API key.
● Click Close.
● The new API key is listed on the Credentials page under API keys.
(Remember to restrict the API key before using it in production.)
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher
" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="@string/map_key"/>
<activity android:name=".MainActivity">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
Output -
EXPERIMENT – 14
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.co
m/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="example.javatpoint.com.implicitintent.MainActivity"
>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.575"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="172dp"
android:text="Visit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>
Activity class
package example.javatpoint.com.implicitintent;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=editText.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}
Output –
EXPERIMENT – 15
android:id = "@+id/save"
android:text = "Save"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
</LinearLayout>
In the above code, we have taken edit text and button. When user click on
button, it will take data from edit text and store in external storage.
Step 3 − Add the following code to src/MainActivity.java
package com.example.andy.myapplication;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 100;
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText enterText = findViewById(R.id.enterText);
save = findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
os = new FileOutputStream(file);
os.write(enterText.getText().toString().getBytes());
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
requestPermission(); // Code for permission
}
} else {
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + "/text/");
dir.mkdir();
File file = new File(dir, "sample.txt");
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(enterText.getText().toString().getBytes());
os.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }
} }); }
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result = = PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
} }
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(MainActivity.this, "Write External Storage permission allows us to
create files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[]
grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] = =
PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use local drive .");
} else {
Log.e("value", "Permission Denied, You cannot use local drive .");
}
break;
} } }
AIM – Write a program to store and fetch data from SQL lite database.
THEORY – SQLite is an open-source relational database i.e., used to perform
database operations on android devices such as storing, manipulating or
retrieving persistent data from the database.
It is embedded in android by default. So, there is no need to perform any
database setup or administration task.
The android.database.sqlite.SQLiteOpenHelper class is used for database
creation and version management. For performing any database operation, you
have to provide the implementation of onCreate() and onUpgrade() methods of
SQLiteOpenHelper class.
Source code :
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/title"
android:layout_x="110dp"
android:layout_y="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="@string/empid"
android:layout_x="30dp"
android:layout_y="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editEmpid"
android:inputType="number"
android:layout_x="150dp"
android:layout_y="50dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/name"
android:layout_x="30dp"
android:layout_y="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editName"
android:inputType="text"
android:layout_x="150dp"
android:layout_y="100dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/salary"
android:layout_x="30dp"
android:layout_y="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editsalary"
android:inputType="number"
android:layout_x="150dp"
android:layout_y="150dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnAdd"
android:text="@string/add"
android:layout_x="30dp"
android:layout_y="200dp"
android:layout_width="130dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnDelete"
android:text="@string/delete"
android:layout_x="160dp"
android:layout_y="200dp"
android:layout_width="130dp"
android:layout_height="40dp"/>n
<Button android:id="@+id/btnModify"
android:text="@string/modify"
android:layout_x="30dp"
android:layout_y="250dp"
android:layout_width="130dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnView"
android:text="@string/view"
android:layout_x="160dp"
android:layout_y="250dp"
android:layout_width="130dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnViewAll"
android:text="@string/view_all"
android:layout_x="85dp"
android:layout_y="300dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
</AbsoluteLayout>
<resources>
</resources>
8) Now select mainactivity.java file and type the following code.In my coding maniactivity name
is EmployeedetailActivity.
package employee.detail;
//import android.R;
import android.app.Activity;
import android.app.AlertDialog.Builder;
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;
EditText editEmpid,editName,editsalary;
Button btnAdd,btnDelete,btnModify,btnView,btnViewAll;
SQLiteDatabase db;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editEmpid=(EditText)findViewById(R.id.editEmpid);
editName=(EditText)findViewById(R.id.editName);
editsalary=(EditText)findViewById(R.id.editsalary);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnModify=(Button)findViewById(R.id.btnModify);
btnView=(Button)findViewById(R.id.btnView);
btnViewAll=(Button)findViewById(R.id.btnViewAll);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewAll.setOnClickListener(this);
VARCHAR,salary VARCHAR);");
if(view==btnAdd)
if(editEmpid.getText().toString().trim().length()==0||
editName.getText().toString().trim().length()==0||
editsalary.getText().toString().trim().length()==0)
{
return;
VALUES('"+editEmpid.getText()+"','"+editName.getText()+
"','"+editsalary.getText()+"');");
clearText();
if(view==btnDelete)
if(editEmpid.getText().toString().trim().length()==0)
return;
empid='"+editEmpid.getText()+"'", null);
if(c.moveToFirst())
empid='"+editEmpid.getText()+"'");
else
{
showMessage("Error", "Invalid Employee id");
clearText();
if(view==btnModify)
if(editEmpid.getText().toString().trim().length()==0)
return;
empid='"+editEmpid.getText()+"'", null);
if(c.moveToFirst())
name='"+editName.getText()+"',salary='"+editsalary.getText()
else
clearText();
if(view==btnView)
{
if(editEmpid.getText().toString().trim().length()==0)
return;
empid='"+editEmpid.getText()+"'", null);
if(c.moveToFirst())
editName.setText(c.getString(1));
editsalary.setText(c.getString(2));
else
clearText();
if(view==btnViewAll)
if(c.getCount()==0)
return;
}
while(c.moveToNext())
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("salary: "+c.getString(2)+"\n\n");
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
editEmpid.setText("");
editName.setText("");
editsalary.setText("");
editEmpid.requestFocus();
}
OUTPUT:
REFERENCES
https://developer.android.com/
https://www.tutorialspoint.com/android/
3. GEEKFORGEEKS WEBSITE: -
https://www.geeksforgeeks.org/
4. JAVATPONT WEBSITE: -
https://www.javatpoint.com/
5. STACKOVERFLOW WEBSITE: -
https://stackoverflow.com/