0% found this document useful (0 votes)
226 views

18CSMP68 Mobile Application Development Lab Manual in Kotlin

The document provides code for developing two mobile applications: 1) A visiting card application with fields for name, title, contact details etc. laid out in a MaterialCardView. 2) A basic calculator app with buttons for numbers, operators and a display for the expression and result. Both apps are built using Kotlin and XML layouts with constraints, textviews and buttons.

Uploaded by

Kishan N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
226 views

18CSMP68 Mobile Application Development Lab Manual in Kotlin

The document provides code for developing two mobile applications: 1) A visiting card application with fields for name, title, contact details etc. laid out in a MaterialCardView. 2) A basic calculator app with buttons for numbers, operators and a display for the expression and result. Both apps are built using Kotlin and XML layouts with constraints, textviews and buttons.

Uploaded by

Kishan N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

NIE INSTITUTE OF TECHNOLOGY, MYSURU

“MOBILE APPLICATION DEVELOPMENT


LAB MANUAL”
[18CSMP68]
As per VTU Revised Syllabus
Language Used: Kotlin

PREPARED BY:
Prof. DEEPAK P
Asst. Professor, Department of Computer Science & Engineering
NIEIT, Mysuru
Email: [email protected]

1|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU
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.

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

<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="260dp"
android:layout_margin="8dp"
android:elevation="8dp"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"

2|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/title_box"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:text="@string/nieit"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"

3|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/materialTextView"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatar"
app:srcCompat="@drawable/nieit_logo1"/>
</androidx.constraintlayout.widget.ConstraintLayout>

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="@+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_box" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Deepak P"
android:autoSizeMaxTextSize="30dp"

4|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/job_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/job_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Assistant Professor-CSE"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/phone_number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Enter your mobile number"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/address"

5|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/job_title" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Next to BEML, Koorgalli"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/email_etc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone_number" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/email_etc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="[email protected], www.nieit.ac.in, Fax"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"

6|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin File
package `in`.edu.cambridge.visitingcard
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Output:

7|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU
2. Develop an Android application using controls like Button, TextView, EditText for
designing a calculator having basic functionality like Addition, Subtraction,
Multiplication, and Division.

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

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/expression"
android:layout_width="400dp"
android:layout_height="100dp"
android:textSize="20sp"
android:gravity="center"/>

8|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU

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

<com.google.android.material.textview.MaterialTextView
android:id="@+id/result"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="24sp"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/clear"
android:layout_width="400dp"
android:layout_height="100dp"
android:gravity="center"
android:text="C"
android:textSize="24sp" />

</LinearLayout>

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

9|Page
NIE INSTITUTE OF TECHNOLOGY, MYSURU

<com.google.android.material.textview.MaterialTextView
android:id="@+id/one"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="1"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/two"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="2"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/three"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="3"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/division"
android:layout_width="100dp"

10 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_height="50dp"
android:gravity="center"
android:text="/"
android:textSize="24sp" />

</LinearLayout>

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

<com.google.android.material.textview.MaterialTextView
android:id="@+id/four"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="4"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/five"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="5"
android:textSize="24sp" />

11 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
<com.google.android.material.textview.MaterialTextView
android:id="@+id/six"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="6"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/multiplication"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="*"
android:textSize="24sp" />

</LinearLayout>

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

<com.google.android.material.textview.MaterialTextView
android:id="@+id/seven"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"

12 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:text="7"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/eight"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="8"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/nine"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="9"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/subtraction"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="-"
android:textSize="24sp" />

</LinearLayout>

13 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

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

<com.google.android.material.textview.MaterialTextView
android:id="@+id/dot"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="."
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/zero"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="0"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/equals"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="="

14 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/addition"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="+"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin File:
package `in`.edu.cambridge.calculator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import net.objecthunter.exp4j.ExpressionBuilder

class MainActivity : AppCompatActivity() {


private lateinit var zero: TextView
private lateinit var one: TextView
private lateinit var two: TextView
private lateinit var three: TextView
private lateinit var four: TextView

15 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
private lateinit var five: TextView
private lateinit var six: TextView
private lateinit var seven: TextView
private lateinit var eight: TextView
private lateinit var nine: TextView
private lateinit var addition: TextView
private lateinit var subtraction: TextView
private lateinit var multiplication: TextView
private lateinit var division: TextView
private lateinit var equals : TextView
private lateinit var clear: TextView
private lateinit var dot: TextView
private lateinit var result : TextView
private lateinit var expression : TextView

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

zero = findViewById(R.id.zero)
one = findViewById(R.id.one)
two = findViewById(R.id.two)
three = findViewById(R.id.three)
four = findViewById(R.id.four)
five = findViewById(R.id.five)
six = findViewById(R.id.six)
seven = findViewById(R.id.seven)
eight = findViewById(R.id.eight)

16 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
nine = findViewById(R.id.nine)
dot = findViewById(R.id.dot)
clear = findViewById(R.id.clear)
addition = findViewById(R.id.addition)
subtraction = findViewById(R.id.subtraction)
division= findViewById(R.id.division)
multiplication = findViewById(R.id.multiplication)
equals = findViewById(R.id.equals)
result = findViewById(R.id.result)
expression = findViewById(R.id.expression)

zero.setOnClickListener {
pressButton("0", true)
}
one.setOnClickListener {
pressButton("1", true)
}
two.setOnClickListener {
pressButton("2", true)
}
three.setOnClickListener {
pressButton("3", true)
}

four.setOnClickListener {
pressButton("4", true)
}

17 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
five.setOnClickListener {
pressButton("5", true)
}
six.setOnClickListener {
pressButton("6", true)
}
seven.setOnClickListener {
pressButton("7", true)
}
eight.setOnClickListener {
pressButton("8", true)
}
nine.setOnClickListener {
pressButton("9", true)
}

addition.setOnClickListener {
pressButton("+", true)
}

subtraction.setOnClickListener {
pressButton("-", true)
}
multiplication.setOnClickListener {
pressButton("*", true)
}
division.setOnClickListener {
pressButton("/", true)

18 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
}
dot.setOnClickListener {
pressButton(".", true)
}
clear.setOnClickListener {
result.text = ""
expression.text = ""
}
equals.setOnClickListener {
val text = expression.text.toString()
val expression = ExpressionBuilder(text).build()
val expResult = expression.evaluate()
val longResult = expResult.toLong()
if (expResult == longResult.toDouble()) {
result.text = longResult.toString()
} else {
result.text = result.toString()
}
}
}
fun pressButton(string: String, clear: Boolean) {
if(clear) {
result.text = ""
expression.append(string)
} else {
expression.append(result.text)
expression.append(string)
result.text = ""

19 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
}
}
}

Output:

20 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
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.

Activity_login.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=".LoginActivity">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/login_username_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"

21 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
app:layout_constraintBottom_toTopOf="@+id/login_password_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25"
app:layout_constraintVertical_chainStyle="packed">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/login_username"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Username" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/login_password_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:endIconCheckable="true"
app:endIconMode="password_toggle"
app:layout_constraintBottom_toTopOf="@+id/logInButton"
app:layout_constraintEnd_toEndOf="parent"

22 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/login_username_layout">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/logInButton"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Login Up"
app:layout_constraintBottom_toTopOf="@+id/login_to_signup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/login_password_layout" />

<com.google.android.material.textview.MaterialTextView

23 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:id="@+id/login_to_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Sign Up"
android:textColor="@color/purple_500"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logInButton" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity_Signup.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=".SignupActivity">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/signUp_username_layout"
24 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/signUp_password_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25"
app:layout_constraintVertical_chainStyle="packed">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/signUp_username"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Username" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/signUp_password_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="16dp"

25 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:endIconCheckable="true"
app:endIconMode="password_toggle"
app:layout_constraintBottom_toTopOf="@+id/signUpButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/signUp_username_layout">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/signUp_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/signUpButton"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Sign Up"

26 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
app:layout_constraintBottom_toTopOf="@+id/signUp_to_logIn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/signUp_password_layout" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/signUp_to_logIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Log In"
android:textColor="@color/purple_500"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/signUpButton" />

</androidx.constraintlayout.widget.ConstraintLayout>

Login_Activity.kt
package `in`.edu.cambridge.loginsignup

27 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
import `in`.edu.cambridge.loginsignup.data.AppDatabase
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.room.Room

class LoginActivity : AppCompatActivity() {

private lateinit var logInUsername: EditText


private lateinit var logInPassword: EditText
private lateinit var logInBtn: Button
private lateinit var logInToSignup: TextView

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
supportActionBar?.setTitle("Log In")

logInUsername = findViewById(R.id.login_username)
logInPassword = findViewById(R.id.login_password)
logInToSignup = findViewById(R.id.login_to_signup)
logInBtn = findViewById(R.id.logInButton)

28 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
val db = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "app-database"
).allowMainThreadQueries().build()

val userDao = db.userDao()

logInBtn.setOnClickListener {

val tempUser = userDao.getUser(logInUsername.text.toString())

if(tempUser.password.equals(logInPassword.text.toString())){
Toast.makeText(baseContext,"Login Sucessful",Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(baseContext,"Invalid Username Or
Password",Toast.LENGTH_SHORT).show()
}

logInToSignup.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
}
}

29 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
Signup_activity.kt
package `in`.edu.cambridge.loginsignup
import `in`.edu.cambridge.loginsignup.data.AppDatabase
import `in`.edu.cambridge.loginsignup.data.User
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.room.Room
import java.util.regex.Matcher
import java.util.regex.Pattern

class SignupActivity : AppCompatActivity() {

private lateinit var signUpUsername: EditText


private lateinit var signUpPassword: EditText
private lateinit var signUpBtn: Button
private lateinit var signUpToLogIn: TextView

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_signup)
supportActionBar?.setTitle("Sign Up")

30 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
signUpUsername = findViewById(R.id.signUp_username)
signUpPassword = findViewById(R.id.signUp_password)
signUpToLogIn = findViewById(R.id.signUp_to_logIn)
signUpBtn = findViewById(R.id.signUpButton)

val db = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "app-database"
).allowMainThreadQueries().build()

val userDao = db.userDao()

signUpBtn.setOnClickListener {

if(signUpPassword.text.toString().length<8 &&
!isValidPassword(signUpPassword.text.toString())){
Toast.makeText(baseContext,"Invalid Password",
Toast.LENGTH_SHORT).show()
}else{
val newUser = User(signUpUsername.text.toString(),
signUpPassword.text.toString())
userDao.insert(newUser)
Toast.makeText(baseContext,"Sign Up Sucessful",
Toast.LENGTH_SHORT).show()
}

signUpToLogIn.setOnClickListener{

31 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}
}

fun isValidPassword(password: String?): Boolean {


val pattern: Pattern
val matcher: Matcher
val PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[A-
Z])(?=.*[@#$%^&+=!])(?=\\S+$).{4,}$"
pattern = Pattern.compile(PASSWORD_PATTERN)
matcher = pattern.matcher(password)
return matcher.matches()
}

Database/Data Files:
AppDatabase.kt
package `in`.edu.cambridge.loginsignup.data

import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [User::class], version = 1)


abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}

32 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
User.kt
package `in`.edu.cambridge.loginsignup.data

import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class User(
@PrimaryKey val username: String,
val password: String?
)
UserDao.kt
package `in`.edu.cambridge.loginsignup.data
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query

@Dao
interface UserDao {
@Query("SELECT * FROM user WHERE username == :username")
fun getUser(username: String): User

@Insert
fun insert(user: User)

33 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
Output:

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.

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">
34 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

<com.google.android.material.button.MaterialButton
android:id="@+id/change_wallpaper"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:text="Change Wallpaper"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt
//@file:Suppress("DEPRECATION")

package `in`.edu.cambridge.wallpaper
import android.app.WallpaperManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

35 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

class MainActivity : AppCompatActivity() {

var myWallpaperList =
arrayOf(R.drawable.wallpaper1,R.drawable.wallpaper2,R.drawable.wallpaper3,R.drawable.wall
paper4,R.drawable.wallpaper5)

private lateinit var changeWallpaper:Button

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

changeWallpaper = findViewById(R.id.change_wallpaper)
changeWallpaper.setOnClickListener { setWallpaper() }
}

//@Suppress("DEPRECATION")
fun setWallpaper() {
Toast.makeText(this, "Setting Wallpaper please wait.",
Toast.LENGTH_SHORT).show()
//@Suppress("DEPRECATION", "TrailingComma")
@Suppress("DEPRECATION")
Handler().postDelayed({
for(i in myWallpaperList) {
val bitmap: Bitmap = BitmapFactory.decodeResource(resources, i)
val wallpaperManager = WallpaperManager.getInstance(baseContext)
wallpaperManager.setBitmap(bitmap)

36 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

}
}, 2000)
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.edu.cambridge.wallpaper">

<uses-permission android:name="android.permission.SET_WALLPAPER" />


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Wallpaper">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

37 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
Output:

38 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
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.

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

<com.google.android.material.textview.MaterialTextView
android:id="@+id/counter_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Counter Value"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/start_counter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

39 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
app:layout_constraintVertical_bias="0.25"
app:layout_constraintVertical_chainStyle="packed" />

<com.google.android.material.button.MaterialButton
android:id="@+id/start_counter"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Start"
app:layout_constraintBottom_toTopOf="@+id/stop_counter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/counter_value" />

<com.google.android.material.button.MaterialButton
android:id="@+id/stop_counter"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"

40 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
app:layout_constraintTop_toBottomOf="@+id/start_counter" />

<com.google.android.material.button.MaterialButton
android:id="@+id/reset_counter"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Reset"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/start_counter" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt
package `in`.edu.cambridge.counter

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.os.CountDownTimer
//import android.view.View

class MainActivity : AppCompatActivity() {


private lateinit var counterValue:TextView

41 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
private lateinit var startCounter : Button
private lateinit var stopCounter : Button
private lateinit var resetCounter : Button

val timer = MyCounter(10000000, 10)


var countervalue : Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

counterValue = findViewById(R.id.counter_value)
startCounter = findViewById(R.id.start_counter)

startCounter.setOnClickListener {
timer.start()
startCounter.isEnabled=false
}
stopCounter = findViewById(R.id.stop_counter)
stopCounter.setOnClickListener {
timer.cancel()
startCounter.isEnabled=true
}
resetCounter = findViewById(R.id.reset_counter)
resetCounter.setOnClickListener {

countervalue = 0
counterValue.text = (countervalue).toString()

42 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
}
/*fun reset(view : View){
val count = 0
var text = count.toString()
}*/

inner class MyCounter(millisInFuture: Long, countDownInterval: Long) :


CountDownTimer(millisInFuture, countDownInterval) {
override fun onFinish() {
}

override fun onTick(millisUntilFinished: Long) {


countervalue++
counterValue.text = (countervalue).toString()
}
}
}
Output:

43 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
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.

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

<com.google.android.material.button.MaterialButton
android:id="@+id/parse_xml"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_marginBottom="8dp"
android:text="Parse XML Data"
app:layout_constraintBottom_toTopOf="@+id/parse_json"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.24000001"
app:layout_constraintVertical_chainStyle="packed" />

44 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
<com.google.android.material.button.MaterialButton
android:id="@+id/parse_json"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_marginBottom="8dp"
android:text="Parse JSON Data"
app:layout_constraintBottom_toTopOf="@+id/data_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/parse_xml" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/data_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="16dp"
android:text=""
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/city_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/parse_json" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/city_name"

45 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="City Name :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/latitude"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/data_type" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Latitude :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/longitude"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/city_name" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/longitude"

46 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Longitude :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/temprature"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/latitude" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Temperature :"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/humidity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/longitude" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/humidity"

47 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Humidity :"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/temperature" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt
package `in`.edu.cambridge.parsingxmlandjson

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import org.json.JSONObject
import java.io.IOException
import java.nio.charset.Charset
import javax.xml.parsers.DocumentBuilderFactory
import android.annotation.SuppressLint as SuppressLint1

48 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
class MainActivity : AppCompatActivity() {
private lateinit var parseXMLBtn: Button
private lateinit var parseJSONBtn: Button

private lateinit var datatype : TextView


private lateinit var cityName: TextView
private lateinit var latitude: TextView
private lateinit var longitude: TextView
private lateinit var temperature: TextView
private lateinit var humidity: TextView

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

parseXMLBtn = findViewById(R.id.parse_xml)
parseXMLBtn.setOnClickListener { parseXML() }
parseJSONBtn = findViewById(R.id.parse_json)
parseJSONBtn.setOnClickListener { parseJSON() }
parseXMLBtn.setOnClickListener { parseXML() }

datatype = findViewById(R.id.data_type)
cityName = findViewById(R.id.city_name)
latitude = findViewById(R.id.latitude)
longitude = findViewById(R.id.longitude)
temperature = findViewById(R.id.temperature)
humidity = findViewById(R.id.humidity)

49 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

@SuppressLint1("SetTextI18n")
fun parseXML(){
datatype.text = "XML Data"
try {
val iStream = assets.open("myxml.xml")
val builderFactory = DocumentBuilderFactory.newInstance()
var docBuilder = builderFactory.newDocumentBuilder()
var doc = docBuilder.parse(iStream)
cityName.text = "City Name : " +
doc.getElementsByTagName("City_Name").item(0).getFirstChild().getNodeValue()
latitude.text = "Latitude : " +
doc.getElementsByTagName("Latitude").item(0).getFirstChild().getNodeValue()
longitude.text = "Longitude : " +
doc.getElementsByTagName("Longitude").item(0).getFirstChild().getNodeValue()
temperature.text = "Temperature : " +
doc.getElementsByTagName("Temperature").item(0).getFirstChild().getNodeValue()
humidity.text = "Humidity : " +
doc.getElementsByTagName("Humidity").item(0).getFirstChild().getNodeValue()
}
catch (ex: IOException) {

}
}

@SuppressLint1("SetTextI18n")
fun parseJSON(){
datatype.text = "JSON Data"

50 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
val obj = JSONObject(loadJSONFromAsset())
cityName.text = "City Name : " + obj.getString("City Name")
latitude.text = "Latitude : " + obj.getString("Latitude")
longitude.text = "Longitude : " + obj.getString("Longitude")
temperature.text = "Temperature : " + obj.getString("Temperature")
humidity.text = "Humidity : " + obj.getString("Humidity")
}

private fun loadJSONFromAsset(): String {


val json: String?
try {
val inputStream = assets.open("myjson.json")
val size = inputStream.available()
val buffer = ByteArray(size)
val charset: Charset = Charsets.UTF_8
inputStream.read(buffer)
inputStream.close()
json = String(buffer, charset)
}
catch (ex: IOException) {
ex.printStackTrace()
return ""
}
return json
}
}

51 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
Assets File: Myjson.json Myxml.xml
{ <data>
"City Name": "Mysore", <City_Name>Mysore</City_Name>
"Latitude" : "12.295", <Latitude>22.295</Latitude>
"Longitude" : "76.639", <Longitude>76.639</Longitude>
"Temperature" : "22", <Temperature>42</Temperature>
"Humidity" : "90%" <Humidity>80%</Humidity>
} </data>

Output:

52 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
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.

Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edTv"
android:hint="Enter Text"
android:textSize="19sp"
android:textStyle="bold"
android:layout_margin="15dp"
android:padding="15dp"

53 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:textColor="@color/purple_500"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="Pitch"
android:textColor="@color/purple_500"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/seekBar_pitch"
android:progress="50"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<TextView
android:text="Speed"
android:textColor="@color/purple_500"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/seekBar_speed"
android:progress="50"
android:layout_width="200dp"

54 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnSpeak"
android:text="Convert text to speech!"
android:textSize="19sp"
android:textStyle="bold"
android:textAllCaps="false"
android:layout_gravity="center"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
</ScrollView>
</LinearLayout>

MainActivity.kt

package `in`.edu.cambridge.texttospeech

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.SeekBar
import java.util.*

55 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

class MainActivity : AppCompatActivity() {


private lateinit var speakBtn:Button
private lateinit var userText:EditText
private lateinit var seekBerPitch:SeekBar
private lateinit var seekBerSpeed:SeekBar
private lateinit var mTTs :TextToSpeech
//private lateinit var btnClear :Button

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

/**set find view Id*/


speakBtn = findViewById(R.id.btnSpeak)
userText = findViewById(R.id.edTv)
seekBerPitch = findViewById(R.id.seekBar_pitch)
seekBerSpeed = findViewById(R.id.seekBar_speed)

mTTs = TextToSpeech(this){status->
if (status == TextToSpeech.SUCCESS){
val result = mTTs.setLanguage(Locale.ENGLISH)
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTs","Language is not Supported")

56 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
}else{
speakBtn.isEnabled = true
}
}
else{
Log.e("TTs","Initialization failed")
}
}
speakBtn.setOnClickListener { speak() }
}

@Suppress("DEPRECATION")
private fun speak() {
val text = userText.text.toString()
var pitch = seekBerPitch.progress.toFloat()/50
if (pitch<0.1) pitch = 0.1f
var speed = seekBerSpeed.progress.toFloat()/50
if (speed<0.1) speed = 0.1f
mTTs.setPitch(pitch)
mTTs.setSpeechRate(speed)
mTTs.speak(text,TextToSpeech.QUEUE_FLUSH,null)

override fun onDestroy() {


if (mTTs != null){

57 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
mTTs.stop()
mTTs.shutdown()
}
super.onDestroy()
}

Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.edu.cambridge.texttospeech">

<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TextToSpeech">
<activity android:name=".MainActivity">
<intent-filter>

58 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Output:

59 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

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

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

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

60 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

<com.google.android.material.textview.MaterialTextView
android:id="@+id/contact"
android:layout_width="200dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="24sp"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/clear"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="X"
android:textSize="24sp" />

</LinearLayout>

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

<com.google.android.material.textview.MaterialTextView
android:id="@+id/one"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"

61 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:text="1"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/two"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="2"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/three"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="3"
android:textSize="24sp" />

</LinearLayout>

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

<com.google.android.material.textview.MaterialTextView
android:id="@+id/four"

62 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="4"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/five"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="5"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/six"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="6"
android:textSize="24sp" />

</LinearLayout>

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

63 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

<com.google.android.material.textview.MaterialTextView
android:id="@+id/seven"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="7"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/eight"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="8"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/nine"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="9"
android:textSize="24sp" />

</LinearLayout>

<LinearLayout

64 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/star"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="*"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/zero"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="0"
android:textSize="24sp" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/hash"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="#"
android:textSize="24sp" />

65 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
</LinearLayout>

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

<com.google.android.material.button.MaterialButton
android:id="@+id/call"
android:layout_width="134dp"
android:layout_height="54dp"
android:layout_margin="8dp"
android:text="Call"/>

<com.google.android.material.button.MaterialButton
android:id="@+id/save"
android:layout_width="134dp"
android:layout_height="54dp"
android:layout_margin="8dp"
android:text="Save"/>

</LinearLayout>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

66 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
MainActivity.kt
package `in`.edu.cambridge.phonedialer
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


private lateinit var saveBtn: Button
private lateinit var callBtn: Button

private lateinit var zero: TextView


private lateinit var one: TextView
private lateinit var two: TextView
private lateinit var three: TextView
private lateinit var four: TextView
private lateinit var five: TextView
private lateinit var six: TextView
private lateinit var seven: TextView
private lateinit var eight: TextView
private lateinit var nine: TextView
private lateinit var star: TextView
private lateinit var hash: TextView
private lateinit var clear: TextView

67 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

private lateinit var contact: TextView

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

saveBtn = findViewById(R.id.save)
callBtn = findViewById(R.id.call)

zero = findViewById(R.id.zero)
one = findViewById(R.id.one)
two = findViewById(R.id.two)
three = findViewById(R.id.three)
four = findViewById(R.id.four)
five = findViewById(R.id.five)
six = findViewById(R.id.six)
seven = findViewById(R.id.seven)
eight = findViewById(R.id.eight)
nine = findViewById(R.id.nine)
star = findViewById(R.id.star)
hash = findViewById(R.id.hash)
clear = findViewById(R.id.clear)

contact = findViewById(R.id.contact)

zero.setOnClickListener {
pressButton("0", true)

68 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
}

one.setOnClickListener {
pressButton("1", true)
}

two.setOnClickListener {
pressButton("2", true)
}

three.setOnClickListener {
pressButton("3", true)
}

four.setOnClickListener {
pressButton("4", true)
}

five.setOnClickListener {
pressButton("5", true)
}

six.setOnClickListener {
pressButton("6", true)
}

seven.setOnClickListener {
pressButton("7", true)

69 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
}

eight.setOnClickListener {
pressButton("8", true)
}

nine.setOnClickListener {
pressButton("9", true)
}

star.setOnClickListener {
pressButton("*", true)
}

hash.setOnClickListener {
pressButton("#", true)
}

clear.setOnClickListener {
contact.text = ""
}

callBtn.setOnClickListener {
val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "${contact.text}"))
startActivity(intent)
}

70 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU
saveBtn.setOnClickListener {
val intent = Intent(
ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
Uri.parse("tel:" + contact.text))
intent.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true)
startActivity(intent)
}

fun pressButton(string: String, clear: Boolean) {


if (!clear) {
contact.text = ""
} else {
contact.append(string)
}
}

Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.edu.cambridge.phonedialer">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

71 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PhoneDialer">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Output:

72 | P a g e
NIE INSTITUTE OF TECHNOLOGY, MYSURU

“In order to be irreplaceable, one must


always be different”

73 | P a g e

You might also like