Mohanad Aljhani

Mohanad Aljhani
mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.2 kB
4
Indexable
Never
//MainActivity Mohanad Aljhani

package com.example.tempconverter
import android.R
import android.app.AlertDialog
import android.app.AlertDialog.Builder
import android.content.DialogInterface
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import com.ayushsb24.tempconverter.databinding.ActivityMainBinding
import java.text.DecimalFormat


class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    lateinit var selectedUnit: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val df = DecimalFormat("#.##")//Decimal formatter
        selectedUnit = "Fahrenheit"
        binding.selectType.setOnClickListener() {
            showAlertDialog()
        }
        binding.editInput.addTextChangedListener() {
            val resultText: String
            var inputVal = binding.editInput.text.toString()
            if (inputVal.isNotEmpty()) {
                var doubleInput = inputVal.toDouble()
                if(selectedUnit == "Fahrenheit"){
                    resultText = df.format((doubleInput - 32) * 0.5556)
                    binding.textResultType.text = "Celsius"
                }else{

                    resultText = df.format((doubleInput *1.8)+32)
                    binding.textResultType.text = "Fahrenheit"
                }
                binding.textResult.text = resultText
            }
        }
    }
    private fun showAlertDialog() {
        val alertDialog: Builder = Builder(this@MainActivity)
        alertDialog.setTitle("Select Unit") //Setting title for alertBox
        val items = arrayOf("Fahrenheit", "Celsius")
        val checkedItem = -1
        alertDialog.setSingleChoiceItems(items, checkedItem,
            DialogInterface.OnClickListener { dialog, which ->
                selectedUnit = items[which]
                binding.textType.setText(items[which])
            })
        alertDialog.setPositiveButton(
            R.string.ok,
            DialogInterface.OnClickListener { dialog, which ->
                dialog.dismiss()
            })
        val alert: AlertDialog = alertDialog.create()
        alert.setCanceledOnTouchOutside(false)
        alert.show()
    }
}
private fun showAlertDialog() {
    val alertDialog: Builder = Builder(this@MainActivity)
    alertDialog.setTitle("Select Unit") //Setting title for alertBox
    val items = arrayOf("Fahrenheit", "Celsius") //Array to contained Options
    val checkedItem = -1
    alertDialog.setSingleChoiceItems(items, checkedItem,
        DialogInterface.OnClickListener { dialog, which ->
            selectedUnit = items[which]
            binding.textType.setText(items[which])
        })
    alertDialog.setPositiveButton(
        R.string.ok,
        DialogInterface.OnClickListener { dialog, which ->
            dialog.dismiss()
        })
    val alert: AlertDialog = alertDialog.create()
    alert.setCanceledOnTouchOutside(false)
    alert.show()
}


// activity_main.xml