0% found this document useful (0 votes)
1K views47 pages

Laboratory Manual: Mobile Application Development 18CSMP68

This document provides details about the Mobile Application Development course offered by the Department of Computer Science and Engineering at K S School of Engineering and Management. The course code is 18CSMP68 and has a prerequisite of Core Java. The course objectives are to learn Android programming, configure Android Studio, understand Android user interfaces, work with SQLite databases, share data using services, and develop several mobile applications demonstrating core Android concepts and skills. The syllabus outlines 8 programs to complete in Part A and 8 additional programs to complete in Part B as mini-projects, focusing on more advanced Android topics.

Uploaded by

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

Laboratory Manual: Mobile Application Development 18CSMP68

This document provides details about the Mobile Application Development course offered by the Department of Computer Science and Engineering at K S School of Engineering and Management. The course code is 18CSMP68 and has a prerequisite of Core Java. The course objectives are to learn Android programming, configure Android Studio, understand Android user interfaces, work with SQLite databases, share data using services, and develop several mobile applications demonstrating core Android concepts and skills. The syllabus outlines 8 programs to complete in Part A and 8 additional programs to complete in Part B as mini-projects, focusing on more advanced Android topics.

Uploaded by

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

K S School of Engineering and Management Mobile Application Development (18CSMP68)

Department of Computer Science and


Engineering

LABORATORY MANUAL

Mobile Application Development


18CSMP68

Dept. of CSE, KSSEM, 2020-21 Page 1


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Course Details

Course Name: Mobile Application Development


Course Code: 18CSMP68
Course prerequisite: Core Java

Course Objectives

Upon completion of this course, students are expected 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.

Dept. of CSE, KSSEM, 2020-21 Page 2


K S School of Engineering and Management Mobile Application Development (18CSMP68)

SYLLABUS
MOBILE APPLICATION DEVELOPMENT

Subject Code: 18CSMP68 IA Marks: 40


No. of Practical Hrs. / Week: 0:0:2 Exam Marks: 60
Total No. of Practical Hrs: 3 Hours/Week Exam Hours: 03
No. of Credits: 02

Descriptions (if any):

1. The installation procedure of the Android Studio/Java software must be demonstrated and
carried out in groups.
2. Students should use the latest version of Android Studio/Java/ Kotlin to execute these
programs. Diagrams given are for representational purposes only, students are expected to
improvise on them.
3. Part B programs should be developed as an application and are to be demonstrated as a
mini project in a group by adding extra features or the students can also develop their
application and demonstrate it as a mini-project. (Projects/programs are not limited to the
list given in Part B).

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

Dept. of CSE, KSSEM, 2020-21 Page 3


K S School of Engineering and Management Mobile Application Development (18CSMP68)

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

Program 3
Create a SIGN Up activity with Username and Password. Validation of password should happen
based on the following rules:
• Password should contain uppercase and lowercase letters.
• Password should contain letters and numbers.
• Password should contain special characters.
• Minimum length of the password (the default value is 8).
On successful SIGN UP proceed to the next Login activity. Here the user should SIGN IN using
the Username and Password created during signup activity. If the Username and Password are
matched then navigate to the next activity 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.

Dept. of CSE, KSSEM, 2020-21 Page 4


K S School of Engineering and Management Mobile Application Development (18CSMP68)

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

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

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

Dept. of CSE, KSSEM, 2020-21 Page 5


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Program 7
Develop a simple application with one Edit Text so that the user can write some text in it. Create
a button called “Convert Text to Speech” that converts the user input text into voice.

Program 8
Create an activity like a phone dialer with CALL and SAVE buttons. On pressing the CALL
button, it must call the phone number and on pressing the SAVE button it must save the number
to the phone contacts.

PART B

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

Dept. of CSE, KSSEM, 2020-21 Page 6


K S School of Engineering and Management Mobile Application Development (18CSMP68)

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

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

Program 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
“FirstCreate a File”.

Dept. of CSE, KSSEM, 2020-21 Page 7


K S School of Engineering and Management Mobile Application Development (18CSMP68)

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

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

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

Dept. of CSE, KSSEM, 2020-21 Page 8


K S School of Engineering and Management Mobile Application Development (18CSMP68)

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

Dept. of CSE, KSSEM, 2020-21 Page 9


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Course Outcomes
After successful completion of the Course, the participants will be able to

Create, test and debug Android application by setting up Android development


18CSMP68.1
environment.
Implement adaptive, responsive user interfaces that work across a wide range
18CSMP68.2
of devices.
Demonstrate methods in storing, sharing and retrieving data in Android
18CSMP68.3
applications.
18CSMP68.4 Infer the role of permissions and security for Android applications.

CO-PO Mapping

CO No. PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12

18CSMP68.1 3 2 2 - 2 1 - - 1 1 - 1
18CSMP68.2 3 2 2 - 2 1 - - 1 1 - 1
18CSMP68.3 3 2 2 - 2 1 - - 1 1 - 1
18CSMP68.4 3 2 2 - - - - 2 1 1 - 1

Dept. of CSE, KSSEM, 2020-21 Page 10


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Procedure to Conduct Practical Examination

Experiment distribution
• For laboratories having only one part: Students are allowed to pick one experiment from
the lot with equal opportunity.
• 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 (Courseed to change in accordance with university regulations)


• For laboratories having only one part –
Procedure + Execution + Viva-Voce: 15+70+15= 100 Marks
• 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

Dept. of CSE, KSSEM, 2020-21 Page 11


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1. Android Studio Tutorials


1.1 Install Android Studio and Packages:

Download Android Version 4.0.2 from the below link


https://redirector.gvt1.com/edgedl/android/studio/install/4.0.2.0/android-studio-ide-
193.6821437-windows.exe

1.2 Configure Android SDK packages:

Go to Tools  SDK Manager

Dept. of CSE, KSSEM, 2020-21 Page 12


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1.3 Creating Emulator

Go to Tools  Select AVD Manager

Dept. of CSE, KSSEM, 2020-21 Page 13


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Select Create Virtual Device Select Phone Pixel 2  Press Next

Select Android Q, if not already downloaded press download, After download completes Select
Q and Press Next Button.

Dept. of CSE, KSSEM, 2020-21 Page 14


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Enter AVD Name and Press Finish.

Press Play Button to Start Emulator

Dept. of CSE, KSSEM, 2020-21 Page 15


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Dept. of CSE, KSSEM, 2020-21 Page 16


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1.4 Creating a New Project in Android

While creating a New Project for First Time, make sure Android Studio is connected to internet,
It downloads the required packages from internet.
Go to File New New Project

Choose Phone and Tablet  Empty Activity  Press Next

Dept. of CSE, KSSEM, 2020-21 Page 17


K S School of Engineering and Management Mobile Application Development (18CSMP68)

In Configure your Project Screen, Enter below details and Press Finish Button.
Enter Name of the Application  This will be application name this will be visible with Home
Screen Icon.

Package Name  Enter package name atleast two identifier (Eg: com.example). Best Practice is
3 or more identifier (Eg: com.example.firstapp).

Save Location Location where to save the Project

Language  Choose Java

Minimum API Level  Android 5.0

Select Checkbox Use androidx.artifacts folder as below screenshot.

Dept. of CSE, KSSEM, 2020-21 Page 18


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1.5 Android Project Structure:

Select Project Explorer and Select Android from Project View

Dept. of CSE, KSSEM, 2020-21 Page 19


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Basic View:

2. Importing an Existing Project in Android Studio


3. Creating an Activity in Android
Right Click on Package  New  Activity Empty Activity

Enter Activity Name and Press Finish

Dept. of CSE, KSSEM, 2020-21 Page 20


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Dept. of CSE, KSSEM, 2020-21 Page 21


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1.6 Creating a Layout in Android

Right Click on Layout Folder  New  XMLLayout XML File

Enter xml file name and press Finish

Dept. of CSE, KSSEM, 2020-21 Page 22


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1.7 Creating Assets Folder in Android

Right Click on app folder  New  Folder  Assets Folder  Press Finish Button

Dept. of CSE, KSSEM, 2020-21 Page 23


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1.8 Creating File in assets Folder:

Right Click on assets folder  New  File

Enter filename with extension (Eg: abc.xml)

Dept. of CSE, KSSEM, 2020-21 Page 24


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Programs
PART A

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

1. Create a New Android Project with Empty Activity.


2. Open activity_main.xml file from res layout folder, check/add Linear Layout as the root
view.
3. Create layout using nested Relative Layout and TextView.
4. Use View background property to draw the line
5. Add Image to drawable folder and reference the image in the layout using
@drawable/<image_name>
6. Use android:layout_gravity/android:gravity properties to center the components.

Design

Dept. of CSE, KSSEM, 2020-21 Page 25


K S School of Engineering and Management Mobile Application Development (18CSMP68)
_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingTop="25dp"
android:paddingRight="20dp"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="59dp">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="31dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="K S SCHOOL OF ENGINEERING
AND MANAGEMENT"
android:textColor="#E61717"
android:textSize="20sp" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginLeft="11dp"
android:layout_marginBottom="0dp"
android:layout_toRightOf="@id/textView"
app:srcCompat="@drawable/KSSEM_logo" />

</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ashoka S"
android:textSize="16dp"

Dept. of CSE, KSSEM, 2020-21 Page 26


K S School of Engineering and Management Mobile Application Development (18CSMP68)

android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Assistant Professor"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="988683303”
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bangalore"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="[email protected] "
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center"
/>

</LinearLayout>

Dept. of CSE, KSSEM, 2020-21 Page 27


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Sample Output

<PUT SCREENSHOT OF OUTPUT WHICH YOU GOT AFTER


EXECUTION>

Dept. of CSE, KSSEM, 2020-21 Page 28


K S School of Engineering and Management Mobile Application Development (18CSMP68)

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

Dept. of CSE, KSSEM, 2020-21 Page 29


K S School of Engineering and Management Mobile Application Development (18CSMP68)

1. Create a New Android Project with Empty Activity.


2. Open activity_main.xml file from res layout folder, check/add Constraint Layout as the
root view.
3. Create Layout using Drag and Drop framework.
4. Open MainActivty.java file, Override onCreate() method and bring activity_main.xml file
on screen using setContentView() and bring the view references using findViewById()
method.
5. Add Listeners to Button Click Event:
6. Create a class which implements OnClickListener interface.
7. Override onClick() method of OnClickListener Interface.
8. Register the button for click event by calling setOnClickListener() method of View class
and pass the object of the class that implemented OnClickListener Interface.
9. Create a logic to Add/Subtract/Multiply/Divide to perform arithmetic operation on 2
operands (Eg: 10+20), If more than 2 operands or wrong input, display invalid input
messages.

Design

Dept. of CSE, KSSEM, 2020-21 Page 30


K S School of Engineering and Management Mobile Application Development (18CSMP68)

activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.andr
oid.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">

<Button
android:id="@+id/button_clear"
android:layout_width="87dp"
android:layout_height="53dp"
android:layout_marginTop="30dp"
android:text="C"
app:layout_constraintStart_toStartOf="@+id/button_add"
app:layout_constraintTop_toBottomOf="@+id/button_add" />

<Button
android:id="@+id/button_sub"
android:layout_width="87dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="-"
app:layout_constraintStart_toEndOf="@+id/button_three"
app:layout_constraintTop_toBottomOf="@+id/button_mul" />

<Button
android:id="@+id/button_add"
android:layout_width="87dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="+"
app:layout_constraintStart_toEndOf="@+id/button_equal"
app:layout_constraintTop_toBottomOf="@+id/button_sub" />

<Button
android:id="@+id/button_mul"
android:layout_width="87dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="*"
app:layout_constraintStart_toEndOf="@+id/button_six"
app:layout_constraintTop_toBottomOf="@+id/button_div" />

Dept. of CSE, KSSEM, 2020-21 Page 31


K S School of Engineering and Management Mobile Application Development (18CSMP68)

<Button
android:id="@+id/button_equal"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="="
app:layout_constraintStart_toEndOf="@+id/button_zero"
app:layout_constraintTop_toBottomOf="@+id/button_three" />

<Button
android:id="@+id/button_zero"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="0"
app:layout_constraintStart_toEndOf="@+id/button_dot"
app:layout_constraintTop_toBottomOf="@+id/button_two" />

<Button
android:id="@+id/button_dot"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="."
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_one" />

<Button
android:id="@+id/button_three"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="3"
app:layout_constraintStart_toEndOf="@+id/button_two"
app:layout_constraintTop_toBottomOf="@+id/button_six" />

<Button
android:id="@+id/button_two"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="2"
app:layout_constraintStart_toEndOf="@+id/button_one"
app:layout_constraintTop_toBottomOf="@+id/button_five" />

Dept. of CSE, KSSEM, 2020-21 Page 32


K S School of Engineering and Management Mobile Application Development (18CSMP68)

<Button
android:id="@+id/button_one"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_four" />

<Button
android:id="@+id/button_six"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="6"
app:layout_constraintStart_toEndOf="@+id/button_five"
app:layout_constraintTop_toBottomOf="@+id/button_nine" />

<Button
android:id="@+id/button_seven"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_result" />

<Button
android:id="@+id/button_eight"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="8"
app:layout_constraintStart_toEndOf="@+id/button_seven"
app:layout_constraintTop_toBottomOf="@+id/txt_result" />

<Button
android:id="@+id/button_nine"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="9"
app:layout_constraintStart_toEndOf="@+id/button_eight"
app:layout_constraintTop_toBottomOf="@+id/txt_result" />

Dept. of CSE, KSSEM, 2020-21 Page 33


K S School of Engineering and Management Mobile Application Development (18CSMP68)

<Button
android:id="@+id/button_four"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_seven" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="SIMPLE CALCULATOR"
android:textSize="26dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="Result"
android:textSize="18dp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText
android:id="@+id/txt_result"
android:layout_width="310dp"
android:layout_height="46dp"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<Button
android:id="@+id/button_div"
android:layout_width="87dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"

Dept. of CSE, KSSEM, 2020-21 Page 34


K S School of Engineering and Management Mobile Application Development (18CSMP68)

android:text="/"
app:layout_constraintStart_toEndOf="@+id/button_nine"
app:layout_constraintTop_toBottomOf="@+id/txt_result" />

<Button
android:id="@+id/button_five"
android:layout_width="62dp"
android:layout_height="53dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:text="5"
app:layout_constraintStart_toEndOf="@+id/button_four"
app:layout_constraintTop_toBottomOf="@+id/button_eight" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.partaprogram2;

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.util.regex.Pattern;

public class MainActivityextendsAppCompatActivityimplementsView.OnClickListener {


Button btnOne, btnTwo, btnThree, btnFour, btnFive, btnSix;
Button btnSeven, btnEight, btnNine, btnZero;
Button btnAdd,btnSub,btnMul,btnDiv;
Button btnClear,btnEqual,btnDot;

EditTexttxtResult;

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

btnOne=(Button)findViewById(R.id.button_one);
btnOne.setOnClickListener(this);

btnTwo=(Button)findViewById(R.id.button_two);
btnTwo.setOnClickListener(this);

btnThree=(Button)findViewById(R.id.button_three);
btnThree.setOnClickListener(this);

Dept. of CSE, KSSEM, 2020-21 Page 35


K S School of Engineering and Management Mobile Application Development (18CSMP68)

btnFour=(Button)findViewById(R.id.button_four);
btnFour.setOnClickListener(this);

btnFive=(Button)findViewById(R.id.button_five);
btnFive.setOnClickListener(this);

btnSix=(Button)findViewById(R.id.button_six);
btnSix.setOnClickListener(this);

btnSeven=(Button)findViewById(R.id.button_seven);
btnSeven.setOnClickListener(this);

btnEight=(Button)findViewById(R.id.button_eight);
btnEight.setOnClickListener(this);

btnNine=(Button)findViewById(R.id.button_nine);
btnNine.setOnClickListener(this);

btnZero=(Button)findViewById(R.id.button_zero);
btnZero.setOnClickListener(this);

btnAdd=(Button)findViewById(R.id.button_add);
btnAdd.setOnClickListener(this);

btnSub=(Button)findViewById(R.id.button_sub);
btnSub.setOnClickListener(this);

btnMul=(Button)findViewById(R.id.button_mul);
btnMul.setOnClickListener(this);

btnDiv=(Button)findViewById(R.id.button_div);
btnDiv.setOnClickListener(this);

btnClear=(Button)findViewById(R.id.button_clear);
btnClear.setOnClickListener(this);

btnEqual=(Button)findViewById(R.id.button_equal);
btnEqual.setOnClickListener(this);

btnDot=(Button)findViewById(R.id.button_dot);
btnDot.setOnClickListener(this);

txtResult=(EditText)findViewById(R.id.txt_result);
txtResult.setText("");
}

Dept. of CSE, KSSEM, 2020-21 Page 36


K S School of Engineering and Management Mobile Application Development (18CSMP68)

public void onClick(View v)


{
if(v.equals(btnOne))
txtResult.append("1");
if(v.equals(btnTwo))
txtResult.append("2");
if(v.equals(btnThree))
txtResult.append("3");
if(v.equals(btnFour))
txtResult.append("4");
if(v.equals(btnFive))
txtResult.append("5");
if(v.equals(btnSix))
txtResult.append("6");
if(v.equals(btnSeven))
txtResult.append("7");
if(v.equals(btnEight))
txtResult.append("8");
if(v.equals(btnNine))
txtResult.append("9");
if(v.equals(btnZero))
txtResult.append("0");
if(v.equals(btnDot))
txtResult.append(".");
if(v.equals(btnClear))
txtResult.setText("");

if(v.equals(btnEqual))
{
try {

String data = txtResult.getText().toString();


if (data.contains("/")) {
String[] operands = data.split("/");
if(operands.length==2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double result = operand1 / operand2;
txtResult.setText(String.valueOf(result));
}
else
{
Toast.makeText(getBaseContext(),"Invalid Input",
Toast.LENGTH_LONG).show();
}

}
else if (data.contains("*")) {
String[] operands = data.split(Pattern.quote("*"));
if(operands.length==2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
Dept. of CSE, KSSEM, 2020-21 Page 37
K S School of Engineering and Management Mobile Application Development (18CSMP68)

double result = operand1 * operand2;


txtResult.setText(String.valueOf(result));
}
else
{
Toast.makeText(getBaseContext(),"Invalid Input",
Toast.LENGTH_LONG).show();
}

}
else if (data.contains("+")) {
String[] operands = data.split(Pattern.quote("+"));
if(operands.length==2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double result = operand1 + operand2;
txtResult.setText(String.valueOf(result));
}
else
{
Toast.makeText(getBaseContext(),"Invalid Input",
Toast.LENGTH_LONG).show();
}
}
else if (data.contains("-")) {
String[] operands = data.split("-");
if(operands.length==2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double result = operand1 - operand2;
txtResult.setText(String.valueOf(result));
}
else
{
Toast.makeText(getBaseContext(),"Invalid Input",
Toast.LENGTH_LONG).show();
}
}
}

catch(Exception e) {
Toast.makeText(getBaseContext(),"Invalid Input",
Toast.LENGTH_LONG).show();
}
}

Dept. of CSE, KSSEM, 2020-21 Page 38


K S School of Engineering and Management Mobile Application Development (18CSMP68)

if(v.equals(btnAdd))
txtResult.append("+");
if(v.equals(btnSub))
txtResult.append("-");
if(v.equals(btnMul))
txtResult.append("*");
if(v.equals(btnDiv))
txtResult.append("/");
}
}

Sample Output

<PUT SCREENSHOT OF OUTPUT WHICH YOU GOT AFTER


EXECUTION>

Dept. of CSE, KSSEM, 2020-21 Page 39


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Program 3
Create a SIGN Up activity with Username and Password. Validation of password should happen based on
the following rules:
• Password should contain uppercase and lowercase letters.
• Password should contain letters and numbers.
• Password should contain special characters.
• Minimum length of the password (the default value is 8).
On successful SIGN UP proceed to the next Login activity. Here the user should SIGN IN using the
Username and Password created during signup activity. If the Username and Password are matched then
navigate to the next activity 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.

1. Create a New Android Project with Empty Activity.


2. Open activity_main.xml file from res layout folder, check/add Constraint Layout as the
root view.
3. Create Signup Layout using Drag and Drop framework design the layout.
4. Create One more Empty Activity LoginActivity using Android Studio Create Activity
Flow (Refer Android Studio Tutorial)
5. Open activity_login.xml file from reslayout folder, check/add Constraint Layout as the
root view.
6. Create Login Layout using Drag and Drop framework.
7. Add Listeners to Button Click Event:
• Create a class which implements OnClickListener interface.
• Override onClick() method of OnClickListener Interface.
• Register the button for click event by calling setOnClickListener() method of View
class and pass the object of the class that implemented OnClickListener Interface.
8. Use Regular Expression"^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!])[A-Za-
z\\d@$!]{8,}$” to validate the password.

Dept. of CSE, KSSEM, 2020-21 Page 40


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Design

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.andr
oid.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/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="SIGN UP"
android:textColor="@android:color/background_dark"
android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Dept. of CSE, KSSEM, 2020-21 Page 41


K S School of Engineering and Management Mobile Application Development (18CSMP68)

android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:text="USERNAME"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
android:id="@+id/textView4"
android:layout_width="82dp"
android:layout_height="34dp"
android:layout_marginTop="50dp"
android:text="PASSWORD"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

<EditText
android:id="@+id/txt_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3" />

<EditText
android:id="@+id/txt_password"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/txt_username"
app:layout_constraintStart_toStartOf="@+id/txt_username"
app:layout_constraintTop_toBottomOf="@+id/txt_username" />

<Button
android:id="@+id/btn_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Sign Up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_password" />
</androidx.constraintlayout.widget.ConstraintLayout>

Dept. of CSE, KSSEM, 2020-21 Page 42


K S School of Engineering and Management Mobile Application Development (18CSMP68)

Activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.andr
oid.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=".LoginActivity">

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:text="Username"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7" />

<EditText
android:id="@+id/txt_login_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="@+id/textView9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toTopOf="@+id/textView9" />

<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"

Dept. of CSE, KSSEM, 2020-21 Page 43


K S School of Engineering and Management Mobile Application Development (18CSMP68)

android:text="PASSWORD"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView9" />

<EditText
android:id="@+id/txt_login_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/txt_login_username"
app:layout_constraintStart_toStartOf="@+id/txt_login_username"
app:layout_constraintTop_toTopOf="@+id/textView10" />

<Button
android:id="@+id/btn_login_signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_login_password" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.parta.program3;

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;

public class MainActivityextends AppCompatActivityimplements View.OnClickListener {

EditTexttxtUsername;
EditTexttxtPassword;

Button btnSignup;

String regularExpression="^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!])[A-Za-
z\\d@$!]{8,}$";

Dept. of CSE, KSSEM, 2020-21 Page 44


K S School of Engineering and Management Mobile Application Development (18CSMP68)

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtUsername=(EditText)findViewById(R.id.txt_username);
txtPassword=(EditText)findViewById(R.id.txt_password);

btnSignup=(Button)findViewById(R.id.btn_signup);
btnSignup.setOnClickListener(this);

public void onClick(View v)


{
String username=txtUsername.getText().toString();
String password=txtPassword.getText().toString();

if(validatePassword(password)) {
Bundle bundle = new Bundle();
bundle.putString("user", username);
bundle.putString("Madlab@2021", password);

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


it.putExtra("data", bundle);

startActivity(it);
}
else
{

Toast.makeText(getBaseContext(), "Invalid Password",


Toast.LENGTH_LONG).show();
}
}

public booleanvalidatePassword(String password)


{
Pattern pattern= Pattern.compile(regularExpression);
Matcher matcher=pattern.matcher(password);
return matcher.matches();

}
}

Dept. of CSE, KSSEM, 2020-21 Page 45


K S School of Engineering and Management Mobile Application Development (18CSMP68)

LoginActivity.java
package com.example.parta.program3;

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;

public class LoginActivityextends AppCompatActivityimplements View.OnClickListener {

EditTexttxtLoginUsername;
EditTexttxtLoginPassword;
Button btnLogin;

String user,pass;

int count=0;

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

txtLoginUsername=(EditText)
findViewById(R.id.txt_login_username);
txtLoginPassword=(EditText)
findViewById(R.id.txt_login_password);

btnLogin=(Button)findViewById(R.id.btn_login_signin);
btnLogin.setOnClickListener(this);

Bundle bundle=getIntent().getBundleExtra("data");
user=bundle.getString("user");
pass=bundle.getString("Madlab@2021");

public void onClick(View v)


{
String user1=txtLoginUsername.getText().toString();
String pass1=txtLoginPassword.getText().toString();

if(user.equals(user1)&&pass.equals(pass1))
{
Toast.makeText(this,"Login Successful"
,Toast.LENGTH_LONG).show();
}
else

Dept. of CSE, KSSEM, 2020-21 Page 46


K S School of Engineering and Management Mobile Application Development (18CSMP68)

{
count++;
if(count==3)
{
btnLogin.setEnabled(false);
Toast.makeText(this,
"Failed Login Attempts"
,Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this,"Login Failed "+count
,Toast.LENGTH_LONG).show();
}
}

}
}

Sample Output

<PUT SCREENSHOT OF OUTPUT WHICH YOU GOT AFTER


EXECUTION>

Dept. of CSE, KSSEM, 2020-21 Page 47

You might also like