18csmp68-Mad-Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 72

BANGALORE INSTITUTE OF TECHNOLOGY

K.R.ROAD, V.V. PURA, BENGALURU - 560 004

Department of Computer Science and Engineering

18CSMP68

Mobile Application Development Lab Manual

VI- Semester

Prepared By
Dr. B.T. Harish Kumar
Assistant Professor
MOBILE APPLICATION DEVELOPMENT
(Effective from the academic year 2018 -2019)
SEMESTER – VI
Course Code 18CSMP68 IA Marks 40
Number of Contact Hours/Week 0:0:2 Exam Marks 60
Total Number of Contact Hours 3 Hours/Week Exam Hours 03
CREDITS – 02
Laboratory Objectives: This laboratory (18CSMP68) will enable students to
 Learn and acquire the art of Android Programming.
 Configure Android studio to run the applications.
 Understand and implement Android's User interface functions.
 Create, modify and query on SQlite database.
 Inspect different methods of sharing data using services.
Descriptions (if any):
1. The installation procedure of the Android Studio/Java software must be demonstrated and carri ed
out in groups.
2. Students should use the latest version of Android Studio/Java/ Kotlin to execute these programs.
Diagrams given are for representational purposes only, students are expected to improvise on them.
3. Part B programs should be developed as an application and are to be demonstrated as a mini project
in a group by adding extra features or the students can also develop their application and
demonstrate it as a mini-project. (Projects/programs are not limited to the list given in
Part B).

Programs List:

PART – A
1 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.
2 Develop an Android application using controls like Button, Text View, Edit Text for designing a
calculator having basic functionality like Addition, Subtraction, Multiplication, and Division.

3 Create a SIGN Up activity with Username and Password. Validation of password should happen
based on the following rules:

 Password should contain uppercase and lowercase letters.


 Password should contain letters and numbers.
 Password should contain special characters.
 Minimum length of the password (the default value is 8).

On successful SIGN UP proceed to the next Login activity. Here the user should SIGN IN using
the Username and Password created during signup activity. If the Username and Password are
matched then navigate to the next activity which displays a message saying “Successful Login” or
else display a toast message saying “Login Failed”. The user is given only two attempts and after
that display a toast message saying “Failed Login Attempts” and disable the SIGN IN button. Use
Bundle to transfer information from one activity to another.
4 Develop an application to set an image as wallpaper. On click of a button, the wallpaper image
should start to change randomly every 30 seconds.

5 Write a program to create an activity with two buttons START and STOP. On pressing of the
START button, the activity must start the counter by displaying the numbers from One and the
counter must keep on counting until the STOP button is pressed. Display the counter value in a
Text View control.

6 Create two files of XML and JSON type with values for City_Name, Latitude, Longitude,
Temperature, and Humidity. Develop an application to create an activity with two buttons to
parse the XML and JSON files which when clicked should display the data in their respective
layouts side by side.
7 Develop a simple application with one Edit Text 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.

8 Create an activity like a phone dialer with CALL and SAVE 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.
PART-B

1 Write a program to enter Medicine Name, Date and Time of the Day as input from the user and
store it in the SQLite database. Input for Time of the Day should be either Morning or Afternoon
or Evening or Night. Trigger an alarm based on the Date and Time of the Day and display the
Medicine Name.

2 Develop a content provider application with an activity called “Meeting Schedule” which takes
Date, Time and Meeting Agenda as input from the user and store this information into the SQLite
database. Create another application with an activity called “Meeting Info” having DatePicker
control, which on the selection of a date should display the Meeting Agenda information for that
particular date, else it should display a toast message saying “No Meeting on this Date”.
3 Create an application to receive an incoming SMS which is notified to the user. On clicking this
SMS notification, the message content and the number should be displayed on the screen. Use
appropriate emulator control to send the SMS message to your application.

4 Write a program to create an activity having a Text box, and also Save, Open and Create buttons.
The user has to write some text in the Text box. On pressing the Create button the text should be
saved as a text file in MkSDcard. On subsequent changes to the text, the Save button should be
pressed to store the latest content to the same file. On pressing the Open button, it should display
the contents from the previously stored files in the Text box. If the user tries to save the contents
in the Textbox to a file without creating it, then a toast message has to be displayed saying “First
Create a File”.

5 Create an application to demonstrate a basic media player that allows the user to Forward,
Backward, Play and Pause an audio. Also, make use of the indicator in the seek bar to move the
audio forward or backward as required.
6 Develop an application to demonstrate the use of Asynchronous tasks in android. The
asynchronous task should implement the functionality of a simple moving banner. On pressing the
Start Task button, the banner message should scroll from right to left. On pressing the Stop Task
button, the banner message should stop. Let the banner message be “Demonstration of
Asynchronous Task”.

7 Develop an application that makes use of the clipboard framework for copying and pasting of the
text. The activity consists of two Edit Text controls and two Buttons to trigger the copy and paste
functionality.

8 Create an AIDL service that calculates Car Loan EMI. The formula to calculate EMI is

E = P * (r(1+r)n)/((1+r)n-1)
where
E = The EMI payable on the car loan amount
P = The Car loan Principal Amount
r = The interest rate value computed on a monthly basis
n = The loan tenure in the form of months
The down payment amount has to be deducted from the principal amount paid towards buying the
Car. Develop an application that makes use of this AIDL service to calculate the EMI. This
application should have four Edit Text to read the Principal Amount, Down Payment, Interest
Rate, Loan Term (in months) and a button named as “Calculate Monthly EMI”. On click of this
button, the result should be shown in a Text View. Also, calculate the EMI by varying the Loan
Term and Interest Rate values.
Laboratory Outcomes: After studying these laboratory programs, students will be able to

 Create, test and debug Android application by setting up Android development environment.
 Implement adaptive, responsive user interfaces that work across a wide range of devices.
 Infer long running tasks and background work in Android applications.
 Demonstrate methods in storing, sharing and retrieving data in Android applications.

 Infer the role of permissions and security for Android applications.


Procedure to Conduct Practical Examination

 Experiment distribution
o For laboratories having only one part: Students are allowed to pick one experiment from the
lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick one experiment
from PART A and one experiment from PART B, with equal opportunity.

 Change of experiment is allowed only once and marks allotted for procedure to be
made zero of the changed part only.

 Marks Distribution (Coursed to change in accordance with university regulations)


o For laboratories having only one part – Procedure + Execution + Viva-Voce: 15+70+15=
100 Marks
o For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks
ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks

Text Books:
1. Google Developer Training, "Android Developer Fundamentals Course – Concept
Reference”, Google Developer Training Team, 2017.
https://www.gitbook.com/book/google-developer-training/android-developer-
fundamentals- course-concepts/details
(Download pdf file from the above link)
Reference Books:
1. Erik Hellman, “Android Programming – Pushing the Limits”, 1st Edition, Wiley India Pvt
Ltd, 2014. ISBN-13: 978-8126547197
2. Dawn Griffiths and David Griffiths, “Head First Android Development”, 1st Edition,
O‟Reilly SPD Publishers, 2015. ISBN-13: 978-9352131341
3. Bill Phillips, Chris Stewart and Kristin Marsicano, “Android Programming: The Big Nerd
Ranch Guide”, 3rd Edition, Big Nerd Ranch Guides, 2017. ISBN-13: 978-0134706054
18CSMP68 Mobile Application Development Lab Manual 2020-2021

Program No.1:

Create an application to design a Visiting Card. The Visiting card should havea 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.

XML Code - Activity_Main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/textView"
android:layout_width="354dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="731dp"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="BANGALORE INSTITUTE OF TECHNOLOGY"
android:textAlignment="center"
android:textColor="#ff00"
android:textSize="25sp" />
<ImageView
android:id="@+id/imageView"

Department of Computer Science & Engineering, B.I.T Page |1


18CSMP68 Mobile Application Development Lab Manual 2020-2021

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="721dp"
android:layout_marginEnd="17dp"
android:layout_marginRight="17dp"
app:srcCompat="@drawable/bitlogo"
/>

<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="698dp"
android:background="#4444"
/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="652dp"
android:layout_marginEnd="144dp"
android:layout_marginRight="144dp"
android:text="Dr. Harish Kumar B T"
android:textAlignment="center"
android:textColor="#3700b3"
android:textSize="20sp"
/>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="603dp"
android:layout_marginEnd="157dp"
android:layout_marginRight="157dp"
android:text="Assistant Professor"
android:textAlignment="center"
android:textSize="18sp"
/>

Department of Computer Science & Engineering, B.I.T Page |2


18CSMP68 Mobile Application Development Lab Manual 2020-2021

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="556dp"
android:layout_marginEnd="163dp"
android:layout_marginRight="163dp"
android:text="Mob: 9980119894"
android:textAlignment="center"
android:textColor="#b00020"
android:textSize="18sp" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="510dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:text="Email ID: [email protected]"
android:textAlignment="center"
android:textColor="#3700b3"
android:textSize="18sp"
/>
</RelativeLayout>

Design Output:

Department of Computer Science & Engineering, B.I.T Page |3


18CSMP68 Mobile Application Development Lab Manual 2020-2021

JAVA Coding: Activity_Main.java

package com.example.labpgm1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Output:

Department of Computer Science & Engineering, B.I.T Page |4


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Program No.2:

Develop an Android application using controls like Button, TextView, EditText for
designing a calculator having basic functionality like Addition, Subtraction,
Multiplication and Division.

XML Code: 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="match_parent"
android:layout_height="wrap_content"
android:text="SIMPLE CALCULATOR"
android:textSize="30sp"
android:textColor="@color/design_default_color_primary_dark"
android:textAlignment="center"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Number"
android:id="@+id/num1"
android:textSize="20sp"
android:textAlignment="center"
/>

Department of Computer Science & Engineering, B.I.T Page |5


18CSMP68 Mobile Application Development Lab Manual 2020-2021

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Second Number"
android:id="@+id/num2"
android:textSize="20sp"
android:textAlignment="center"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:id="@+id/result"
android:textSize="20sp"
android:textAlignment="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seven"
android:text="7"
android:textAlignment="center"

android:onClick="seven"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/eight"
android:text="8"
android:textAlignment="center"
android:onClick="eight"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nine"
android:text="9"
android:textAlignment="center"
android:onClick="nine"
/>

Department of Computer Science & Engineering, B.I.T Page |6


18CSMP68 Mobile Application Development Lab Manual 2020-2021

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/divide"
android:text="/"
android:textAlignment="center"
android:onClick="divide"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/four"
android:text="4"
android:textAlignment="center"
android:onClick="four"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/five"
android:text="5"
android:textAlignment="center"
android:onClick="five"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/six"
android:text="6"
android:textAlignment="center"
android:onClick="six"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/multiply"
android:text="*"
android:textAlignment="center"
android:onClick="multiply"
/>
</LinearLayout>

Department of Computer Science & Engineering, B.I.T Page |7


18CSMP68 Mobile Application Development Lab Manual 2020-2021

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/one"
android:text="1"
android:textAlignment="center"
android:onClick="one"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/two"
android:text="2"
android:textAlignment="center"
android:onClick="two"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/three"
android:text="3"
android:textAlignment="center"
android:onClick="three"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="+"
android:textAlignment="center"
android:onClick="add"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Department of Computer Science & Engineering, B.I.T Page |8


18CSMP68 Mobile Application Development Lab Manual 2020-2021

android:id="@+id/dot"
android:text="."
android:textAlignment="center"
android:onClick="dot"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/zero"
android:text="0"
android:textAlignment="center"
android:onClick="zero"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/equals"
android:text="="
android:textAlignment="center"
android:onClick="compute"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/minus"
android:text="- "
android:textAlignment="center"
android:onClick="sub"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clear_one_digit_at_a_time"
android:text="C"
android:textAlignment="center"
android:onClick="clear_one_digit_at_a_time"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Department of Computer Science & Engineering, B.I.T Page |9


18CSMP68 Mobile Application Development Lab Manual 2020-2021

android:id="@+id/clear_one_field_completely"
android:text="CE"
android:textAlignment="center"
android:onClick="clear_one_field_completely"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/All_Clear"
android:text="AC"
android:textAlignment="center"
android:onClick="All_Clear"
/>
</LinearLayout>
</LinearLayout>

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 10


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Java Coding Part: ActivityMain.java


package com.example.labpgm2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText number1,number2;
TextView res;
char op;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1=findViewById(R.id.num1);
number2=findViewById(R.id.num2);
res=findViewById(R.id.result);

public void one(View v)


{
if(number1.hasFocus())
{
number1.append("1");
}
else if(number2.hasFocus())
{
number2.append("1");
}

else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}

public void two(View v)


{
if(number1.hasFocus())
{
number1.append("2");
}

Department of Computer Science & Engineering, B.I.T P a g e | 11


18CSMP68 Mobile Application Development Lab Manual 2020-2021

else if(number2.hasFocus())
{
number2.append("2");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void three(View v)


{
if(number1.hasFocus())
{
number1.append("3");
}
else if(number2.hasFocus())
{
number2.append("3");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void four(View v)


{
if(number1.hasFocus())
{
number1.append("4");
}
else if(number2.hasFocus())
{
number2.append("4");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void five(View v)


{
if(number1.hasFocus())
{
number1.append("5");
}
else if(number2.hasFocus())

Department of Computer Science & Engineering, B.I.T P a g e | 12


18CSMP68 Mobile Application Development Lab Manual 2020-2021

{
number2.append("5");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void six(View v)


{
if(number1.hasFocus())
{
number1.append("6");
}
else if(number2.hasFocus())
{
number2.append("6");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void seven(View v)


{
if(number1.hasFocus())
{
number1.append("7");
}
else if(number2.hasFocus())
{
number2.append("7");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void eight(View v)


{
if(number1.hasFocus())
{
number1.append("8");
}
else if(number2.hasFocus())
{
number2.append("8");

Department of Computer Science & Engineering, B.I.T P a g e | 13


18CSMP68 Mobile Application Development Lab Manual 2020-2021

}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}

public void nine(View v)


{
if(number1.hasFocus())
{
number1.append("9");
}
else if(number2.hasFocus())
{
number2.append("9");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void zero(View v)


{
if(number1.hasFocus())
{
number1.append("0");
}
else if(number2.hasFocus())
{
number2.append("0");
}
else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void dot(View v)


{
if(number1.hasFocus())
{
number1.append(".");
}
else if(number2.hasFocus())
{
number2.append(".");
}

Department of Computer Science & Engineering, B.I.T P a g e | 14


18CSMP68 Mobile Application Development Lab Manual 2020-2021

else {
Toast.makeText(this, "Please get the focus of First/Second Number field",
Toast.LENGTH_LONG).show();
}
}

public void add(View v)


{
op='+';
}

public void sub(View v)


{
op='-';
}

public void multiply(View v)


{
op='*';
}

public void divide(View v)


{
op='/';
}

public void compute(View v)


{
float n1,n2,r;
switch(op)
{
case '+': n1=Float.parseFloat(number1.getText().toString());
n2=Float.parseFloat(number2.getText().toString());
r=n1+n2;
res.setText(""+n1+"+"+n2+"="+r);
break;
case '-': n1=Float.parseFloat(number1.getText().toString());
n2=Float.parseFloat(number2.getText().toString());
r=n1-n2;
res.setText(""+n1+"- "+n2+"="+r);
break;
case '*': n1=Float.parseFloat(number1.getText().toString());
n2=Float.parseFloat(number2.getText().toString());
r=n1*n2;
res.setText(""+n1+"*"+n2+"="+r);
break;
case '/': n1=Float.parseFloat(number1.getText().toString());
n2=Float.parseFloat(number2.getText().toString());
r=n1/n2;
res.setText(""+n1+"/"+n2+"="+r);

Department of Computer Science & Engineering, B.I.T P a g e | 15


18CSMP68 Mobile Application Development Lab Manual 2020-2021

break;
}
}

public void All_Clear(View v)


{
number1.setText("");
number2.setText("");
res.setText("");
}

public void clear_one_field_completely(View v)


{
if(number1.hasFocus())
{
number1.setText("");
res.setText("");
}
else if(number2.hasFocus())
{
number2.setText("");
res.setText("");
}

else
{
Toast.makeText(this, "Please click on Number1/Number2 Field",
Toast.LENGTH_LONG).show();
}
}

public void clear_one_digit_at_a_time(View v)


{
if(number1.hasFocus())
{
String n;
n=number1.getText().toString();
// n=n.substring(0,n.length()-1); /// deletes digits from right to left
n=n.substring(1,n.length()); // deletes digits from left to right
number1.setText(n);
}

else if(number2.hasFocus())
{
String n;
n=number2.getText().toString();
// n=n.substring(0,n.length()-1); /// deletes digits from right to left
n=n.substring(1,n.length()); // deletes digits from left to right
number2.setText(n);
}

Department of Computer Science & Engineering, B.I.T P a g e | 16


18CSMP68 Mobile Application Development Lab Manual 2020-2021

else
{
Toast.makeText(this, "Please click on Number1/Number2 Field",
Toast.LENGTH_LONG).show();
}
}

Output:

Department of Computer Science & Engineering, B.I.T P a g e | 17


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 18


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Program 3:
Create a SIGN Up activity with Username and Password. Validation of password should happen
based on the following rules:

 Password should contain uppercase and lowercase letters.


 Password should contain letters and numbers.
 Password should contain special characters.
 Minimum length of the password (the default value is 8).

On successful SIGN UP proceed to the next Login activity. Here the user should SIGN IN using
the Username and Password created during signup activity. If the Username and Password are
matched then navigate to the next activity whichdisplays a message saying “Successful Login” or
else display a toast message saying “Login Failed”.The user is given only two attempts and after
thatdisplay a toast message saying “Failed Login Attempts” and disable the SIGN IN button. Use
Bundle to transfer information from one activity to another.

XML Part: 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="match_parent"
android:layout_height="wrap_content"
android:text="Sign UP Activity"
android:textSize="30sp"
android:textAlignment="center"
android:textColor="@color/design_default_color_error"
/>

Department of Computer Science & Engineering, B.I.T P a g e | 19


18CSMP68 Mobile Application Development Lab Manual 2020-2021

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/uid"
android:hint="Enter User Name"
android:textSize="20sp"
android:textAlignment="center"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pwd"
android:hint="Enter Password"
android:textSize="20sp"
android:inputType="textPassword"
android:textAlignment="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/signup"
android:text="SIGNUP"
android:onClick="signup"
/>
</LinearLayout>

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 20


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Activity_signin.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=".Signin">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SIGN IN ACTIVITY"
android:textAlignment="center"
android:textSize="30sp"
android:textColor="@color/design_default_color_error"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/uid"
android:hint="Enter UID"
android:textAlignment="center"
android:textSize="20sp"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pwd"
android:hint="Enter Password"
android:textAlignment="center"
android:inputType="textPassword"
android:textSize="20sp"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/signin"
android:text="SIGN IN"
android:textAlignment="center"
android:onClick="signin"
/>
</LinearLayout>

Department of Computer Science & Engineering, B.I.T P a g e | 21


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 22


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Activity_success.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"
tools:context=".Success">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN SUCCESSFUL"
android:textSize="30sp"
android:textAlignment="center"
android:layout_gravity="center"
android:textColor="@color/design_default_color_primary_dark"
/>
</LinearLayout>

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 23


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Java Coding Part: MainActivity.java

package com.example.labpgm3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {


EditText username, passwd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.uid);
passwd = findViewById(R.id.pwd);
}

public void signup(View v)


{
if(passwd.getText().toString().length()>=8 &&
validatepassword(passwd.getText().toString()))
{
Toast.makeText(this, "SIGN UP Successful", Toast.LENGTH_LONG).show();
Intent i = new Intent(this,Signin.class);
Bundle b=new Bundle();
b.putString("uid",username.getText().toString());
b.putString("password",passwd.getText().toString());
i.putExtras(b);
startActivity(i);
}
else
{
Toast.makeText(this, "Password is not meeting the constraints",
Toast.LENGTH_LONG).show();
}
}

public boolean validatepassword(String password)


{

Department of Computer Science & Engineering, B.I.T P a g e | 24


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Pattern ptrn;
Matcher mat;
String passwordptrn="^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-
9])(?=.*[<>/@%$#+=])(?=\\S+$).{8,}$";
ptrn=Pattern.compile(passwordptrn);
mat=ptrn.matcher(password);
return mat.matches();
}

Java Coding Part: Signin.java

package com.example.labpgm3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Signin extends AppCompatActivity {


EditText username,password;
Button signin;
int attempts=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
username=findViewById(R.id.uid);
password=findViewById(R.id.pwd);
signin=findViewById(R.id.signin);
}

public void signin(View v)


{
Bundle b=getIntent().getExtras();
String uname=b.getString("uid");
String passwd=b.getString("password");
if(username.getText().toString().equals(uname) &&
password.getText().toString().equals(passwd))
{
Toast.makeText(this, "SIGN IN SUCCESSFUL", Toast.LENGTH_LONG).show();

Department of Computer Science & Engineering, B.I.T P a g e | 25


18CSMP68 Mobile Application Development Lab Manual 2020-2021

attempts=0;
Intent i =new Intent(this,Success.class);
startActivity(i);
}

else
{
attempts++;
Toast.makeText(this, "SIGN IN UNSUCCESSFUL. Number of
Attempts="+attempts, Toast.LENGTH_LONG).show();

if(attempts==3)
signin.setEnabled(false);
}
}
}

Java Coding Part: Success.java


package com.example.labpgm3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class Success extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_success);
}
}

Department of Computer Science & Engineering, B.I.T P a g e | 26


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Output:

Department of Computer Science & Engineering, B.I.T P a g e | 27


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 28


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 29


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Program 4:

Develop an application to set an image as wallpaper. On click of a button, the wallpaper image
should start to change randomly every 30 seconds.

XML Part: 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="match_parent"
android:layout_height="wrap_content"
android:text="Wall Paper Changing Application"
android:textAlignment="center"
android:textSize="30sp"
android:textColor="@color/design_default_color_primary"
/>
<Button
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/wp"
android:text="changewallpaper"
android:textSize="20sp"
android:onClick="ChangeWallpaper"
/>

</LinearLayout>

Department of Computer Science & Engineering, B.I.T P a g e | 30


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 31


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Java Coding Part: MainActivity.java

package com.example.labpgm4;

import androidx.appcompat.app.AppCompatActivity;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {


Timer mytimer;
Drawable drawable;
WallpaperManager wpm;
int next=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytimer=new Timer();
wpm=WallpaperManager.getInstance(this);
}

public void changewallpaper(View v)


{
setWallPaper();
}

public void setWallPaper()


{
mytimer.schedule(new TimerTask() {
@Override
public void run() {
if(next==1)
{
drawable=getResources().getDrawable(R.drawable.one);
next=2;
}
else if(next==2)
{
drawable=getResources().getDrawable(R.drawable.two);
next=3;
}

Department of Computer Science & Engineering, B.I.T P a g e | 32


18CSMP68 Mobile Application Development Lab Manual 2020-2021

else if(next==3)
{
drawable=getResources().getDrawable(R.drawable.three);
next=4;
}

else if(next==4)
{
drawable=getResources().getDrawable(R.drawable.four);
next=5;
}

else if(next==5)
{
drawable=getResources().getDrawable(R.drawable.five);
next=6;
}

else if(next==6)
{
drawable=getResources().getDrawable(R.drawable.six);
next=7;
}

else if(next==7)
{
drawable=getResources().getDrawable(R.drawable.seven);
next=8;
}

else if(next==8)
{
drawable=getResources().getDrawable(R.drawable.eight);
next=9;
}

else if(next==9)
{
drawable=getResources().getDrawable(R.drawable.nine);
next=10;
}

else if(next==10)
{
drawable=getResources().getDrawable(R.drawable.ten);
next=1;
}

Bitmap img=((BitmapDrawable)drawable).getBitmap();

Department of Computer Science & Engineering, B.I.T P a g e | 33


18CSMP68 Mobile Application Development Lab Manual 2020-2021

try {
wpm.setBitmap(img);
} catch (IOException e) {
e.printStackTrace();
}

}
},30000,5000);
}
}

Output:

Department of Computer Science & Engineering, B.I.T P a g e | 34


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 35


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Program No. 5:

Write a program to create an activity with two buttons START and STOP. On pressing of the
START button, the activity must start the counter by displaying the numbers from One and the
counter must keep on counting until the STOP button is pressed. Display the counter value in a
TextView control.

XML Part– 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="match_parent"
android:layout_height="wrap_content"
android:text="COUNTER APPLICATION"
android:textSize="30sp"
android:textColor="@color/design_default_color_primary"
android:textAlignment="center"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/count"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="@color/design_default_color_primary"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/start"
android:text="START"

Department of Computer Science & Engineering, B.I.T P a g e | 36


18CSMP68 Mobile Application Development Lab Manual 2020-2021

android:onClick="start"
android:textSize="20sp"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:text="STOP"
android:onClick="stop"
android:textSize="20sp"
/>

</LinearLayout>

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 37


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Java Code – MainActivity.java

package com.example.program5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


int i=1;
Button start,stop;
TextView counter;
Handler myhandler=new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=findViewById(R.id.start);
stop=findViewById(R.id.stop);
counter=findViewById(R.id.count);
}

public void start(View v)


{
i=1;
myhandler.postDelayed(Threadcount,0);

public void stop(View v)


{
myhandler.removeCallbacks(Threadcount);
}

public Runnable Threadcount=new Runnable()


{

@Override
public void run() {
counter.setText(""+i);
i++;
myhandler.postDelayed(Threadcount,0);
}
};
}

Department of Computer Science & Engineering, B.I.T P a g e | 38


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Output:

Department of Computer Science & Engineering, B.I.T P a g e | 39


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 40


18CSMP68 Mobile Application Development Lab Manual 2020-2021

6 Create two files of XML and JSON type with values for City_Name, Latitude, Longitude,
Temperature, and Humidity. Develop an application to create an activity with two buttons to
parse the XML and JSON files which when clicked should display the data in their respective
layouts side by side.

XML Part– 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="match_parent"
android:layout_height="wrap_content"
android:text="XML and JSON Parser"
android:textSize="30sp"
android:textAlignment="center"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="XMLParser"
android:text="XML PARSER"
android:textAlignment="center"
/>

<Button
android:layout_width="match_parent"

Department of Computer Science & Engineering, B.I.T P a g e | 41


18CSMP68 Mobile Application Development Lab Manual 2020-2021

android:layout_height="wrap_content"
android:onClick="JSONParser"
android:text="JSON PARSER"
android:textAlignment="center"
/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resxml"
android:textSize="15sp"
android:textAlignment="center"
android:paddingLeft="10dp"
android:paddingRight="40dp"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resjson"
android:textSize="15sp"
android:textAlignment="center"
android:paddingLeft="40dp"
/>

</LinearLayout>

</LinearLayout>

Department of Computer Science & Engineering, B.I.T P a g e | 42


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 43


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Java Code – MainActivity.java


package com.example.json123;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
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 org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class MainActivity extends AppCompatActivity {


TextView resxml,resjson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resxml=findViewById(R.id.resxml);
resjson=findViewById(R.id.resjson);
}

public void XMLParser(View v)


{
try {
InputStream is=getAssets().open("city.xml");
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(is);
Element element=doc.getDocumentElement();
element.normalize();
NodeList nList= doc.getElementsByTagName("place");
resxml.setText("XML DATA");

for(int i=0;i<nList.getLength();i++)
{
Node node=nList.item(i);

Department of Computer Science & Engineering, B.I.T P a g e | 44


18CSMP68 Mobile Application Development Lab Manual 2020-2021

if(node.getNodeType()==Node.ELEMENT_NODE)
{
Element element2=(Element)node;
resxml.setText(resxml.getText()+"\n City
Name:"+getValue("cityname",element2)+"\n");
resxml.setText(resxml.getText()+"\n Lat:"+getValue("lat",element2)+"\n");
resxml.setText(resxml.getText()+"\n Long:"+getValue("long",element2)+"\n");
resxml.setText(resxml.getText()+"\n
Temperature:"+getValue("temp",element2)+"\n");
resxml.setText(resxml.getText()+"\n
Humidity:"+getValue("humidity",element2)+"\n");
resxml.setText(resxml.getText()+"\n -------------------------");
}
}

catch (IOException | ParserConfigurationException | SAXException e) {


e.printStackTrace();
}
}

private static String getValue(String tag, Element element)


{
NodeList nodeList=element.getElementsByTagName(tag).item(0).getChildNodes();
Node node=nodeList.item(0);
return node.getNodeValue();
}

public void JSONParser(View v)


{
String json;
try {
InputStream is=getAssets().open("city1.json");
int size=is.available();
byte[] buffer=new byte[size];
is.read(buffer);
is.close();
json=new String(buffer, "UTF-8");
JSONArray jsonArray=new JSONArray(json);
resjson.setText("JSON DATA");

for(int i=0; i<jsonArray.length();i++)


{
JSONObject obj=jsonArray.getJSONObject(i);
resjson.setText(resjson.getText()+"\n City Name:"+obj.getString("name")+"\n");
resjson.setText(resjson.getText()+"\n Lat:"+obj.getString("lat")+"\n");
resjson.setText(resjson.getText()+"\n Long:"+obj.getString("long")+"\n");

Department of Computer Science & Engineering, B.I.T P a g e | 45


18CSMP68 Mobile Application Development Lab Manual 2020-2021

resjson.setText(resjson.getText()+"\n Temperature:"+obj.getString("temp")+"\n");
resjson.setText(resjson.getText()+"\n Humidity:"+obj.getString("humidity")+"\n");
resjson.setText(resjson.getText()+"\n --------------------");
}
}

catch (IOException | JSONException e) {


e.printStackTrace();
}
}

Department of Computer Science & Engineering, B.I.T P a g e | 46


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Output:

Department of Computer Science & Engineering, B.I.T P a g e | 47


18CSMP68 Mobile Application Development Lab Manual 2020-2021

7 Develop a simple application with one Edit Text 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.

XML Part– 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="match_parent"
android:layout_height="wrap_content"
android:text="TEXT TO SPEECH APP"
android:textColor="@color/design_default_color_primary_dark"
android:textAlignment="center"
android:textSize="30sp"

/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pitch"
android:hint="Enter Pitch (1.0 is normal)"
android:textSize="20sp"

/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/speechrate"
android:hint="Enter Speech Rate (1.0 is normal)"
android:textSize="20sp"
/>

Department of Computer Science & Engineering, B.I.T P a g e | 48


18CSMP68 Mobile Application Development Lab Manual 2020-2021

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/texttospeak"
android:hint="Enter Text to Speak"
android:inputType="textMultiLine"

/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnspeak"
android:text="SPEAK"
android:onClick="speak"
android:textSize="30sp"
/>

</LinearLayout>

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 49


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Java Code – MainActivity.java

package com.example.textspeech;

import androidx.appcompat.app.AppCompatActivity;

import android.app.UiAutomation;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

import static android.speech.tts.TextToSpeech.*;

public class MainActivity extends AppCompatActivity {


EditText pitchrate,setspeechrate,text;
TextToSpeech t;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pitchrate=findViewById(R.id.pitch);
setspeechrate=findViewById(R.id.speechrate);
text=findViewById(R.id.texttospeak);

t=new TextToSpeech(this, new OnInitListener() {


@Override
public void onInit(int status) {
if(status==SUCCESS)
{
t.setLanguage(Locale.ENGLISH);
}
else
{
Toast.makeText(MainActivity.this, "TextToSpeech Initialization Failed",
Toast.LENGTH_LONG).show();
}
}
});

public void speak(View v)


{

Department of Computer Science & Engineering, B.I.T P a g e | 50


18CSMP68 Mobile Application Development Lab Manual 2020-2021

float pr,sr;
String txt;
if(pitchrate.getText().toString().equals(""))
{
pr=1.0f;
}
else
{
pr=Float.parseFloat(pitchrate.getText().toString());
}

if(setspeechrate.getText().toString().equals(""))
{
sr=1.0f;
}
else
{
sr=Float.parseFloat(setspeechrate.getText().toString());
}

t.setPitch(pr);
t.setSpeechRate(sr);
t.speak(text.getText().toString(),QUEUE_FLUSH,null);
}
}

Department of Computer Science & Engineering, B.I.T P a g e | 51


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Output:

Department of Computer Science & Engineering, B.I.T P a g e | 52


18CSMP68 Mobile Application Development Lab Manual 2020-2021

8 Create an activity like a phone dialer with CALL and SAVE 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.

XML Part– 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"
android:layout_marginTop="150dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CALL and SAVE APPLICATION"
android:textSize="30sp"
android:textAlignment="center"
android:textColor="@color/design_default_color_primary_dark"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/num"
android:hint="Enter Number To Dial/SAVE"
android:textAlignment="center"

Department of Computer Science & Engineering, B.I.T P a g e | 53


18CSMP68 Mobile Application Development Lab Manual 2020-2021

android:textSize="20sp"
android:textColor="@color/design_default_color_error"/>

<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/del"
android:text="DEL"
android:textAlignment="center"
android:onClick="delete"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">

<Button
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="three"
android:text="3" />

<Button
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:onClick="one"
android:text="1" />

<Button
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="two"
android:text="2" />

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">

Department of Computer Science & Engineering, B.I.T P a g e | 54


18CSMP68 Mobile Application Development Lab Manual 2020-2021

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/four"
android:text="4"
android:onClick="four"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/five"
android:text="5"
android:onClick="five"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/six"
android:text="6"
android:onClick="six"/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seven"
android:text="7"
android:onClick="seven"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/eight"
android:text="8"
android:onClick="eight"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nine"
android:text="9"
android:onClick="nine"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"

Department of Computer Science & Engineering, B.I.T P a g e | 55


18CSMP68 Mobile Application Development Lab Manual 2020-2021

android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/star"
android:text="*"
android:onClick="star"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/zero"
android:text="0"
android:onClick="zero"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hash"
android:text="#"
android:onClick="hash"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/call"
android:text="CALL"
android:onClick="call"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save"
android:text="SAVE"
android:onClick="save"/>
</LinearLayout>

</LinearLayout>

Department of Computer Science & Engineering, B.I.T P a g e | 56


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Design Output:

Department of Computer Science & Engineering, B.I.T P a g e | 57


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Java Code – MainActivity.java


package com.example.l8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number=findViewById(R.id.num);
}

public void one(View v)


{
number.append("1");
}

public void two(View v)


{
number.append("2");

public void three(View v)


{
number.append("3");
}

public void four(View v)


{
number.append("4");
}

public void five(View v)


{
number.append("5");
}

public void six(View v)

Department of Computer Science & Engineering, B.I.T P a g e | 58


18CSMP68 Mobile Application Development Lab Manual 2020-2021

{
number.append("6");
}

public void seven(View v)


{
number.append("7");
}

public void eight(View v)


{
number.append("8");
}

public void nine(View v)


{
number.append("9");
}

public void zero(View v)


{
number.append("0");
}

public void star(View v)


{
number.append("*");
}

public void hash(View v)


{
number.append("#");
}

public void delete(View v)


{
String n=number.getText().toString();
n=n.substring(0,n.length()-1);
number.setText(n);
}

public void call(View v)


{
String num=number.getText().toString();
Intent i =new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+num));

//Intent i =new Intent(Intent.ACTION_VIEW,Uri.parse(num));


startActivity(i);
}

Department of Computer Science & Engineering, B.I.T P a g e | 59


18CSMP68 Mobile Application Development Lab Manual 2020-2021

public void save(View v)


{
String num=number.getText().toString();
Intent i =new Intent(Intent.ACTION_INSERT,
ContactsContract.Contacts.CONTENT_URI);
i.putExtra(ContactsContract.Intents.Insert.PHONE,num);
startActivity(i);
}
}

Output:

Department of Computer Science & Engineering, B.I.T P a g e | 60


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 61


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 62


18CSMP68 Mobile Application Development Lab Manual 2020-2021

Department of Computer Science & Engineering, B.I.T P a g e | 63

You might also like