AAD Week2 LDQ
AAD Week2 LDQ
Creation (onCreate()):
When the user initiates the student registration process, the corresponding
activity is created.
In this phase, the activity is created and prepared for interaction with the
user.
Starting (onStart()):
In the onStart() method, the activity becomes visible to the user, but it may
not be fully interactive yet.
At this stage, the activity is visible to the user, but it might not have
received focus or input focus.
Resuming (onResume()):
In the onResume() method, the activity is ready to receive user input and
respond to user interactions.
This phase occurs when the activity is in the foreground and actively
interacting with the user, such as when the registration form is displayed,
and the user can enter their information.
Pausing (onPause()):
If another activity comes into the foreground, partially covering the current
activity, the current activity enters the paused state.
This phase might occur when the user switches to another app or when a
dialog or notification appears on top of the activity.
Stopping (onStop()):
When the activity is no longer visible to the user, it enters the stopped
state.
In the onStop() method, the activity is no longer visible to the user, but its
state and instance variables are retained.
This phase might occur when the user navigates to another activity or
presses the device's Home button.
Destroying (onDestroy()):
This phase occurs when the activity is explicitly finished by the user or the
system, or when the system needs to reclaim memory.
Step-1 : We are going to need a SplashActivity, RegisteredList page for showing registered students and RegisterActivity
page for registering the course.
Step-2 : Once class files are created, we then create their respective xml files for UI design activity_main.xml, activity_reg-
istration_list.xml and activity_register.xml
Step-3 : activity_registration_list.xml will contain a textView for titlebar, a recyclerview for showing the list and a button
which will navigate to the RegisterActivity page.
Step-4 : In activity_register.xml, there will be only a TextView (Name) and an AutoCompleteTextView field (Course).
Step-5 : In activity_main.xml, only one TextView will be placed in center which shows the App Name since its a splash
page.
Step-6 : 3000 millisecond delay will be there in SplashActivity once its over it will be navigated to RegisteredList automat-
ically.
Step-7 : In RegisteredList, implement the click listener for register button and navigate it to RegisterActivity using Intent
class and StartActivity function.
Step-8 : Create a custom adapter for RecyclerView called RecyclerAdapter and inflate the activity_list_row (custom layout)
which we created for the item in the list. The item row layout contains 3 TextViews and a button (Id, Name, Course and a
delete button). Set the custom adapter to Recyclerview in RegisteredList class.
Step-9 : Created a Data model bean class called ListItemModel which contains name, course and id.
Step-10 : Create a class called DBHandler which extends SQLLiteOpenHelper class and override the methods to perform
CRUD operations in local DB.
Step-11 : Create a method called register having name and course as arguments for inserting values in db and returns bool-
ean flag based on the status of the operation.
Step-12 : Create a method called getData to fetch records from db which returns a cursor object and create a deleteRecord
method having id as argument for deleting a specific record.
Step-13 : Create a dbHandler class object in RegisteredList class. Using that object we call the getData method and popu-
late the data in recyclerview. Call the deleteRecord method to delete that specific record based on click of delete button in
recycler adapter class.
Step-14 : Create a dbHandler class object in RegisterActivity class. Using that object we call the register method on click of
submit button. Get the name from the edit text and create an array with static data to populate in AutoCompleteTextView
dropdown.
SplashActivity.kt
package com.example.courseregapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Handler(Looper.getMainLooper()).postDelayed({
startActivity(Intent(this, RegisteredList::class.java))
}, 3000)
}
activity_main.xml
<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=".SplashActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.Material3.HeadlineLarge"
android:textStyle="bold"
android:gravity="center"
android:elegantTextHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
DBHandler.kt
package com.example.courseregapp
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
db.execSQL(query)
onCreate(db)
values.put(NAME_COl, name)
values.put(COURSE_COL, course)
val db = this.writableDatabase
val value = db.insert(TABLE_NAME, null, values).toInt()
db.close()
return value != -1
val db = this.readableDatabase
val db = this.writableDatabase
db.close()
return status != 0
companion object{
ListItemModel.kt
package com.example.courseregapp
data class ListItemModel(var id: Int, var name: String, var course: String)
RegisteredList.kt
package com.example.courseregapp
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.courseregapp.databinding.ActivityRegistrationListBinding
super.onCreate(savedInstanceState)
mBinding = ActivityRegistrationListBinding.inflate(layoutInflater)
setContentView(mBinding.root)
initViews()
mBinding.apply {
rvView.layoutManager = LinearLayoutManager(this@RegisteredList)
btnRegister.setOnClickListener {
startActivity(Intent(this@RegisteredList, RegisterActivity::class.java))
}
private fun populateView() {
cursor?.let {
if (list.isNotEmpty())
list.clear()
while(it.moveToNext()) {
super.onResume()
populateView()
mBinding.rvView.adapter = adapter
if (dbHandler.deleteRecord(id)) {
if (list.isNotEmpty()) {
list.removeAt(pos)
adapter?.notifyItemRemoved(pos)
adapter?.notifyItemRangeChanged(pos, list.size)
mBinding.rvView.adapter = adapter
} else {
}
activity_registration_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlt_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegisteredList">
<TextView
android:id="@+id/tv_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textAppearance="@style/TextAppearance.Material3.HeadlineLarge"
android:textStyle="bold"
android:text="Enrolled Students"
android:textColor="@color/white"
android:elevation="10dp"
android:background="@color/dark_blue"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_view"
android:layout_above="@+id/btn_register"
android:background="@color/gray"
android:scrollbars="vertical"
android:overScrollMode="always"
tools:listitem="@layout/activity_list_row"
tools:itemCount="2"/>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
android:padding="16dp"
android:text="Register"/>
</RelativeLayout>
RecyclerAdapter.kt
package com.example.courseregapp
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class RecyclerAdapter(private var mList: List<ListItemModel>, private val listener: RowClickListener): RecyclerView.Adapter<Recycler-
Adapter.ViewHolder>() {
return ViewHolder(view)
holder.let {
it.tvId.text = mList[position].id.toString() ?: "000"
it.tvName.text = mList[position].name
it.tvCourse.text = mList[position].course
it.btnDelete.setOnClickListener {
listener.onItemClickListener(mList[position].id, position)
interface RowClickListener {
activity_list_row.xml
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardElevation="6dp">
<RelativeLayout
android:id="@+id/rlt_sub_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:textColor="@color/black"
android:text="001"
android:textSize="20sp"/>
<LinearLayout
android:id="@+id/llt_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_toEndOf="@id/tv_id"
android:layout_toStartOf="@id/btn_delete"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="1"
android:text="ARUN SIVANANDAN"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:ellipsize="end"
android:maxLines="2"
android:text="BCA"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignBottom="@id/llt_container"
android:layout_marginEnd="5dp"
android:text="Delete"
android:textColor="@color/white"
android:textStyle="bold"
android:backgroundTint="@color/red" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
RegisterActivity.kt
package com.example.courseregapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import com.example.courseregapp.databinding.ActivityRegisterBinding
private var courseList = arrayOf<String>("B.E", "B.Sc", "M.E", "M.Sc", "B.A", "M.A", "B.C.A", "M.C.A", "B.Tech", "M.Tech", "B.Com",
"M.Com")
DBHandler(this, null)
super.onCreate(savedInstanceState)
mBinding = ActivityRegisterBinding.inflate(layoutInflater)
setContentView(mBinding.root)
initViews();
mBinding.apply {
acTv.setAdapter(adapter)
btnSubmit.setOnClickListener {
if (dbHandler.register(edtName.text.toString(), acTv.text.toString())) {
finish()
} else {
activity_register.xml
<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=".RegisterActivity">
<TextView
android:id="@+id/tv_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/dark_blue"
android:elevation="10dp"
android:padding="16dp"
android:text="Register Course"
android:textAppearance="@style/TextAppearance.Material3.HeadlineLarge"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:boxBackgroundMode="outline"
app:boxCornerRadiusBottomEnd="16dp"
app:boxCornerRadiusBottomStart="16dp"
app:boxCornerRadiusTopEnd="16dp"
app:boxCornerRadiusTopStart="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_view">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/dark_blue"
android:textColorHint="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:boxBackgroundMode="outline"
app:boxCornerRadiusBottomEnd="16dp"
app:boxCornerRadiusBottomStart="16dp"
app:boxCornerRadiusTopEnd="16dp"
app:boxCornerRadiusTopStart="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="@+id/ac_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/dark_blue"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:padding="16dp"
android:text="Submit"
android:textSize="18sp"
android:textColor="@color/white"
android:backgroundTint="@color/dark_blue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/til_dropdown" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Naveen S Naveen S
8 Naveen S
M.C.A
3. Create an android application for simple basic calculator
A basic simple calculator android application has been created using kotlin. Below mentioned are the class and layout
files with output screenshots for reference.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CalcApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
activity_calculator.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="data"
type="com.example.calcapp.DataModel" />
<variable
name="activity"
type="com.example.calcapp.MainActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/tabLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textview_calculate_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textSize="@dimen/text_size_normal"
android:text="@{data.calculateBefore}"
android:textColor="@android:color/darker_gray"
android:maxLines="1" />
<TextView
android:id="@+id/textview_calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textSize="@dimen/text_size_large"
android:text="@{data.calculate}"
android:textColor="@color/white"
android:maxLines="1"/>
</LinearLayout>
<TableLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/orange"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TableRow
android:id="@+id/tr_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin"
android:weightSum="4">
<Button
android:id="@+id/button_clear"
android:onClick="@{activity.onClickAction}"
android:text="@string/clear"
android:textAllCaps="true"
style="@style/Btn_Left"/>
<ImageButton
android:id="@+id/button_backspace"
android:layout_width="@dimen/width"
android:layout_height="@dimen/height"
android:layout_weight="1"
android:layout_marginStart="@dimen/margin"
android:src="@drawable/ic_baseline_backspace_24"
android:onClick="@{activity.onClickAction}"
android:background="?attr/colorPrimary"
style="@style/Widget.AppCompat.Button"/>
<Button
android:id="@+id/button_percentage"
android:text="@string/percentage"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_divide"
android:text="@string/divide"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Right"/>
</TableRow>
<TableRow
android:id="@+id/tr_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin"
android:weightSum="4">
<Button
android:id="@+id/button_seven"
android:text="@string/seven"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_eight"
android:text="@string/eight"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left" />
<Button
android:id="@+id/button_nine"
android:text="@string/nine"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_multiply"
android:text="@string/multiply"
android:textAllCaps="false"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Right"/>
</TableRow>
<TableRow
android:id="@+id/tr_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin"
android:weightSum="4">
<Button
android:id="@+id/button_four"
android:text="@string/four"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_five"
android:text="@string/five"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_six"
android:text="@string/six"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_subtract"
android:text="@string/subtract"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Right"/>
</TableRow>
<TableRow
android:id="@+id/tr_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin"
android:gravity="center"
android:weightSum="4">
<Button
android:id="@+id/button_one"
android:text="@string/one"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_two"
android:text="@string/two"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_three"
android:text="@string/three"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Left"/>
<Button
android:id="@+id/button_add"
android:text="@string/add"
android:onClick="@{activity.onClickAction}"
style="@style/Btn_Right"/>
</TableRow>
<TableRow
android:id="@+id/tr_five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin"
android:weightSum="3">
<Button
android:id="@+id/button_dot"
android:layout_width="@dimen/width"
android:layout_height="@dimen/height"
android:layout_weight=".74"
android:layout_marginStart="@dimen/margin"
android:background="?attr/colorPrimary"
android:textColor="@color/white"
android:text="@string/dot"
android:textSize="@dimen/text_size_small"
android:onClick="@{activity.onClickAction}" />
<Button
android:id="@+id/button_zero"
android:layout_width="@dimen/width"
android:layout_height="@dimen/height"
android:layout_weight=".75"
android:layout_marginStart="@dimen/margin"
android:background="?attr/colorPrimary"
android:textColor="@color/white"
android:text="@string/zero"
android:textSize="@dimen/text_size_small"
android:onClick="@{activity.onClickAction}" />
<Button
android:id="@+id/button_equalTo"
android:layout_width="@dimen/width"
android:layout_height="@dimen/height"
android:layout_weight="1.51"
android:layout_marginStart="@dimen/margin"
android:layout_marginEnd="@dimen/margin"
android:background="?attr/colorPrimary"
android:textColor="@color/white"
android:text="@string/equalTo"
android:textSize="@dimen/text_size_small"
android:textAllCaps="false"
android:onClick="@{activity.onClickAction}" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity.xml
package com.example.calcapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import com.example.calcapp.databinding.ActivityCalculatorBinding
DataModel("", "")
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_calculator)
binding.apply {
activity = this@MainActivity
data = dataModel
// textviewCalculate.setHorizontallyScrolling(true)
// textviewCalculate.movementMethod = ScrollingMovementMethod()
onLongClickListenerAction()
when (view.id) {
R.id.button_clear -> {
clearData()
binding.data = dataModel
R.id.button_backspace -> {
if (dataModel.calculate.length > 1)
else
dataModel.calculate = ""
binding.data = dataModel
R.id.button_equalTo -> {
addValues()
if (isAdd)
dataModel.calculateBefore = "$valueOne+$valueTwo"
else if (isSubtract)
dataModel.calculateBefore = "$valueOne-$valueTwo"
else if (isDivide)
else if (isMultiply)
dataCalculate()
binding.data = dataModel
dataModel.apply {
when (value) {
getString(R.string.add) -> {
if (isShowAnswer) {
calculateBefore = ""
valueOne = ""
valueTwo = ""
isShowAnswer = false
return
if (calculate.isNotEmpty()) {
// If calculate has a number then add + and assign to calculateBefore
isAdd = true
addValues()
calculateBefore = "$valueOne+$valueTwo"
calculate = ""
getString(R.string.subtract) -> {
if (isShowAnswer) {
calculateBefore = ""
valueOne = ""
valueTwo = ""
isShowAnswer = false
calculate = calculate.plus(value)
calculate = calculate.plus(value)
isSubtract = true
addValues()
calculateBefore = "$valueOne-$valueTwo"
calculate = ""
getString(R.string.multiply) -> {
if (isShowAnswer) {
calculateBefore = ""
valueOne = ""
valueTwo = ""
isShowAnswer = false
}
if ((calculate.length == 1 && calculate.substring(0) == "-") ||
return
if (calculate.isNotEmpty()) {
isMultiply = true
addValues()
calculate = ""
getString(R.string.divide) -> {
if (isShowAnswer) {
calculateBefore = ""
valueOne = ""
valueTwo = ""
isShowAnswer = false
return
if (calculate.isNotEmpty()) {
isDivide = true
addValues()
calculate = ""
getString(R.string.percentage) -> {
if (isShowAnswer) {
calculateBefore = ""
valueOne = ""
valueTwo = ""
isShowAnswer = false
}
return
if (calculate.isNotEmpty()) {
isPercentage = true
addValues()
calculateBefore = "$valueOne%$valueTwo"
calculate = ""
} else -> {
calculate = calculate.plus(value)
binding.data = this
dataModel.apply {
if (calculate.isNotEmpty()) {
if (valueOne.isEmpty()) {
valueOne = calculate
} else {
valueTwo = calculate
dataModel.apply {
numOne = if (valueOne.length == 2) {
valueOne.substring(1).toDouble()
} else {
valueOne.substring(1, valueOne.length).toDouble()
numTwo = if (valueTwo.length == 2) {
valueTwo.substring(1).toDouble()
} else {
valueTwo.substring(1, valueTwo.length).toDouble()
if (isAdd) {
} else if (isSubtract) {
} else if (isMultiply) {
} else if (isDivide) {
} else if (isPercentage) {
numOne = if (valueOne.length == 2) {
valueOne.substring(1).toDouble()
} else {
valueOne.substring(1, valueOne.length).toDouble()
valueTwo.substring(0, valueTwo.length).toDouble()
} else {
valueTwo.substring(0).toDouble()
}
if (isAdd) {
} else if (isSubtract) {
} else if (isMultiply) {
} else if (isDivide) {
} else if (isPercentage) {
valueOne.substring(0, valueOne.length).toDouble()
} else {
valueOne.substring(0).toDouble()
numTwo = if (valueTwo.length == 2) {
valueTwo.substring(1).toDouble()
} else {
valueTwo.substring(1, valueTwo.length).toDouble()
if (isAdd) {
} else if (isSubtract) {
} else if (isMultiply) {
} else if (isDivide) {
} else if (isPercentage) {
} else {
else
valueOne.substring(0).toDouble()
valueTwo.substring(0, valueTwo.length).toDouble()
else
valueTwo.substring(0).toDouble()
if (isAdd) {
} else if (isSubtract) {
} else if (isMultiply) {
} else if (isDivide) {
} else if (isPercentage) {
} else {
total.toString()
} else {
total.toInt().toString()
isShowAnswer = true
isAdd = false
isSubtract = false
isMultiply = false
isDivide = false
isPercentage = false
binding.data = this
valueOne = ""
valueTwo = ""
dataModel.apply {
calculateBefore = ""
calculate = ""
valueOne = ""
valueTwo = ""
binding.buttonBackspace.setOnLongClickListener {
clearData()
binding.data = dataModel
true
}
Output :
5000 x 4.8
= 24,000