Last active
January 5, 2025 12:39
-
-
Save ShailMurtaza/7bc809ad2153054d75213b54373d742a to your computer and use it in GitHub Desktop.
Unit Conversion Application Main_Activity file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.unitconversionapplication | |
import android.os.Bundle | |
import androidx.activity.enableEdgeToEdge | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.view.ViewCompat | |
import androidx.core.view.WindowInsetsCompat | |
import android.text.Editable | |
import android.text.TextWatcher | |
import android.widget.* | |
class MainActivity : AppCompatActivity() { | |
private lateinit var inputBox: EditText | |
private lateinit var outputBox: TextView | |
private lateinit var unitTypeGroup: RadioGroup | |
private lateinit var lengthUnitSpinner1: Spinner | |
private lateinit var lengthUnitSpinner2: Spinner | |
private lateinit var temperatureUnitSpinner1: Spinner | |
private lateinit var temperatureUnitSpinner2: Spinner | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
inputBox = findViewById(R.id.inputBox) | |
outputBox = findViewById(R.id.outputBox) | |
unitTypeGroup = findViewById(R.id.unitTypeGroup) | |
lengthUnitSpinner1 = findViewById(R.id.lengthUnitSpinner1) | |
lengthUnitSpinner2 = findViewById(R.id.lengthUnitSpinner2) | |
temperatureUnitSpinner1 = findViewById(R.id.temperatureUnitSpinner1) | |
temperatureUnitSpinner2 = findViewById(R.id.temperatureUnitSpinner2) | |
// Set up radio button change listener | |
unitTypeGroup.setOnCheckedChangeListener { _, checkedId -> | |
when (checkedId) { | |
R.id.lengthRadioButton -> { | |
lengthUnitSpinner1.visibility = Spinner.VISIBLE | |
lengthUnitSpinner2.visibility = Spinner.VISIBLE | |
temperatureUnitSpinner1.visibility = Spinner.GONE | |
temperatureUnitSpinner2.visibility = Spinner.GONE | |
} | |
R.id.temperatureRadioButton -> { | |
lengthUnitSpinner1.visibility = Spinner.GONE | |
lengthUnitSpinner2.visibility = Spinner.GONE | |
temperatureUnitSpinner1.visibility = Spinner.VISIBLE | |
temperatureUnitSpinner2.visibility = Spinner.VISIBLE | |
} | |
} | |
} | |
// Set up input box listener to perform conversions | |
inputBox.addTextChangedListener(object : TextWatcher { | |
override fun afterTextChanged(s: Editable?) { | |
performConversion() | |
} | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} | |
}) | |
} | |
private fun performConversion() { | |
val inputText = inputBox.text.toString() | |
if (inputText.isNotEmpty()) { | |
val inputValue = inputText.toDoubleOrNull() ?: return | |
val outputValue = when (unitTypeGroup.checkedRadioButtonId) { | |
R.id.lengthRadioButton -> convertLength(inputValue) | |
R.id.temperatureRadioButton -> convertTemperature(inputValue) | |
else -> null | |
} | |
outputBox.text = outputValue?.toString() ?: "" | |
} else { | |
outputBox.text = "" | |
} | |
} | |
private fun convertLength(value: Double): Double? { | |
val fromUnit = lengthUnitSpinner1.selectedItem.toString() | |
val toUnit = lengthUnitSpinner2.selectedItem.toString() | |
val baseValue = when (fromUnit) { | |
"m" -> value | |
"km" -> value * 1000 | |
"inches" -> value * 0.0254 | |
"feet" -> value * 0.3048 | |
else -> return null | |
} | |
return when (toUnit) { | |
"m" -> baseValue | |
"km" -> baseValue / 1000 | |
"inches" -> baseValue / 0.0254 | |
"feet" -> baseValue / 0.3048 | |
else -> null | |
} | |
} | |
private fun convertTemperature(value: Double): Double? { | |
val fromUnit = temperatureUnitSpinner1.selectedItem.toString() | |
val toUnit = temperatureUnitSpinner2.selectedItem.toString() | |
return when (fromUnit to toUnit) { | |
"centigrade" to "fahrenheit" -> (value * 9/5) + 32 | |
"fahrenheit" to "centigrade" -> (value - 32) * 5/9 | |
else -> value // if fromUnit == toUnit | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment