Create calculator App using kotline assignment
Create calculator App using kotline assignment
Activity_main.xml
<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
Styles.xml
<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>
</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.*
canAddDecimal = false
}
else
workingsTV.append(view.text)
canAddOperation = true
}
}
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
}
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)
}
}
}
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
}