package com.example.calcapptoturial
import android.content.Intent
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.ImageButton
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class SettingsActivity : AppCompatActivity() {
private lateinit var editTextFormativeWeight: EditText
private lateinit var editTextSummativeWeight: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dialog_settings)
// Initialize EditText fields
editTextFormativeWeight = findViewById(R.id.editTextFormativeWeight)
editTextSummativeWeight = findViewById(R.id.editTextSummativeWeight)
// Set current weights as hints for EditText fields
val currentFormativeWeight = intent.getDoubleExtra(EXTRA_FORMATIVE_WEIGHT, 40.0)
val currentSummativeWeight = intent.getDoubleExtra(EXTRA_SUMMATIVE_WEIGHT, 60.0)
editTextFormativeWeight.hint = "Current Formative Weight: $currentFormativeWeight"
editTextSummativeWeight.hint = "Current Summative Weight: $currentSummativeWeight"
// Set click listener for the save button
findViewById<Button>(R.id.btnSave).setOnClickListener {
saveWeights()
finish() // Close the settings activity
}
val themeOptions = arrayOf(
"Keller HS Light",
"Timber Creek HS Light",
"Fossil Ridge HS Light",
"Keller Central HS Light",
"Keller HS Dark",
"Timber Creek HS Dark",
"Fossil Ridge HS Dark",
"Keller Central HS Dark",
"Default (Light + Orange)"
)
val themeSpinner = findViewById<Spinner>(R.id.themeSpinner)
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, themeOptions)
themeSpinner.adapter = adapter
// Find the close button
val closeButton = findViewById<ImageButton>(R.id.btnClose)
// Set click listener for the close button
closeButton.setOnClickListener {
// Close the settings activity
finish()
}
}
private fun saveWeights() {
// Get the input values from EditText fields
val formativeWeightInput = editTextFormativeWeight.text.toString()
val summativeWeightInput = editTextSummativeWeight.text.toString()
// Convert input values to doubles
val formativeWeight = if (formativeWeightInput.isNotEmpty()) formativeWeightInput.toDouble() else 40.0
val summativeWeight = if (summativeWeightInput.isNotEmpty()) summativeWeightInput.toDouble() else 60.0
// Create an intent to pass the weights back to MainActivity
val intent = Intent().apply {
putExtra(EXTRA_FORMATIVE_WEIGHT, formativeWeight)
putExtra(EXTRA_SUMMATIVE_WEIGHT, summativeWeight)
}
setResult(RESULT_OK, intent) // Set result OK to indicate successful operation
}
companion object {
const val EXTRA_FORMATIVE_WEIGHT = "extra_formative_weight"
const val EXTRA_SUMMATIVE_WEIGHT = "extra_summative_weight"
}
}
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