Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculator app #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,87 @@ package com.scainitiative.kotlinscainitiativeproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

var isOperator = true
var numbers = ""
var operators = "+"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val w = window;
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}

fun numberClicked(view: View) {
if (isOperator)
displayEdittext.setText("")
isOperator = false

var buttonClicked = displayEdittext.text.toString()
var buttonSelected = view as Button
when(buttonSelected.id){

cal_one.id -> {buttonClicked += "1"}
cal_two.id -> {buttonClicked += "2"}
cal_three.id -> {buttonClicked += "3"}
cal_four.id -> {buttonClicked += "4"}
cal_five.id -> {buttonClicked += "5"}
cal_six.id -> {buttonClicked += "6"}
cal_seven.id -> {buttonClicked += "7"}
cal_eight.id -> {buttonClicked += "8"}
cal_nine.id -> {buttonClicked += "9"}
cal_zero.id -> {buttonClicked += "0"}
cal_full_stop.id -> {buttonClicked += "."}
cal_add_minus.id -> {buttonClicked + "-$buttonClicked"}

}
displayEdittext.setText(buttonClicked)

}

fun signOperation(view: View) {
isOperator = true
numbers = displayEdittext.text.toString()
var buttonSelected = view as Button
when (buttonSelected.id){
cal_minus.id -> operators = "-"
cal_divide.id -> operators = "/"
cal_multiply.id -> operators = "*"
cal_plus.id -> operators = "+"
}


}

fun equalTo(view: View) {
var anumber = displayEdittext.text.toString()
var result = 0.0
when (operators){
"+" -> {result = numbers.toDouble() + anumber.toDouble()}
"*" -> {result = numbers.toDouble() * anumber.toDouble()}
"-" -> {result = numbers.toDouble() - anumber.toDouble()}
"/" -> {result = numbers.toDouble() / anumber.toDouble()}
}
displayEdittext.setText(result.toString())

}

fun clear(view: View) {
displayEdittext.setText("0")
isOperator = true
}

fun percentage(view: View) {
var num = displayEdittext.text.toString().toDouble()/100
displayEdittext.setText(num.toString())
isOperator = true

}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_arrow_downward_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#261CFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"/>
</vector>
Loading