0% found this document useful (0 votes)
16 views9 pages

Create Calculator App Using Kotline Assignment

Uploaded by

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

Create Calculator App Using Kotline Assignment

Uploaded by

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

Create calculator App using kotline

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:background="@color/almostBlack"
tools:ignore="HardcodedText"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:padding="20dp">

<TextView
android:id="@+id/workingsTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:lines="2"
android:maxLines="2"
android:textColor="@color/white"
android:textAlignment="textEnd"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="@id/resultsTV"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/resultsTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:lines="1"
android:maxLines="1"
android:textColor="@color/white"
android:textAlignment="textEnd"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

<LinearLayout
style="@style/buttonRow">

<Button
style="@style/buttonNumber"
android:textColor="@color/red"
android:onClick="allClearAction"
android:text="AC"/>
<Button
style="@style/buttonNumber"
android:textColor="@color/red"
android:onClick="backSpaceAction"
android:text="⌫"/>
<Button
style="@style/buttonNumber"
android:text="" />
<Button
style="@style/buttonOperator"
android:text="/"/>

</LinearLayout>
<LinearLayout
style="@style/buttonRow">

<Button
style="@style/buttonNumber"
android:text="7"/>
<Button
style="@style/buttonNumber"
android:text="8"/>
<Button
style="@style/buttonNumber"
android:text="9" />
<Button
style="@style/buttonOperator"
android:text="x"/>

</LinearLayout>

<LinearLayout
style="@style/buttonRow">

<Button
style="@style/buttonNumber"
android:text="4"/>
<Button
style="@style/buttonNumber"
android:text="5"/>
<Button
style="@style/buttonNumber"
android:text="6" />
<Button
style="@style/buttonOperator"
android:text="-"/>

</LinearLayout>

<LinearLayout
style="@style/buttonRow">

<Button
style="@style/buttonNumber"
android:text="1"/>
<Button
style="@style/buttonNumber"
android:text="2"/>
<Button
style="@style/buttonNumber"
android:text="3" />
<Button
style="@style/buttonOperator"
android:text="+"/>

</LinearLayout>

<LinearLayout
style="@style/buttonRow">

<Button
style="@style/buttonNumber"
android:text="."/>
<Button
style="@style/buttonNumber"
android:text="0"/>
<Button
style="@style/buttonNumber"
android:layout_weight="2"
android:background="@color/orange"
android:textSize="40sp"
android:onClick="equalsAction"
android:text="=" />

</LinearLayout>

</LinearLayout>

Colors.xml

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


<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#F00</color>
<color name="almostBlack">#222</color>
<color name="orange">#FF8c00</color>
</resources>

Styles.xml

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


<resources>
<style name="buttonRow">
<item name="android:layout_weight">1</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
</style>

<style name="buttonNumber">
<item name="android:layout_weight">1</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">@null</item>
<item name="android:textSize">25sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:onClick">numberAction</item>
</style>

<style name="buttonOperator">
<item name="android:layout_weight">1</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">@null</item>
<item name="android:textSize">25sp</item>
<item name="android:textColor">@color/orange</item>
<item name="android:onClick">operationAction</item>
</style>

</resources>

Themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.KotlinCalculatorApp" parent="Theme.AppCompat">

<item name="colorPrimary">@color/orange</item>
<item name="colorPrimaryVariant">@color/almostBlack</item>
<item name="colorOnPrimary">@color/white</item>

<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

</style>
</resources>

MainActivity.kt
package code.with.cal.kotlincalculatorapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity()


{
private var canAddOperation = false
private var canAddDecimal = true

override fun onCreate(savedInstanceState: Bundle?)


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

fun numberAction(view: View)


{
if(view is Button)
{
if(view.text == ".")
{
if(canAddDecimal)
workingsTV.append(view.text)

canAddDecimal = false
}
else
workingsTV.append(view.text)

canAddOperation = true
}
}

fun operationAction(view: View)


{
if(view is Button && canAddOperation)
{
workingsTV.append(view.text)
canAddOperation = false
canAddDecimal = true
}
}

fun allClearAction(view: View)


{
workingsTV.text = ""
resultsTV.text = ""
}

fun backSpaceAction(view: View)


{
val length = workingsTV.length()
if(length > 0)
workingsTV.text = workingsTV.text.subSequence(0, length - 1)
}

fun equalsAction(view: View)


{
resultsTV.text = calculateResults()
}

private fun calculateResults(): String


{
val digitsOperators = digitsOperators()
if(digitsOperators.isEmpty()) return ""

val timesDivision = timesDivisionCalculate(digitsOperators)


if(timesDivision.isEmpty()) return ""

val result = addSubtractCalculate(timesDivision)


return result.toString()
}

private fun addSubtractCalculate(passedList: MutableList<Any>): Float


{
var result = passedList[0] as Float

for(i in passedList.indices)
{
if(passedList[i] is Char && i != passedList.lastIndex)
{
val operator = passedList[i]
val nextDigit = passedList[i + 1] as Float
if (operator == '+')
result += nextDigit
if (operator == '-')
result -= nextDigit
}
}

return result
}
private fun timesDivisionCalculate(passedList: MutableList<Any>): MutableList<Any>
{
var list = passedList
while (list.contains('x') || list.contains('/'))
{
list = calcTimesDiv(list)
}
return list
}

private fun calcTimesDiv(passedList: MutableList<Any>): MutableList<Any>


{
val newList = mutableListOf<Any>()
var restartIndex = passedList.size

for(i in passedList.indices)
{
if(passedList[i] is Char && i != passedList.lastIndex && i < restartIndex)
{
val operator = passedList[i]
val prevDigit = passedList[i - 1] as Float
val nextDigit = passedList[i + 1] as Float
when(operator)
{
'x' ->
{
newList.add(prevDigit * nextDigit)
restartIndex = i + 1
}
'/' ->
{
newList.add(prevDigit / nextDigit)
restartIndex = i + 1
}
else ->
{
newList.add(prevDigit)
newList.add(operator)
}
}
}

if(i > restartIndex)


newList.add(passedList[i])
}

return newList
}
private fun digitsOperators(): MutableList<Any>
{
val list = mutableListOf<Any>()
var currentDigit = ""
for(character in workingsTV.text)
{
if(character.isDigit() || character == '.')
currentDigit += character
else
{
list.add(currentDigit.toFloat())
currentDigit = ""
list.add(character)
}
}

if(currentDigit != "")
list.add(currentDigit.toFloat())

return list
}

You might also like