vendredi 23 avril 2021

How to save a Checkbox state in android studio?

I'm new to android studio and I just can't figure out how to save the checkbox state using sharedpreference. If someone can help me I would greatly appreciate the assistance.

class SelectAlertSettings : AppCompatActivity() {

    private lateinit var mp : MediaPlayer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.select_alert_config)


    }

    fun onCheckboxClicked(view: View) {
        if (view is CheckBox) {
            val checked: Boolean = view.isChecked

            when (view.id) {
                R.id.checkbox_proximity_alert -> {
                    if (checked) {

                        val proximityAlert = R.raw.proximity_alert
                        mp = MediaPlayer.create(this, proximityAlert)
                        mp.start()

                    } else {

                        mp.stop()
                    }

                }

            }

        }

        val btnCancel : Button = findViewById(R.id.btnDone)
        btnCancel.setOnClickListener{
            finish()
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire