Mobile Application Development Laboratory
Mobile Application Development Laboratory
MOBILE APPLICATION
DEVELOPMENT – 18CSMP68
LAB MANUAL
VISVESVARAYA TECHNOLOGICAL UNIVERSITY
JNANA SANGAMA, BELGAVI – 590018, KARNATAKA
In Collaboration with
Dr. Gowrishankar S
Associate Professor,
Dr. Ambedkar Institute of Technology, Bengaluru
[email protected]
2
Hardware and Software Requirements
Google provides Android Studio for the Windows, Mac OS X and Linux platforms. You can
download Android Studio from the Android Studio home page,
(https://developer.android.com/studio/index.html) where you will also find the traditional
SDK’s with Android Studio’s command-line tools. Before downloading Android Studio,
make sure your platform meets the following requirements:
Windows requirements
Linux OS requirements
You are encouraged to run each of these programs. All the programs are tested using Android
Studio 3.5 on Windows platform.
Note: The authors assume no responsibility for errors and omissions resulting from the use
of the information contained herein.
3
MOBILE APPLICATION DEVELOPMENT
(Effective from the academic year 2018 -2019)
SEMESTER – VI
Subject 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 will enable students to
1. Learn and acquire the art of Android Programming.
2. Configure Android studio to run the applications.
3. Understand and implement Android's User interface functions.
4. Create, modify and query on SQlite database.
5. Inspect different methods of sharing data using services.
Descriptions (if any):
Installation procedure of the Android Studio/Java software must be demonstrated, carried
out in groups.
Students should use the latest version of Android Studio/Java to execute these programs.
All of these diagrams are for representational purpose only. Students are expected to
improvise on it.
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, TextView, EditText for
designing a calculator having basic functionality like Addition, Subtraction,
Multiplication, and Division.
4
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.
5
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.
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 EditText so that the user can write some text in it.
Create a button called “Convert Text to Speech” that converts the user input text into
voice.
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.
6
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”.
7
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,
8
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.
7 Develop an application that makes use of the clipboard framework for copying and
pasting of the text. The activity consists of two EditText 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 EditText 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 TextView. Also,
calculate the EMI by varying the Loan Term and Interest Rate values.
9
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 (Subjected 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,
10
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
11
CONTENTS
Sl. Experiments Page
No No.
PART – A
1 Create an application to design a Visiting Card. The Visiting card should have a 15
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, TextView, EditText for 18
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 28
should happen based on the rules
4 Develop an application to set an image as wallpaper. On click of a button, the 35
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 38
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.
6 Create two files of XML and JSON type with values for City_Name, Latitude, 41
Longitude, Temperature, and Humidity. Develop an application to create an activity
with two buttons to parse the XML and JSON files
7 Develop a simple application with one EditText so that the user can write some text 46
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 49
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 57
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” 68
which takes Date, Time and Meeting Agenda as input from the user and store this
information into the SQLite database.
3 Create an application to receive an incoming SMS which is notified to the user. On 79
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
12
4 Write a program to create an activity having a Text box, and also Save, Open and 83
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.
5 Create an application to demonstrate a basic media player that allows the user to 88
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. 94
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 97
pasting of the text. The activity consists of two EditText controls and two Buttons to
trigger the copy and paste functionality.
8 Create an AIDL service that calculates Car Loan EMI. Develop an application that 100
makes use of this AIDL service to calculate the EMI. This application should have
four EditText 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 TextView. Also, calculate the EMI by
varying the Loan Term and Interest Rate values.
13
Create your First Android Application
Android applications are written in Java, and use XML extensively. We shall assume that you
have basic knowledge of Java and XML. Goto "Android Guides" @
https://developer.android.com/guide/index.html. Read "Building your first app".
14
MOBILE APPLICATION DEVELOPMENT
PART – A
1. Create an application to design a Visiting Card. The Visiting card should have company
logo at the top right corner. The company name should be displayed in Capital letters,
aligned to the center. Information like name of the employee, job title, phone number,
address, email, fax, website address is to be displayed. Insert a horizontal line between the
job title and the phone number.
First, create the android application as discussed in “Create your First Android Application”.
Copy an image for logo and save the image in the drawable folder.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="17dp"
android:layout_marginRight="17dp"
android:text=" GOOGLE"
android:textColor="@color/colorAccent"
android:textSize="38dp"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_marginTop="103dp"
android:layout_marginBottom="498dp"
15
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.07"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView3"
android:layout_width="81dp"
android:layout_height="30dp"
android:layout_marginBottom="16dp"
android:text="Andy Rubin"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="@+id/view"
app:layout_constraintHorizontal_bias="0.463"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Founder and CEO, Playground Global"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.453" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="165dp"
android:layout_marginLeft="165dp"
android:layout_marginTop="301dp"
android:layout_marginEnd="187dp"
android:layout_marginRight="187dp"
android:layout_marginBottom="283dp"
android:text="Ph:Number: 2019201923"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="165dp"
android:layout_marginLeft="165dp"
android:layout_marginTop="346dp"
android:layout_marginEnd="187dp"
android:layout_marginRight="187dp"
android:layout_marginBottom="238dp"
android:text="Address: Chappaqua, New York, U.S"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
16
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="161dp"
android:layout_marginLeft="161dp"
android:layout_marginTop="386dp"
android:layout_marginEnd="191dp"
android:layout_marginRight="191dp"
android:layout_marginBottom="198dp"
android:text="Email: [email protected]"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
17
2. Develop an Android application using controls like Button, TextView, EditText for
designing a calculator having basic functionality like Addition, Subtraction, Multiplication,
and Division.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIMPLE CALCULATOR"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.396"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="73dp"
android:layout_marginEnd="277dp"
android:layout_marginBottom="639dp"
android:text="Results / Calculation"
android:textColor="@color/colorAccent"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button7"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="77dp"
android:layout_marginTop="142dp"
android:layout_marginEnd="256dp"
18
android:layout_marginBottom="541dp"
android:text="7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button8"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="47dp"
android:layout_marginTop="142dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="541dp"
android:text="8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button9"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="147dp"
android:layout_marginTop="142dp"
android:layout_marginEnd="122dp"
android:layout_marginBottom="526dp"
android:text="9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttondiv"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="227dp"
android:layout_marginTop="142dp"
android:layout_marginEnd="125dp"
android:layout_marginBottom="532dp"
android:text="/"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="97dp"
android:layout_marginEnd="97dp"
android:layout_marginBottom="589dp"
android:ems="10"
android:inputType="number"
19
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button4"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="10dp"
android:layout_marginTop="192dp"
android:layout_marginEnd="256dp"
android:layout_marginBottom="496dp"
android:text="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button5"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="47dp"
android:layout_marginTop="192dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="493dp"
android:text="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button6"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="147dp"
android:layout_marginTop="192dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="526dp"
android:text="6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonmul"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="227dp"
android:layout_marginTop="192dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="532dp"
android:text="*"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
20
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button1"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="10dp"
android:layout_marginTop="240dp"
android:layout_marginEnd="256dp"
android:layout_marginBottom="496dp"
android:text="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button2"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="47dp"
android:layout_marginTop="240dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="493dp"
android:text="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button3"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="147dp"
android:layout_marginTop="240dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="526dp"
android:text="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonsub"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="227dp"
android:layout_marginTop="240dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="532dp"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
21
<Button
android:id="@+id/buttondot"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="10dp"
android:layout_marginTop="290dp"
android:layout_marginEnd="256dp"
android:layout_marginBottom="496dp"
android:text="."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button0"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="47dp"
android:layout_marginTop="290dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="493dp"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonequal"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="147dp"
android:layout_marginTop="290dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="526dp"
android:text="="
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonplus"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="227dp"
android:layout_marginTop="290dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="532dp"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttoncan"
22
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="227dp"
android:layout_marginTop="340dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="532dp"
android:text="C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.lab2a;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
EditText inputnum;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = findViewById(R.id.button0);
23
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
button6 = findViewById(R.id.button6);
button7 = findViewById(R.id.button7);
button8 = findViewById(R.id.button8);
button9 = findViewById(R.id.button9);
button10 = findViewById(R.id.buttondot);
buttonAdd = findViewById(R.id.buttonplus);
buttonSub = findViewById(R.id.buttonsub);
buttonDivision = findViewById(R.id.buttondiv);
buttonMul = findViewById(R.id.buttonmul);
buttonC = findViewById(R.id.buttoncan);
buttonEqual = findViewById(R.id.buttonequal);
inputnum = findViewById(R.id.editText4);
result = findViewById(R.id.textView8);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "6");
}
24
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + "9");
}
});
button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputnum.setText(inputnum.getText() + ".");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = ADDITION;
result.setText(decimalFormat.format(mValueOne) + "+");
inputnum.setText(null);
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
result.setText(decimalFormat.format(mValueOne) + "-");
inputnum.setText(null);
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
result.setText(decimalFormat.format(mValueOne) + "*");
inputnum.setText(null);
}
});
buttonDivision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVISION;
25
result.setText(decimalFormat.format(mValueOne) + "/");
inputnum.setText(null);
}
});
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(inputnum.getText().length() > 0) {
CharSequence currentText = inputnum.getText();
inputnum.setText(currentText.subSequence(0, currentText.length()-
1));
}
else {
mValueOne = Double.NaN;
mValueTwo = Double.NaN;
inputnum.setText("");
result.setText("");
}
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
result.setText(result.getText().toString() +
decimalFormat.format(mValueTwo) + " = " +
decimalFormat.format(mValueOne));
mValueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
if(CURRENT_ACTION == ADDITION)
mValueOne = this.mValueOne + mValueTwo;
else if(CURRENT_ACTION == SUBTRACTION)
mValueOne = this.mValueOne - mValueTwo;
else if (CURRENT_ACTION == MULTIPLICATION)
mValueOne = this.mValueOne * mValueTwo;
else if (CURRENT_ACTION == DIVISION)
mValueOne = this.mValueOne / mValueTwo;
}
else
{
try
{
mValueOne = Double.parseDouble(inputnum.getText().toString());
}
catch (Exception e)
{
}
}
}
}
26
Output:
3. Create a SIGN Up activity with Username and Password. Validation of password should
happen based on the following rules:
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”
27
and disable the SIGN IN button. Use Bundle to transfer information from one activity to
another.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="41dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="172dp"
android:text="SIGN UP ACTIVITY"
android:textColor="@color/colorAccent"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="98dp"
android:layout_marginEnd="346dp"
android:layout_marginBottom="604dp"
android:text="Username:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editText3"
android:layout_width="166dp"
android:layout_height="39dp"
android:layout_marginStart="124dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="54dp"
android:layout_marginBottom="610dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
28
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="148dp"
android:layout_marginEnd="346dp"
android:layout_marginBottom="604dp"
android:text="Password:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editText1"
android:layout_width="166dp"
android:layout_height="39dp"
android:layout_marginStart="124dp"
android:layout_marginTop="138dp"
android:layout_marginEnd="54dp"
android:layout_marginBottom="610dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="108dp"
android:layout_marginTop="190dp"
android:layout_marginEnd="215dp"
android:layout_marginBottom="517dp"
android:text="SIGN UP"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
29
package com.example.lab3a;
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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Button signup;
EditText username, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.editText3);
password = findViewById(R.id.editText1);
signup = findViewById(R.id.button);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(password.getText().toString().length()>=8 &&
validatePassword(password.getText().toString())){
Toast.makeText(getBaseContext(),"Successful Sign
UP",Toast.LENGTH_LONG).show();
Intent it = new Intent(getBaseContext(), login_activity.class);
startActivity(it);
}else{
Toast.makeText(getBaseContext(),"not
VALID",Toast.LENGTH_LONG).show();
}
}
});
}
return matcher.matches();
30
}
}
Following is the content of res/layout/login_activity.xml. After successful login activtity it
moves to success activity.
login_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="41dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="172dp"
android:text="LOGIN ACTIVITY"
android:textColor="@color/colorAccent"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="98dp"
android:layout_marginEnd="346dp"
android:layout_marginBottom="604dp"
android:text="Username:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/l_username"
android:layout_width="166dp"
android:layout_height="39dp"
android:layout_marginStart="124dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="54dp"
android:layout_marginBottom="610dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
31
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="148dp"
android:layout_marginEnd="346dp"
android:layout_marginBottom="604dp"
android:text="Password:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/l_password"
android:layout_width="166dp"
android:layout_height="39dp"
android:layout_marginStart="124dp"
android:layout_marginTop="138dp"
android:layout_marginEnd="54dp"
android:layout_marginBottom="610dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button_signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="108dp"
android:layout_marginTop="190dp"
android:layout_marginEnd="215dp"
android:layout_marginBottom="517dp"
android:text="SIGN IN"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
login_activity.java
package com.example.lab3a;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
32
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Button lsignin;
EditText lusername, lpassword;
int counter = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
lusername = findViewById(R.id.l_username);
lpassword = findViewById(R.id.l_password);
lsignin = findViewById(R.id.button_signin);
lsignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle b = getIntent().getExtras();
String suser = b.getString("usern");
String spass = b.getString("pass");
if(suser.toString().equals(lusername.getText().toString()) &&
spass.toString().equals(lpassword.getText().toString()))
{
Intent it = new Intent(getBaseContext(), success.class);
startActivity(it);
}
else
{
Toast.makeText(getBaseContext(),"LOGIN
FAILED",Toast.LENGTH_LONG).show();
}
counter--;
if(counter == 0)
{
Toast.makeText(getBaseContext(),"FAILED LOGIN
ATTEMPTS",Toast.LENGTH_LONG).show();
lsignin.setEnabled(false);
}
}
});
}
}
success.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
33
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="163dp"
android:layout_marginTop="300dp"
android:layout_marginEnd="189dp"
android:layout_marginBottom="412dp"
android:text="SUCCESSFULL LOGIN"
android:textColor="@color/colorAccent"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
success.java
package com.example.lab3a;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
}
}
Output:
34
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.
First, create the android application as discussed in “Create your First Android Application”.
Copy the images and save the images in the drawable folder. Following is the content of the
modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHANGING WALLPAPER APPLICATION"
android:textColor="@color/colorAccent"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
35
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.063" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="53dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="590dp"
android:text="CLICK HERE TO CHANGE WALLPAPER"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.820"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Save five images (jpg format) in the drawable folder. In this example one.jpg, two.jpg,three.jpg,
four.jpg and five.jpg images are saved in drawable folder.
MainActivity.java
package com.example.lab4a;
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
Button changewallpaper;
Timer mytimer;
Drawable drawable;
WallpaperManager wpm;
int prev=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
36
mytimer = new Timer();
wpm = WallpaperManager.getInstance(this);
changewallpaper = findViewById(R.id.button);
changewallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setWallpaper();
}
});
}
mytimer.schedule(new TimerTask() {
@Override
public void run() {
if(prev==1) {
drawable = getResources().getDrawable(R.drawable.one);
prev = 2;
}
else if(prev==2) {
drawable = getResources().getDrawable(R.drawable.two);
prev=3;
}
else if(prev==3) {
drawable = getResources().getDrawable(R.drawable.three);
prev=4;
}
else if(prev==4) {
drawable = getResources().getDrawable(R.drawable.four);
prev=5;
}
else if(prev==5) {
drawable = getResources().getDrawable(R.drawable.five);
prev=1;
}
try {
wpm.setBitmap(wallpaper);
}
catch (IOException e) {
e.printStackTrace();
}
}
},0,30000);
}
}
Output:
37
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.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COUNTER APPLICATION"
android:textColor="@color/colorAccent"
android:textSize="18sp"
38
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="79dp"
android:layout_marginBottom="596dp"
android:text="Counter Value"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:layout_marginTop="129dp"
android:layout_marginEnd="171dp"
android:layout_marginBottom="542dp"
android:text="START"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:layout_marginTop="191dp"
android:layout_marginEnd="171dp"
android:layout_marginBottom="542dp"
android:text="STOP"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
39
package com.example.lab5a;
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;
int i = 1;
long startTime, timeInMilliseconds = 0;
Handler customHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtcounter = findViewById(R.id.textView1);
btnstart = findViewById(R.id.btn_start);
btnstop = findViewById(R.id.btn_stop);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
customHandler.postDelayed(updateTimerThread,0);
}
});
btnstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
customHandler.removeCallbacks(updateTimerThread);
}
});
}
Output:
40
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.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PARSING XML AND JSON DATA"
41
android:textColor="@color/colorAccent"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.059" />
<Button
android:id="@+id/buttonxml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="33dp"
android:layout_marginBottom="574dp"
android:text="Parse XML Data"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.490"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />
<Button
android:id="@+id/buttonjson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="33dp"
android:layout_marginBottom="574dp"
android:text="Parse JSON Data"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.490"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />
<TextView
android:id="@+id/display_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="550dp"
android:layout_marginEnd="33dp"
android:layout_marginBottom="674dp"
android:text="Display Results"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.483"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
</androidx.constraintlayout.widget.ConstraintLayout>
42
Following is the content of the modified src/MainActivity.java.
MainActivity.java
package com.example.lab6a;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
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 java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayResult = findViewById(R.id.display_result);
parsexml = findViewById(R.id.buttonxml);
parsejson = findViewById(R.id.buttonjson);
parsexml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
InputStream is = getAssets().open("city.xml");
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
displayResult.setText(" ");
for (int i = 0; i < nList.getLength(); i++) {
43
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
});
parsejson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String json;
try
{
InputStream is = getAssets().open("example.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
displayResult.setText(" ");
}
catch(IOException e)
{
e.printStackTrace();
}
44
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Navigate to the Project Option. Click on app scr main folder. Right click on main new
Directory. Create a new directory named assets. Create a file example.json in the assets
folder. Following is the content of the file assets/example.json
example.json
[
{
"name": "Mysore ",
"lat": "12.295 ",
"long": "76.639 ",
"temperature":"22 ",
"humidity": "92 %"
},
{
"name": "Bangalore",
"lat": "12.97165 ",
"long": "77.5946 ",
"temperature":"25 ",
"humidity": "74 %"
}
]
Create a file city.xml in the assets folder. Following is the content of the file assets/city.xml
city.xml
<?xml version="1.0"?>
<records>
<place>
<name>Mysore</name>
<lat> 12.295 </lat>
<long>76.639 </long>
<temperature> 22 </temperature>
<humidity> 90 % </humidity>
</place>
<place>
<name>Bangalore</name>
<lat> 12.97165 </lat>
<long>77.5946 </long>
<temperature> 25 </temperature>
<humidity> 74 % </humidity>
45
</place>
</records>
Output:
7. Develop a simple application with one EditText so that the user can write some text in it.
Create a button called “Convert Text to Speech” that converts the user input text into
voice.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
46
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT TO SPEECH APPLICATION"
android:textColor="@color/colorAccent"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<EditText
android:id="@+id/txt_input"
android:layout_width="237dp"
android:layout_height="177dp"
android:layout_marginStart="100dp"
android:layout_marginTop="209dp"
android:layout_marginEnd="77dp"
android:layout_marginBottom="437dp"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_txt2spch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="399dp"
android:layout_marginEnd="51dp"
android:layout_marginBottom="284dp"
android:text="Convert Text to Speech"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.lab7a;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
47
TextToSpeech t1;
EditText txtinput;
Button txttospeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtinput = findViewById(R.id.txt_input);
txttospeech = findViewById(R.id.btn_txt2spch);
txttospeech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String tospeak = txtinput.getText().toString();
Toast.makeText(getBaseContext(),tospeak,Toast.LENGTH_SHORT).show();
t1.speak(tospeak,TextToSpeech.QUEUE_FLUSH,null);
}
});
}
Output
48
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.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL AND SAVE APPLICATION"
49
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.396"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<EditText
android:id="@+id/editText"
android:layout_width="155dp"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="57dp"
android:layout_marginEnd="97dp"
android:layout_marginBottom="589dp"
android:ems="5"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttondel"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="227dp"
android:layout_marginTop="57dp"
android:layout_marginEnd="125dp"
android:layout_marginBottom="532dp"
android:text="DEL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button1"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="35dp"
android:layout_marginTop="122dp"
android:layout_marginEnd="256dp"
android:layout_marginBottom="541dp"
android:text="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button2"
50
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="73dp"
android:layout_marginTop="122dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="541dp"
android:text="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button3"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="157dp"
android:layout_marginTop="122dp"
android:layout_marginEnd="122dp"
android:layout_marginBottom="526dp"
android:text="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button4"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="35dp"
android:layout_marginTop="172dp"
android:layout_marginEnd="256dp"
android:layout_marginBottom="541dp"
android:text="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button5"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="73dp"
android:layout_marginTop="172dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="541dp"
android:text="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button6"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="157dp"
51
android:layout_marginTop="172dp"
android:layout_marginEnd="122dp"
android:layout_marginBottom="526dp"
android:text="6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button7"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="35dp"
android:layout_marginTop="222dp"
android:layout_marginEnd="256dp"
android:layout_marginBottom="541dp"
android:text="7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button8"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="73dp"
android:layout_marginTop="222dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="541dp"
android:text="8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button9"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="157dp"
android:layout_marginTop="222dp"
android:layout_marginEnd="122dp"
android:layout_marginBottom="526dp"
android:text="9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonstar"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="35dp"
android:layout_marginTop="272dp"
android:layout_marginEnd="256dp"
android:layout_marginBottom="541dp"
52
android:text="*"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button0"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="73dp"
android:layout_marginTop="272dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="541dp"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonhash"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginStart="157dp"
android:layout_marginTop="272dp"
android:layout_marginEnd="122dp"
android:layout_marginBottom="526dp"
android:text="#"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttoncall"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_marginStart="38dp"
android:layout_marginTop="322dp"
android:layout_marginEnd="122dp"
android:layout_marginBottom="526dp"
android:text="CALL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/buttonsave"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_marginStart="127dp"
android:layout_marginTop="322dp"
android:layout_marginEnd="122dp"
android:layout_marginBottom="526dp"
android:text="SAVE"
app:layout_constraintBottom_toBottomOf="parent"
53
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.lab8a;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
EditText txtnumber;
int pid = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtnumber = findViewById(R.id.editText);
btn1 = findViewById(R.id.button1);
btn2 = findViewById(R.id.button2);
btn3 = findViewById(R.id.button3);
btn4 = findViewById(R.id.button4);
btn5 = findViewById(R.id.button5);
btn6 = findViewById(R.id.button6);
btn7 = findViewById(R.id.button7);
btn8 = findViewById(R.id.button8);
btn9 = findViewById(R.id.button9);
btn0 = findViewById(R.id.button0);
btncall = findViewById(R.id.buttoncall);
btnsave = findViewById(R.id.buttonsave);
btnstar = findViewById(R.id.buttonstar);
btnhash = findViewById(R.id.buttonhash);
btndelete = findViewById(R.id.buttondel);
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
54
txtnumber.append("0");
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("9");
}
});
btnstar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtnumber.append("*");
}
});
btnhash.setOnClickListener(new View.OnClickListener() {
@Override
55
public void onClick(View view) {
txtnumber.append("#");
}
});
btndelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num = txtnumber.getText().toString();
if (num.length() > 0) {
num = num.substring(0, num.length() - 1);
}
txtnumber.setText(num);
}
});
btncall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callatruntimepermission();
}
});
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num = txtnumber.getText().toString();
Intent it = new Intent(Intent.ACTION_INSERT,
ContactsContract.Contacts.CONTENT_URI);
it.putExtra(ContactsContract.Intents.Insert.PHONE,num);
startActivity(it);
}
});
}
}
else
{
String num = txtnumber.getText().toString();
Intent it = new Intent(Intent.ACTION_CALL);
it.setData(Uri.parse("tel:" + num));
startActivity(it);
}
if(requestCode==pid)
{
if(grantResult[0]==PackageManager.PERMISSION_GRANTED)
{
callatruntimepermission();
}
}
}
56
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission>
Output:
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.
First, create the android application as discussed in “Create your First Android Application”. On
pressing the “Insert Data into the Database” Button it moves to the insertdata activity and on
pressing the “Trigger an Alarm” it moves to the triggeralarm activity. Following is the content of
the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
57
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MEDICINE DATABASE"
android:textColor="@color/colorAccent"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:layout_marginTop="129dp"
android:layout_marginEnd="171dp"
android:layout_marginBottom="542dp"
android:text="INSERT DATA INTO THE DATABASE"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="152dp"
android:layout_marginRight="171dp" />
<Button
android:id="@+id/btn_trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:layout_marginTop="191dp"
android:layout_marginEnd="171dp"
android:layout_marginBottom="542dp"
android:text="TRIGGER AN ALARM"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="152dp"
android:layout_marginRight="171dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_insertdata.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
58
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MEDICINE DATABASE"
android:textColor="@color/colorAccent"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="71dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Medicine Name:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="71dp"
android:layout_marginRight="238dp" />
<EditText
android:id="@+id/editText"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginStart="211dp"
android:layout_marginTop="99dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="211dp"
android:layout_marginRight="100dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="159dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Date:"
59
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="10dp"
android:layout_marginRight="238dp" />
<EditText
android:id="@+id/editText1"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginStart="211dp"
android:layout_marginTop="149dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="211dp"
android:layout_marginRight="100dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="71dp"
android:layout_marginTop="219dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Time of the Day:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="71dp"
android:layout_marginRight="238dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginStart="211dp"
android:layout_marginTop="209dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="211dp"
60
android:layout_marginRight="100dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="260dp"
android:layout_marginEnd="175dp"
android:layout_marginBottom="393dp"
android:text="Insert"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="30dp"
android:layout_marginRight="175dp" />
<Button
android:id="@+id/buttonhome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="260dp"
android:layout_marginTop="260dp"
android:layout_marginEnd="175dp"
android:layout_marginBottom="393dp"
android:text="Go to Home Page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="260dp"
android:layout_marginRight="175dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_triggeractivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".triggeractivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Triggering an alarm"
android:textColor="@color/colorAccent"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
61
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Date : "
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="35dp"
android:layout_marginRight="238dp" />
<EditText
android:id="@+id/editText"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginStart="211dp"
android:layout_marginTop="99dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="211dp"
android:layout_marginRight="100dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="71dp"
android:layout_marginTop="159dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Time of Day:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="71dp"
android:layout_marginRight="238dp" />
<EditText
android:id="@+id/editText1"
android:layout_width="130dp"
62
android:layout_height="wrap_content"
android:layout_marginStart="211dp"
android:layout_marginTop="149dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="211dp"
android:layout_marginRight="100dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="200dp"
android:layout_marginEnd="175dp"
android:layout_marginBottom="393dp"
android:text="Trigger an Alarm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"
android:layout_marginLeft="150dp"
android:layout_marginRight="175dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Navigate to Android view. Right click on app New Android Resource Directory. Select the
Resource type as raw and click OK. Save an examples.mp3 file in the raw directory.
AlarmF.java
package com.example.example1b;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
mp=MediaPlayer.create(context, R.raw.examples);
mp.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
63
Following is the content of the src/insertdata.java to insert the data into the database.
insertdata.java
package com.example.example1b;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Button btninsert,btnhomepage;
EditText medname, meddate, medtime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insertdata);
medname = findViewById(R.id.editText);
meddate = findViewById(R.id.editText1);
medtime = findViewById(R.id.editText2);
btninsert = findViewById(R.id.button);
btnhomepage = findViewById(R.id.buttonhome);
btninsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
database.insert("Medicine",null,cv);
Toast.makeText(getBaseContext(),"Record inserted
successfully",Toast.LENGTH_LONG).show();
}
});
btnhomepage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent home = new Intent(insertdata.this,MainActivity.class);
startActivity(home);
64
}
});
}
}
Mainactivity.java
package com.example.example1b;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
btninsert = findViewById(R.id.btn_insert);
btntrigger = findViewById(R.id.btn_trigger);
btninsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(MainActivity.this,insertdata.class);
startActivity(it);
}
});
btntrigger.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent trigger = new Intent(MainActivity.this,triggeractivity.class);
startActivity(trigger);
}
});
}
}
MedicineHelper.java
package com.example.example1b;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
65
public static String DATABASE_NAME="MEDICINE.db";
public static String MEDICINE_TABLE="Medicine";
public static String MEDICINE_COLUMN1 = "name";
public static String MEDICINE_COLUMN2 = "date";
public static String MEDICINE_COLUMN3 = "time";
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table Medicine (name TEXT, date TEXT, time
TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
triggeractivity.java
package com.example.example1b;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
EditText txtdate,txtday;
Button btntrigger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_triggeractivity);
txtdate = findViewById(R.id.editText);
txtday = findViewById(R.id.editText1);
btntrigger = findViewById(R.id.button);
btntrigger.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(res.getCount() == 0)
{
Toast.makeText(getBaseContext(), "No DATA
",Toast.LENGTH_LONG).show();
}
while (res.moveToNext())
{
Intent intent = new Intent(triggeractivity.this, AlarmF.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager)
getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (1 * 1000), pendingIntent);
Toast.makeText(getBaseContext(), "Alarm set in 1
seconds",Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "Time to take "+
res.getString(0)+ " Medicine : "+res.getString(2),Toast.LENGTH_LONG).show();
}
}
});
}
}
Output:
67
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”.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MEETING SCHEDULE"
android:textColor="@color/colorAccent"
android:textSize="18sp"
68
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.077" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Date:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<EditText
android:id="@+id/editText"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginStart="211dp"
android:layout_marginTop="99dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="159dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Time:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<EditText
android:id="@+id/editText1"
android:layout_width="130dp"
android:layout_height="wrap_content"
69
android:layout_marginStart="211dp"
android:layout_marginTop="149dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="71dp"
android:layout_marginTop="219dp"
android:layout_marginEnd="238dp"
android:layout_marginBottom="600dp"
android:text="Meeting Agenda:"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<EditText
android:id="@+id/editText2"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginStart="211dp"
android:layout_marginTop="209dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="587dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="260dp"
android:layout_marginEnd="175dp"
android:layout_marginBottom="393dp"
android:text="Add Meeting Agenda"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071"/>
</androidx.constraintlayout.widget.ConstraintLayout>
70
Following is the content of the modified src/MainActivity.java
MainActivity.java
package com.example.vtucontentprovider;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Button addagenda;
EditText date, time, meetingagenda;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addagenda = findViewById(R.id.button);
date = findViewById(R.id.editText);
time = findViewById(R.id.editText1);
meetingagenda = findViewById(R.id.editText2);
addagenda.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String addage = meetingagenda.getText().toString();
String adddate = date.getText().toString();
String addtime = time.getText().toString();
Toast.makeText(getBaseContext(),addage,Toast.LENGTH_SHORT).show();
Uri id =
getContentResolver().insert(Uri.parse("content://com.example.vtucontentprovider.Agenda
Provider/agenda"),values);
Toast.makeText(getBaseContext(),id.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
71
Create a new java class file. Following is the content of the modified src/AgendaProvider.java
that extends ContentProvider class.
AgendaProvider.java
package com.example.vtucontentprovider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.widget.Toast;
SQLiteDatabase db;
MeetingDb dbHelper;
@Override
public boolean onCreate() {
dbHelper = new MeetingDb(getContext(),MeetingDb.DATABASE_NAME+".db",null,1);
if(db != null) {
return true;
}
return false;
}
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String
s1) {
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
db = dbHelper.getWritableDatabase();
long id = db.insert(MeetingDb.TABLE_NAME,null, contentValues);
if(id>0)
{
72
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, id);
getContext().getContentResolver().notifyChange(_uri,null);
db.close();
return _uri;
}
else
{
Toast.makeText(getContext(),"ROW INSERT
FAILED",Toast.LENGTH_SHORT).show();
db.close();
return null;
}
}
@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[]
strings) {
return 0;
}
}
MeetingDb.java
package com.example.vtucontentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table agenda (agenda_date TEXT, agenda_time
TEXT, agenda_content TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
Output:
73
Content Resolver part
Create another android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MEETING INFO"
android:textColor="@color/colorAccent"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.457"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<TextView
android:id="@+id/textView"
74
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="297dp"
android:layout_marginBottom="596dp"
android:text="Pick a date to get meeting info:"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/calendarView"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="275dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
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_marginStart="31dp"
android:layout_marginTop="350dp"
android:layout_marginEnd="171dp"
android:layout_marginBottom="330dp"
android:text="Search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_disp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="280dp"
android:layout_marginEnd="90dp"
android:layout_marginBottom="80dp"
android:text="display result"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
75
Following is the content of the modified src/MainActivity.java
MainActivity.java
package com.example.vtucontentresolver;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
Button search;
EditText txtdatepicker;
TextView txtdisplay;
DatePickerDialog picker;
String URL = "content://com.example.vtucontentprovider.AgendaProvider/agenda";
Uri var = Uri.parse(URL);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtdisplay = findViewById(R.id.txt_disp);
txtdatepicker = findViewById(R.id.calendarView);
txtdatepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
search = findViewById(R.id.button);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String where="agenda_date=?";
Cursor cursor =
getContentResolver().query(Uri.parse("content://com.example.vtucontentprovider.AgendaP
rovider/agenda"), mProjection, where, new String[]{searchdate},null);
txtdisplay.setText(" ");
String res = " ";
if(cursor.getCount() == 0)
{
Toast.makeText(getBaseContext(),"No Data Available",
Toast.LENGTH_LONG).show();
}
res = res + "\n" + adate +" "+ " " + atime + " " +
acontent ;
Toast.makeText(getBaseContext(), res,
Toast.LENGTH_LONG).show();
}
txtdisplay.setText(res);
}
});
}
}
Output:
77
78
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.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS APPLICATION"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.041" />
<TextView
android:id="@+id/txtnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="70dp"
android:layout_marginBottom="587dp"
android:text="Display SMS Number"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_marginLeft="90dp"
android:layout_marginRight="70dp" />
<TextView
android:id="@+id/txtmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="115dp"
android:layout_marginEnd="70dp"
android:layout_marginBottom="587dp"
android:text="Display SMS Message"
android:textStyle="bold"
79
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_marginLeft="90dp"
android:layout_marginRight="70dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.smsreplied;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.perm
ission.RECEIVE_SMS))
{
//Do not do anything
}
else
{
ActivityCompat.requestPermissions(MainActivity.this,new
String[]{Manifest.permission.RECEIVE_SMS},MY_PERMISSION_REQUEST_RECEIVE_SMS);
}
}
Bundle b = getIntent().getBundleExtra("data");
txtnumber = findViewById(R.id.txtnumber);
txtmessage = findViewById(R.id.txtmessage);
if(b!=null){
String number=b.getString("number");
String content=b.getString("content");
80
txtnumber.setText(number);
txtmessage.setText(content);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[],
int[] grantResults )
{
switch (requestCode)
{
case MY_PERMISSION_REQUEST_RECEIVE_SMS:
{
if(grantResults.length >0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this,"Thank you for
permitting",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this,"Cant do anything until you permit
me",Toast.LENGTH_LONG).show();
}
}
}
}
}
MySMSReceiver.java
package com.example.smsreplied;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.provider.Telephony.Sms;
import android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() == SMS_RECEIVED)
{
Bundle bundle = intent.getExtras();
81
String senderNumber = null;
String senderMessage = null;
for(int i = 0; i < pdus.length; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
smsm[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
} else {
smsm[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" ></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
Output: Run the emulator and click on Extended Controls (….). Select the Phone tab. Enter the
phone number and the message and click Send
82
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”.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FILE APPLICATION"
android:textColor="@color/colorAccent"
android:textSize="24sp"
android:textStyle="bold"
83
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.058" />
<Button
android:id="@+id/buttoncreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="62dp"
android:layout_marginTop="87dp"
android:layout_marginEnd="241dp"
android:layout_marginBottom="576dp"
android:text="Create"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.058" />
<Button
android:id="@+id/buttonopen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="169dp"
android:layout_marginTop="87dp"
android:layout_marginEnd="114dp"
android:layout_marginBottom="576dp"
android:text="Open"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.058" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_marginStart="58dp"
android:layout_marginTop="133dp"
android:layout_marginEnd="135dp"
android:layout_marginBottom="513dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.058" />
<Button
android:id="@+id/buttonsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="121dp"
android:layout_marginTop="270dp"
android:layout_marginEnd="202dp"
android:layout_marginBottom="372dp"
android:text="Save"
84
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.058" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.lab4b;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtinput = findViewById(R.id.editText);
btncreate = findViewById(R.id.buttoncreate);
btnopen = findViewById(R.id.buttonopen);
btnsave = findViewById(R.id.buttonsave);
btncreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
fos = openFileOutput(FILE_NAME,MODE_PRIVATE);
fos.write(text.getBytes());
txtinput.getText().clear();
85
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fos != null)
{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
btnopen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
txtinput.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (fis != null)
{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = txtinput.getText().toString();
if (text.isEmpty()) {
Toast.makeText(MainActivity.this, "First Create the File",
Toast.LENGTH_LONG).show();
} else {
txtinput.getText().clear();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fss != null) {
try {
fss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
});
}
}
Output:
87
The example.txt file will be saved to /data/user/0/com.example.lab4b/files/example.txt. To
check the existence of the file click on View Tool Windows Device File Explorer. In this
window check the folder /data/user/0/com.example.lab4b/files.
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.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MEDIA PLAYER APPLICATION"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="160dp"
android:layout_marginEnd="208dp"
android:layout_marginBottom="613dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<Button
android:id="@+id/buttonbackward"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="64dp"
android:layout_marginTop="203dp"
android:layout_marginEnd="249dp"
android:layout_marginBottom="520dp"
android:text="B"
android:textSize="18sp"
android:textStyle="bold"
88
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<Button
android:id="@+id/buttonforward"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="90dp"
android:layout_marginTop="203dp"
android:layout_marginEnd="136dp"
android:layout_marginBottom="520dp"
android:text="F"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<Button
android:id="@+id/buttonpause"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="290dp"
android:layout_marginTop="203dp"
android:layout_marginEnd="249dp"
android:layout_marginBottom="520dp"
android:text="P"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<Button
android:id="@+id/buttonplay"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="310dp"
android:layout_marginTop="203dp"
android:layout_marginEnd="136dp"
android:layout_marginBottom="520dp"
android:text="PL"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
89
android:id="@+id/textView2"
android:layout_marginStart="5dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="208dp"
android:layout_marginBottom="613dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView3"
android:layout_marginStart="400dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="208dp"
android:layout_marginBottom="613dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView4"
android:layout_marginStart="192dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="208dp"
android:layout_marginBottom="613dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.443"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />
</androidx.constraintlayout.widget.ConstraintLayout>
Navigate to Android view. Right click on app New Android Resource Directory. Select the
Resource type as raw and click OK. Save Song.mp3 file in the raw directory.
MainActivity.java
package com.example.lab5b;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
90
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnback = findViewById(R.id.buttonbackward);
btnfor = findViewById(R.id.buttonforward);
btnpause = findViewById(R.id.buttonpause);
btnplay = findViewById(R.id.buttonplay);
txt1 = (TextView)findViewById(R.id.textView2);
txt2 = (TextView)findViewById(R.id.textView3);
txt3 = (TextView)findViewById(R.id.textView4);
txt3.setText("Song.mp3");
seekbar = (SeekBar)findViewById(R.id.seekBar2);
seekbar.setClickable(false);
btnpause.setEnabled(false);
btnplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Playing
sound",Toast.LENGTH_SHORT).show();
if(mediaplayer == null)
{
mediaplayer = MediaPlayer.create(getBaseContext(), R.raw.song);
mediaplayer.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
}
});
finalTime = mediaplayer.getDuration();
startTime = mediaplayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
91
}
}
mediaplayer.start();
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
finalTime)))
);
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(UpdateSongTime,100);
btnpause.setEnabled(true);
btnplay.setEnabled(false);
}
});
btnpause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Pausing
sound",Toast.LENGTH_SHORT).show();
mediaplayer.pause();
btnpause.setEnabled(false);
btnplay.setEnabled(true);
}
});
btnfor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int temp = (int)startTime;
if((temp+forwardTime)<=finalTime){
startTime = startTime + forwardTime;
mediaplayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"You have Jumped forward 5
seconds",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Cannot jump forward 5
seconds",Toast.LENGTH_SHORT).show();
}
}
});
btnback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int temp = (int)startTime;
if((temp-backwardTime)>0){
startTime = startTime - backwardTime;
92
mediaplayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"You have Jumped backward 5
seconds",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Cannot jump backward 5
seconds",Toast.LENGTH_SHORT).show();
}
}
});
Output:
93
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”.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ASYNCHRONOUS TASK"
android:textColor="@color/colorAccent"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053" />
<Button
android:id="@+id/buttonstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="102dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="152dp"
android:layout_marginBottom="572dp"
android:text="Start Task"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0471"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="102dp"
android:layout_marginRight="152dp" />
<Button
android:id="@+id/buttonstop"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginStart="102dp"
android:layout_marginTop="152dp"
android:layout_marginEnd="152dp"
94
android:layout_marginBottom="572dp"
android:text="End Task "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0471"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="102dp"
android:layout_marginRight="152dp" />
<TextView
android:id="@+id/txtmarquee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demonstration of Asynchronous Task !!!!"
android:textSize="20sp"
android:textStyle="bold"
android:padding="10dp"
android:textColor="@color/colorAccent"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:layout_marginStart="50dp"
android:layout_marginTop="250dp"
android:layout_marginEnd="152dp"
android:layout_marginBottom="572dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0471"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:visibility="invisible"
android:layout_marginLeft="50dp"
android:layout_marginRight="152dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.marexample;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
TextView txtmarq;
Button btnstart, btnstop;
95
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtmarq = findViewById(R.id.txtmarquee);
btnstart = findViewById(R.id.buttonstart);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnstop = findViewById(R.id.buttonstop);
btnstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtmarq.setSelected(false);
txtmarq.setVisibility(View.INVISIBLE);
}
});
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getBaseContext(),"Async Task
Started!!!!!!!!",Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... strings) {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
txtmarq.setVisibility(View.VISIBLE);
txtmarq.setSelected(true);
}
}
Output:
96
7. Develop an application that makes use of the clipboard framework for copying and
pasting of the text. The activity consists of two EditText controls and two Buttons to trigger
the copy and paste functionality.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLIPBOARD ACTIVITY"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.453"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053" />
97
<EditText
android:id="@+id/copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="49dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="109dp"
android:layout_marginBottom="596dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053" />
<EditText
android:id="@+id/paste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="49dp"
android:layout_marginTop="146dp"
android:layout_marginEnd="109dp"
android:layout_marginBottom="500dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053" />
<Button
android:id="@+id/buttoncopy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="42dp"
android:layout_marginTop="223dp"
android:layout_marginEnd="261dp"
android:layout_marginBottom="450dp"
android:text=" Copy Text "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"/>
<Button
android:id="@+id/buttonpaste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="166dp"
android:layout_marginTop="223dp"
android:layout_marginEnd="110dp"
android:layout_marginBottom="450dp"
android:text="Paste Text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"/>
</androidx.constraintlayout.widget.ConstraintLayout>
98
Following is the content of the modified src/MainActivity.java
MainActivity.java
package com.example.lab7b;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
copyText = findViewById(R.id.copy);
pasteText = findViewById(R.id.paste);
btncopy = findViewById(R.id.buttoncopy);
btnpaste = findViewById(R.id.buttonpaste);
btncopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnpaste.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipData cd2 = cbm.getPrimaryClip();
ClipData.Item item = cd2.getItemAt(0);
String copied = item.getText().toString();
pasteText.setText(copied);
}
});
}
}
Output:
99
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 EditText 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 TextView. Also,
calculate the EMI by varying the Loan Term and Interest Rate values.
First, create the android application as discussed in “Create your First Android Application”.
Following is the content of the modified res/layout/activity_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
100
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAR EMI CALCULATOR"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="103dp"
android:layout_marginEnd="303dp"
android:layout_marginBottom="609dp"
android:text="Principal Amount:"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="100dp"
android:layout_marginRight="303dp" />
<EditText
android:id="@+id/editTextpa"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="86dp"
android:layout_marginBottom="600dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="150dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="82dp"
android:layout_marginTop="155dp"
android:layout_marginEnd="303dp"
android:layout_marginBottom="609dp"
101
android:text="Down Amount:"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="82dp"
android:layout_marginRight="303dp" />
<EditText
android:id="@+id/editTextdp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="138dp"
android:layout_marginBottom="600dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="150dp" />
<TextView
android:id="@+id/textViewir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="207dp"
android:layout_marginEnd="303dp"
android:layout_marginBottom="609dp"
android:text="Interest Rate:"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="80dp"
android:layout_marginRight="303dp" />
<EditText
android:id="@+id/editTextir"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="190dp"
android:layout_marginBottom="600dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="150dp" />
<TextView
android:id="@+id/textViewlt"
android:layout_width="wrap_content"
102
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="268dp"
android:layout_marginEnd="303dp"
android:layout_marginBottom="609dp"
android:text=" Loan Term(mths):"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="90dp"
android:layout_marginRight="303dp" />
<EditText
android:id="@+id/editTextlt"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="245dp"
android:layout_marginBottom="600dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="148dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginTop="280dp"
android:layout_marginEnd="184dp"
android:layout_marginBottom="170dp"
android:text="Calculate Monthly EMI"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="158dp"
android:layout_marginRight="184dp" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="220dp"
android:layout_marginTop="370dp"
android:layout_marginEnd="303dp"
android:layout_marginBottom="609dp"
android:text=" EMI: Result"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
103
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053"
android:layout_marginLeft="220dp"
android:layout_marginRight="303dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.aidldemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.IntentService;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
ICarService cService;
EditText prinamt, downamt, intrate,loanterm;
Button calculateemi;
TextView txtresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prinamt = findViewById(R.id.editTextpa);
downamt = findViewById(R.id.editTextdp);
intrate = findViewById(R.id.editTextir);
loanterm = findViewById(R.id.editTextlt);
calculateemi = findViewById(R.id.button);
txtresult = findViewById(R.id.textViewResult);
calculateemi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Float result =
cService.carcal(principalamount,downpayment,interestrate,lnterm);
txtresult.setText("Monthly EMI in Rs is :" +result);
} catch (RemoteException e) {
e.printStackTrace();
}
104
}
});
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
Navigate to Android view. Right click on app New AIDL AIDL file. Save the interface
as ICarService.aidl. Following is the modified content of the file ICarService.aidl
ICarService.aidl
// ICarService.aidl
package com.example.aidldemo;
interface ICarService {
float carcal(in float pa, in float dp, in float ir, in int lt);
}
Create a new java class file and name it as CarService.java. Following is the content of
src/CarService.java
CarService.java
package com.example.aidldemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return stub;
}
105
@Override
public float carcal(float pa, float dp, float ir, int lt) throws
RemoteException {
float emiamount;
float principalamt = pa - dp; //Subtract downpayment from principal
amount
float Interest = ((ir/12)/100); //Convert interest for permonth
and divide it by 100 to get a number
return emiamount;
}
};
}
Output:
106