package com.example.calcapptoturial
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var inputTextView: TextView
private lateinit var calculatedGradeTextView: TextView
private lateinit var historyTextView: TextView
private lateinit var currentModeTextView: TextView
private var totalFormativeGrades: Double = 0.0
private var totalSummativeGrades: Double = 0.0
private var formativeWeight: Double = 40.0 // Default formative weight
private var summativeWeight: Double = 60.0 // Default summative weight
private var formativeGradeCount: Int = 0
private var summativeGradeCount: Int = 0
companion object {
const val REQUEST_CODE_SETTINGS = 1001 // Define the request code
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
inputTextView = findViewById(R.id.inputext)
calculatedGradeTextView = findViewById(R.id.calculatedgradetext)
historyTextView = findViewById(R.id.historytext)
currentModeTextView = findViewById(R.id.currentmodetext)
// Initialize settingsButton
val settingsButton = findViewById<ImageButton>(R.id.settingsButton)
// Set click listener for settingsButton
settingsButton.setOnClickListener {
val intent = Intent(this, SettingsActivity::class.java)
intent.putExtra(SettingsActivity.EXTRA_FORMATIVE_WEIGHT, formativeWeight)
intent.putExtra(SettingsActivity.EXTRA_SUMMATIVE_WEIGHT, summativeWeight)
startActivityForResult(intent, REQUEST_CODE_SETTINGS)
}
// Set max length for input text view
inputTextView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
if (s?.length ?: 0 > 5) {
s?.delete(5, s.length)
}
}
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_SETTINGS && resultCode == RESULT_OK) {
// Update weights from settings
formativeWeight = data?.getDoubleExtra(SettingsActivity.EXTRA_FORMATIVE_WEIGHT, formativeWeight) ?: formativeWeight
summativeWeight = data?.getDoubleExtra(SettingsActivity.EXTRA_SUMMATIVE_WEIGHT, summativeWeight) ?: summativeWeight
// Update mode text view with updated weights
updateModeTextView()
// Recalculate grade with updated weights
updateCalculatedGrade()
}
}
override fun onResume() {
super.onResume()
// Update mode text view with weights from settings
updateModeTextView()
// Recalculate grade when returning from settings
updateCalculatedGrade()
}
fun onDigitClick(view: View) {
if (inputTextView.text.length < 5) { // Allow up to 5 digits
if (view is TextView) {
val digit: String = view.text.toString()
inputTextView.append(digit)
}
}
}
fun onequalClick(view: View) {
val grade = inputTextView.text.toString().toDoubleOrNull()
if (grade != null) {
updateHistory(grade)
updateCalculatedGrade()
inputTextView.text = ""
}
}
fun onbackClick(view: View) {
val expression = inputTextView.text.toString()
if (expression.isNotEmpty()) {
inputTextView.text = expression.substring(0, expression.length - 1)
}
}
fun onMClick(view: View) {
// Toggle between Formative and Summative mode
if (currentModeTextView.text.contains("Formative")) {
currentModeTextView.text = "Summative Mode: (${String.format("%.1f", summativeWeight)}%)"
} else {
currentModeTextView.text = "Formative Mode: (${String.format("%.1f", formativeWeight)}%)"
}
// Recalculate grade when mode changes
updateCalculatedGrade()
}
fun onclearAllClick(view: View) {
inputTextView.text = ""
calculatedGradeTextView.text = "Grade: 0.0%"
historyTextView.text = ""
historyTextView.visibility = View.GONE // Set visibility to gone
totalFormativeGrades = 0.0
totalSummativeGrades = 0.0
formativeGradeCount = 0
summativeGradeCount = 0
}
private fun updateCalculatedGrade() {
val newAverage = if (formativeGradeCount == 0) {
totalSummativeGrades / summativeGradeCount
} else if (summativeGradeCount == 0) {
totalFormativeGrades / formativeGradeCount
} else {
(totalSummativeGrades / summativeGradeCount) * (summativeWeight / 100.0) +
(totalFormativeGrades / formativeGradeCount) * (formativeWeight / 100.0)
}
val formattedGrade = if (newAverage.isNaN()) {
"0.0%"
} else {
"${String.format("%.2f", newAverage)}%"
}
calculatedGradeTextView.text = "Grade: $formattedGrade"
}
private fun updateHistory(grade: Double) {
if (historyTextView.text.isNotEmpty()) {
historyTextView.append(" + ")
}
historyTextView.append(grade.toInt().toString())
historyTextView.visibility = View.VISIBLE // Set visibility to visible
// Update flag based on mode
if (currentModeTextView.text.contains("Formative")) {
totalFormativeGrades += grade
formativeGradeCount++
} else {
totalSummativeGrades += grade
summativeGradeCount++
}
}
private fun updateModeTextView() {
if (currentModeTextView.text.contains("Formative")) {
currentModeTextView.text = "Formative Mode: (${String.format("%.1f", formativeWeight)}%)"
} else {
currentModeTextView.text = "Summative Mode: (${String.format("%.1f", summativeWeight)}%)"
}
}
fun onQuitButtonClick(view: View?) {
finish() // Finish the activity, effectively quitting the app
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter