Skip to content

Commit

Permalink
complet simple Kotlin calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Hash001001 committed Nov 1, 2019
1 parent 01e5e56 commit 2399a6f
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'net.objecthunter:exp4j:0.4.8'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,88 @@ package com.myfirstapplication.ashirraja.simple_calculator_kotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import net.objecthunter.exp4j.ExpressionBuilder

class MainActivity : AppCompatActivity() {


lateinit var outputTextView: TextView
var lastNumaric: Boolean= false
var stateError: Boolean = false
var lastDot :Boolean=false


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
outputTextView=findViewById(R.id.txtInput)
}

fun onDigit(view: View)
{
if(stateError)
{
outputTextView.text=(view as Button).text
stateError=false
}else {
outputTextView.append((view as Button).text)
}
lastNumaric=true
}

fun onDecimalPoint(view: View)
{
if(lastNumaric && !stateError && !lastDot)
{
outputTextView.append(".")
lastNumaric=false
lastDot=true
}
}

fun onOperator (view:View)
{
if(lastNumaric && !stateError)
{
outputTextView.append((view as Button).text)
lastNumaric=false
lastDot=false
}
}


fun onClear(view:View)
{
this.outputTextView.text= ""
lastNumaric=false
stateError=false
lastDot=false
}
fun onEqual(view:View)
{

if(lastNumaric && !stateError)
{
val text = outputTextView.text.toString()
val expression= ExpressionBuilder(text).build()
try
{
val result= expression.evaluate()
outputTextView.text= result.toString()
lastDot=true
}catch (ex:Exception)
{
outputTextView.text="Error"
stateError=true
lastNumaric=false
}
}

}


}
19 changes: 19 additions & 0 deletions app/src/main/res/drawable/button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#9EB8FF" android:type="linear"/>
<padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
<size android:width="60dp" android:height="60dp"/>
<stroke android:width="1dp" android:color="#ff3da6ef"/>
</shape>
</item>
<item>
<shape>
<gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#ffd9d9d9" android:type="linear"/>
<padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
<size android:width="60dp" android:height="60dp"/>
<stroke android:width="0.5dp" android:color="#ffcecece"/>
</shape>
</item>
</selector>
216 changes: 212 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,220 @@
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
android:ems="10"
android:textSize="48sp"
android:background="#efefef"
android:id="@+id/txtInput"
android:gravity="right|center_vertical"
android:maxLength="12"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"/>

<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_goneMarginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtInput"
android:gravity="fill">

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">

<Button
android:text="7"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnSeven"/>

<Button
android:text="8"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnEight"/>

<Button
android:text="9"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnNine"/>

<Button
android:text="/"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onOperator"
android:id="@+id/btnDivide"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">

<Button
android:text="4"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnFour"/>

<Button
android:text="5"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnFive"/>

<Button
android:text="6"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnSix"/>

<Button
android:text="*"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onOperator"
android:id="@+id/btnMultiply"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">

<Button
android:text="1"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnOne"/>

<Button
android:text="2"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnTwo"/>

<Button
android:text="3"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnThree"/>

<Button
android:text="-"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onOperator"
android:id="@+id/btnSubtract"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">

<Button
android:text="."
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDecimalPoint"
android:id="@+id/btnDecimal"/>

<Button
android:text="0"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onDigit"
android:id="@+id/btnZero"/>

<Button
android:text="CLR"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onClear"
android:id="@+id/btnClear"/>

<Button
android:text="+"
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onOperator"
android:id="@+id/btnAdd"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<Button
android:text="="
android:background="@drawable/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:onClick="onEqual"
android:id="@+id/btnEqual"/>
</TableRow>
</TableLayout>

</android.support.constraint.ConstraintLayout>

0 comments on commit 2399a6f

Please sign in to comment.