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

Lab-8 Tip Calculator

Uploaded by

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

Lab-8 Tip Calculator

Uploaded by

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

8.

Develop 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"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="186dp"
android:layout_height="45dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.816" />

<EditText
android:id="@+id/cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Cost"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.417"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023"
tools:ignore="TouchTargetSizeCheck" />

<RadioGroup
android:id="@+id/RG"
android:layout_width="200dp"
android:layout_height="144dp"
app:layout_constraintBottom_toTopOf="@+id/switch1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.44"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cost"
app:layout_constraintVertical_bias="0.385">

<RadioButton
android:id="@+id/radio15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Awesome (20)" />

<RadioButton
android:id="@+id/radio18"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Good (18)" />

<RadioButton
android:id="@+id/radio20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Average (15)" />
</RadioGroup>

//for round tip enable/disable

<Switch
android:id="@+id/switch1"
android:layout_width="409dp"
android:layout_height="56dp"
android:layout_marginTop="240dp"
android:text="Round Up Tip"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cost"
app:layout_constraintVertical_bias="0.0"
tools:ignore="HardcodedText,UseSwitchCompatOrMaterialXml" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Tip"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch1" />

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

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.calculatetip.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {


lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.button.setOnClickListener() {
calculateTip()
}
}
fun calculateTip()
{
val cost=(binding.cost.text.toString()).toDouble()
val selected=binding.RG.checkedRadioButtonId
val tipPercent=when(selected)
{
R.id.radio15 -> 0.15
R.id.radio18 -> 0.18
else -> 0.20
}
var tip=tipPercent*cost
if(binding.switch1.isChecked)
{
tip=kotlin.math.ceil(tip)
}
binding.textView.text=tip.toString()
}
}

build.gradle:

In gradle scripts -> select build.gradle(Module:app) file -> update the higlighted
build features lines of code.

Finally Sync Now .

Then Run the emulator

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.example.calculatetip'
compileSdk 33

defaultConfig {
applicationId "com.example.calculatetip"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding="true"
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

OUTPUT:

You might also like