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

1. Demonstrate a Simple Application using Start and Stop Button.

<?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:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>

<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="START"
android:textSize="48sp"
app:layout_constraintBottom_toTopOf="@+id/end"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="272dp"
android:text="STOP"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/start" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt

package com.example.startstopmessageapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar

class MainActivity : AppCompatActivity() {

private lateinit var startBtn : Button


private lateinit var stopBtn : Button

override fun onCreate(savedInstanceState: Bundle?) {


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

startBtn = findViewById(R.id.start)
stopBtn = findViewById(R.id.end)

startBtn.setOnClickListener {
val contextView = findViewById<View>(R.id.root)
val snack= Snackbar.make(contextView,"start button
pressed",Snackbar.LENGTH_SHORT)
snack.show()
//Toast.makeText(applicationContext,"start button
pressed",Toast.LENGTH_LONG).show()

stopBtn.setOnClickListener {
val contextView = findViewById<View>(R.id.root)
val snack= Snackbar.make(contextView,"stop button
pressed",Snackbar.LENGTH_SHORT)
snack.show()
//Toast.makeText(applicationContext,"stop button
pressed",Toast.LENGTH_LONG).show()

}
}
}
Design output-1 output-2

2. Implement a Calculator application, showing all arithmetic operations.

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">
<TextView
android:id="@+id/expression"
android:layout_width="400dp"
android:layout_height="100dp"
android:textSize="24sp"
android:gravity="center"/>

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

<TextView
android:id="@+id/result"
android:layout_width="300dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="24sp"/>

<TextView
android:id="@+id/clear"
android:layout_width="100dp"
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">

<TextView
android:id="@+id/one"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="1"
android:textSize="24sp" />

<TextView
android:id="@+id/two"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="2"
android:textSize="24sp" />

<TextView
android:id="@+id/three"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="3"
android:textSize="24sp" />

<TextView
android:id="@+id/division"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="/"
android:textSize="24sp" />

</LinearLayout>

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

<TextView
android:id="@+id/four"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="4"
android:textSize="24sp" />

<TextView
android:id="@+id/five"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="5"
android:textSize="24sp" />

<TextView
android:id="@+id/six"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="6"
android:textSize="24sp" />

<TextView
android:id="@+id/multiplication"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="*"
android:textSize="24sp" />

</LinearLayout>

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

<TextView
android:id="@+id/seven"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="7"
android:textSize="24sp" />

<TextView
android:id="@+id/eight"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="8"
android:textSize="24sp" />

<TextView
android:id="@+id/nine"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="9"
android:textSize="24sp" />

<TextView
android:id="@+id/subtraction"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="-"
android:textSize="24sp" />

</LinearLayout>

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

<TextView
android:id="@+id/dot"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="."
android:textSize="24sp" />

<TextView
android:id="@+id/zero"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="0"
android:textSize="24sp" />

<TextView
android:id="@+id/equals"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="="
android:textSize="24sp" />

<TextView
android:id="@+id/addition"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="+"
android:textSize="24sp" />

</LinearLayout>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.calendarapp

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
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 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)
nine = findViewById(R.id.nine)

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)
}

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)
}

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 = ""
}
}
}
In gradleScripts →gradle.build(Module:)→under dependencies→ add the following
dependency

implementation 'net.objecthunter:exp4j:0.4.8'

Design output-1 output-2


3. Develop an application for calculating Car EMI.

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.textview.MaterialTextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="54sp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="32dp"
android:gravity="center"
android:hint="Result"
android:textSize="24dp"
app:layout_constraintBottom_toTopOf="@+id/principalAmountInputLayout"
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.TextInputLayout
android:id="@+id/principalAmountInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/downPaymentInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resultTextView">

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

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/downPaymentInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/interestRateInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/principalAmountInputLayout">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/downPayment"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:hint="Down Payment" />


</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/interestRateInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/loanTermInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/downPaymentInputLayout">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/interestRate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Interest Rate" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/loanTermInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/calculateEMIButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/interestRateInputLayout">

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

<com.google.android.material.button.MaterialButton
android:id="@+id/calculateEMIButton"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Calculate Monthly EMI"
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/loanTermInputLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.example.caremiapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import java.lang.Math.pow
class MainActivity : AppCompatActivity() {

private lateinit var resultText: TextView


private lateinit var principalAmount : EditText
private lateinit var downPayment : EditText
private lateinit var interestRate : EditText
private lateinit var loanTerm : EditText

private lateinit var calculateBtn : Button

override fun onCreate(savedInstanceState: Bundle?) {


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

resultText = findViewById(R.id.resultTextView)
principalAmount = findViewById(R.id.principalAmount)
downPayment = findViewById(R.id.downPayment)
interestRate = findViewById(R.id.interestRate)
loanTerm = findViewById(R.id.loanTerm)
calculateBtn = findViewById(R.id.calculateEMIButton)

calculateBtn.setOnClickListener {
var emi : Int
var p : Int
var principal = principalAmount.text.toString().toInt()
var r = interestRate.text.toString().toInt()
var n = loanTerm.text.toString().toInt()
var downPaymentAmt =downPayment.text.toString().toInt()
p = if ((downPaymentAmt != 0)){
principal - downPaymentAmt
} else{
principal
}
emi = (p*(r*pow(((1+r).toDouble()), n.toDouble())) /(pow(((1+r).toDouble()),
n.toDouble())-1)).toInt()
resultText.text = emi.toString()
}
}
}
Design output-1 output-2
4. Develop an application showing Clipboard by performing copy and paste
operations.

Activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/root"
tools:context=".MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Clipboard Activity"
android:textSize="24sp"

/>

<EditText

android:id="@+id/copy_this_text"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="start"
android:hint="Enter Some Text and Copy" />

<TextView
android:id="@+id/paste_here"
android:layout_width="match_parent"
android:layout_height="150dp"

/>

<Button
android:id="@+id/copy_text"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_margin="8dp"
android:text="Copy Text" />

<Button
android:id="@+id/paste_text"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:gravity="center"
android:text="Paste Text" />

</LinearLayout>

MainActivity.kt

package com.example.copypasteapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.google.android.material.snackbar.Snackbar

class MainActivity : AppCompatActivity() {

private lateinit var copyText: EditText


private lateinit var pasteText: TextView
private lateinit var copyBtn: Button
private lateinit var pasteBtn: Button

override fun onCreate(savedInstanceState: Bundle?) {


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

copyText = findViewById(R.id.copy_this_text)
copyBtn = findViewById(R.id.copy_text)
copyBtn.setOnClickListener {
copyText()
}

pasteText = findViewById(R.id.paste_here)
pasteBtn = findViewById(R.id.paste_text)
pasteBtn.setOnClickListener {
pasteText()
}

fun copyText(){
val contextView = findViewById<View>(R.id.root)
val snack = Snackbar.make(contextView, "Text Copied",
Snackbar.LENGTH_INDEFINITE).show()

fun pasteText(){
pasteText.text = copyText.text.toString()
val contextView = findViewById<View>(R.id.root)
val snack = Snackbar.make(contextView, "Text Pasted",
Snackbar.LENGTH_INDEFINITE).show()
}
}

design output-1 output-2


5. Create a Sample Application with Login Module to understand Activity and Intent.

Activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Application"
android:textAlignment="center"
android:textSize="40sp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/username"
android:hint="Enter User Name"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:inputType="numberPassword"
android:hint="Enter Password"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/resetBtn"
android:text="RESET"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/loginBtn"
android:text="LOGIN"/>

</LinearLayout>
MainActivity.kt

package com.example.loginapp

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

class MainActivity : AppCompatActivity() {


private lateinit var userET: EditText
private lateinit var passET: EditText
private lateinit var resetBtn: Button
private lateinit var loginBtn: Button

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
userET = findViewById(R.id.username)
passET = findViewById(R.id.password)

resetBtn = findViewById(R.id.resetBtn)
resetBtn.setOnClickListener {
userET.setText("")
passET.setText("")
}

loginBtn = findViewById(R.id.loginBtn)
loginBtn.setOnClickListener {
val user = userET.text.toString()
val pass = passET.text.toString()

if (user.equals("cvr") && pass.equals("1234"))


{
val intent = Intent(this,HomeActivity1::class.java)
intent.putExtra("Username",user)
intent.putExtra("Password",pass)
startActivity(intent)
Toast.makeText(this,"Login is Successful", Toast.LENGTH_LONG).show()
}
else
{
Toast.makeText(this,"Login is Not Successful", Toast.LENGTH_LONG).show()
}
}
}
}

Activity_home.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/resTV"
android:text="Welcome to :"
android:textSize="40sp"/>

</LinearLayout>

HomeActivity.kt

package com.example.loginapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class HomeActivity1 : AppCompatActivity() {

private lateinit var resultV: TextView

override fun onCreate(savedInstanceState: Bundle?) {


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

resultV = findViewById(R.id.resTV)

val intent: Intent = intent

val user = intent.getStringExtra("Username")


val pass = intent.getStringExtra("Password")

// val str = resultV.text.toString()


resultV.setText(user+" "+pass)

}
}

Design output-1 output-2


6. Create a Dice Roller application that has a button to roll a dice and update the
image on thescreen.
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">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/roll"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />

<ImageView
android:id="@+id/imageView"
android:layout_width="160dp"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@drawable/dice_1" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt
package com.example.diceroller

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {


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

val rollButton: Button = findViewById(R.id.button)

rollButton.setOnClickListener {
//val toast = Toast.makeText(this, "Dice Rolled!", Toast.LENGTH_SHORT).show()
//val resultTextView: TextView = findViewById(R.id.textView)
//resultTextView.text = "6"
rollDice()
}
}

private fun rollDice() {

val dice = Dice(6)


val diceRoll = dice.roll()
val diceImage: ImageView = findViewById(R.id.imageView)
val drawableResource = when (diceRoll) {
1 -> R.drawable.dice_1
2 -> R.drawable.dice_2
3 -> R.drawable.dice_3
4 -> R.drawable.dice_4
5 -> R.drawable.dice_5
else -> R.drawable.dice_6
}

diceImage.setImageResource(drawableResource)
diceImage.contentDescription = diceRoll.toString()
}
}
class Dice(private val numSides: Int) {

fun roll(): Int {


return (1..numSides).random()
}
}
Design output-1 output-2

7. Application for setting and changing the Wallpaper

Activity_xml

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<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
package com.example.wallpaperchangeapp

import android.app.WallpaperManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.os.Handler
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {


var myWallpaperList =
arrayOf(
R.drawable.wallpaper1,
R.drawable.wallpaper2,
R.drawable.wallpaper3,
R.drawable.wallpaper4,
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()
}
}

fun setWallpaper() {
Toast.makeText(this, "Setting Wallpaper please wait.", Toast.LENGTH_SHORT)
.show()
Handler (Looper.getMainLooper()).postDelayed({
for (i in myWallpaperList) {
val bitmap: Bitmap = BitmapFactory.decodeResource(resources, i)
val wallpaperManager = WallpaperManager.getInstance(applicationContext)
wallpaperManager.setBitmap(bitmap)

}
}, 2000)
}
}

In Android.ManifestFile→ specify the user permission before <application> as →


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

Design output-1 output-2


8. Develop an application for Phone Dialer.

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

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

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

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

MainActivity.kt
package com.example.phonedialerapp

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.ContactsContract
import android.widget.Button
import android.widget.TextView

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

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)
}

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)
}

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)
}

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)
}
}

In Android.ManifestFile→ specify the user permission before <application> as →


<uses-permission android:name="android.permission.CALL_PHONE"/>
Design output-1

Output-2 output-3
9. Develop an Tip Calculator App with a working Calculate button

Activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/cost_of_service"
android:layout_height="wrap_content"
android:layout_width="160dp"
android:ems="10"
android:inputType="numberDecimal"
android:text=""
android:hint="@string/cost_of_service"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/service_question"
android:text="@string/how_was_the_service"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cost_of_service"/>

<RadioGroup
android:id="@+id/tip_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:checkedButton="@+id/options_twenty_percent"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cost_of_service">

<RadioButton
android:id="@+id/options_twenty_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/amazing_20" />
<RadioButton
android:id="@+id/options_eighteen_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/good_18" />

<RadioButton
android:id="@+id/options_fifteen_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/okay_15" />

</RadioGroup>

<Switch
android:id="@+id/round_up_switch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/round_up_tip"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tip_options" />

<Button
android:id="@+id/calculate_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/calculate"
app:layout_constraintTop_toBottomOf="@id/round_up_switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/tip_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/calculate_button"
tools:text="Tip Amount: $10" />

</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivity.kt

package com.example.tipcalculator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.tipcalculator.databinding.ActivityMainBinding
import java.text.NumberFormat

class MainActivity : AppCompatActivity() {

lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.calculateButton.setOnClickListener{ calculateTip() }
}
fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDouble()
val selectedId = binding.tipOptions.checkedRadioButtonId
val tipPercentage = when (selectedId) {
R.id.options_twenty_percent -> 0.20
R.id.options_eighteen_percent -> 0.18
else -> 0.15
}

var tip = tipPercentage * cost


val roundUp = binding.roundUpSwitch.isChecked
if (roundUp) {
tip = kotlin.math.ceil(tip)
}
val formattedTip = NumberFormat.getCurrencyInstance().format(tip)
binding.tipResult.text = getString(R.string.tip_amount, formattedTip)
}
}
buil.gradle:module file
buildFeatures {
viewBinding = true
}

Design output-1
10. Develop an application for a File System showing Read and Write operations

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.textfield.TextInputLayout
android:id="@+id/text_input_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"

android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/save_file"
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/text_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:hint="Enter some text"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/save_file"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Save File"
app:layout_constraintBottom_toTopOf="@+id/open_file_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/text_input_layout" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/open_file_text"
style="@style/Widget.MaterialComponents.TextView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/purple_500"
app:layout_constraintBottom_toTopOf="@+id/open_file"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/save_file" />

<com.google.android.material.button.MaterialButton
android:id="@+id/open_file"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Open File"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/open_file_text" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.readwritefileapp

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import java.io.*

class MainActivity : AppCompatActivity() {


private lateinit var text_input: EditText
private lateinit var saveBtn : Button
private lateinit var openBtn : Button
private lateinit var open_text : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
text_input = findViewById(R.id.text_input)
open_text = findViewById(R.id.open_file_text)

openBtn = findViewById(R.id.open_file)
saveBtn = findViewById(R.id.save_file)

saveBtn.setOnClickListener {
var data : String = text_input.text.toString()

writeToFile(data, this)
Toast.makeText(this, "File Saved !", Toast.LENGTH_SHORT).show()
}

openBtn.setOnClickListener {
var data = readFromFile(this)
open_text.text = data
}

private fun writeToFile(data: String, context: Context) {


try {
val outputStreamWriter =
OutputStreamWriter(context.openFileOutput("file.txt", MODE_PRIVATE))
outputStreamWriter.write(data)
outputStreamWriter.close()
} catch (e: IOException) {
Log.e("Exception", "File write failed: " + e.toString())
}
}

private fun readFromFile(context: Context): String? {


var str = ""
try {
val inputStream: InputStream = context.openFileInput("file.txt")
if (inputStream != null) {
val inputStreamReader = InputStreamReader(inputStream)
val bufferedReader = BufferedReader(inputStreamReader)
var receiveString: String? = ""
val stringBuilder = StringBuilder()
while (bufferedReader.readLine().also { receiveString = it } != null) {

stringBuilder.append("\n").append(receiveString)
}
inputStream.close()
str = stringBuilder.toString()
}
} catch (e: FileNotFoundException) {
Log.e("Main Activity", "File not found: " + e.toString())
} catch (e: IOException) {
Log.e("Main Activity", "Can not read file: $e")
}
return str
}

}
11. Create a Kotlin application for Rolling a Die using classes.
Write this program from your observation.
12. Create a program with different types of Dwellings(Shelters people live in like -
ROUNT HUT,SQUARE CABIN,ROUND TOWER) that are implemented as a
class heirarchy.
import kotlin.math.PI
import kotlin.math.sqrt

fun main() {
val squareCabin = SquareCabin(6, 50.0)
val roundHut = RoundHut(3, 10.0)
val roundTower = RoundTower(4, 15.5)

with(squareCabin) {
println("\nSquare Cabin\n============")
println("Capacity: ${capacity}")
println("Material: ${buildingMaterial}")
println("Floor area: ${floorArea()}")
}
with(roundHut) {
println("\nRound Hut\n=========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("Floor area: ${floorArea()}")
println("Has room? ${hasRoom()}")
getRoom()
println("Has room? ${hasRoom()}")
getRoom()

}
with(roundTower) {
println("\nRound Tower\n==========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("Floor area: ${floorArea()}")

}
}
abstract class Dwelling(private var residents: Int) {
abstract val buildingMaterial: String
abstract val capacity: Int
abstract fun floorArea(): Double
fun hasRoom(): Boolean {
return residents < capacity
}

fun getRoom() {
if (capacity > residents) {
residents++
println("You got a room!")
} else {
println("Sorry, at capacity and no rooms left.")
}
}

}
class SquareCabin(residents: Int, val length: Double) : Dwelling(residents) {
override val buildingMaterial = "Wood"
override val capacity = 6
override fun floorArea(): Double {
return length * length
}
}
open class RoundHut(val residents: Int, val radius: Double) : Dwelling(residents) {

override val buildingMaterial = "Straw"


override val capacity = 4
override fun floorArea(): Double {
return PI * radius * radius
}
}
class RoundTower(residents: Int, radius: Double,val floors: Int = 2) : RoundHut(residents,
radius) {
override val buildingMaterial = "Stone"
override val capacity = floors * 4
override fun floorArea(): Double {
return super.floorArea() * floors
}
}

13. Create a Kotlin application to demonstrate the companion object, getter and setter
properties.
14. Write a program to demonstrate Activity Life cycle.

You might also like