This is my dilema: I have an activity that uses a toggle button(A00) with the code below. When the activity is created, the function sendCommand is not called(I have to check and uncheck to the uncheck command happen). It's like the toggle button has a neutral state when is created.
A00.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
sendCommand("1050000")
A00.setBackgroundColor(Color.rgb(248, 250, 237))
A00.setTextColor(Color.rgb(96, 96, 96))
} else {
sendCommand("0050000")
A00.setBackgroundColor(Color.rgb(96, 96, 96))
A00.setTextColor(Color.rgb(248, 250, 237)) }
}
Then I have this other activity that uses a spinner. As soon the activity is created, the spinner starts at position 0, which runs the function sendCommand without doing anything.
I need the spinner to behave like the toggle button, with something like a "neutral state" The sendCommand cannot run when the activity is created. Thanks!
SC19.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val selectedItem = view as TextView
if (position == 0) { sendCommand("0000219") }
if (position == 1) { sendCommand("1000219")
selectedItem.setTextColor(Color.parseColor("#f8faed"))
selectedItem.setBackgroundColor(Color.parseColor("#ff4500"))
}
if (position == 2) { sendCommand("1060219")
selectedItem.setTextColor(Color.parseColor("#454545"))
selectedItem.setBackgroundColor(Color.parseColor("#32cd32"))
}
if (position == 3) { sendCommand("1100219")
selectedItem.setTextColor(Color.parseColor("#f8faed"))
selectedItem.setBackgroundColor(Color.parseColor("#1e90ff"))
}
if (position == 4) { sendCommand("1040219")
selectedItem.setTextColor(Color.parseColor("#454545"))
selectedItem.setBackgroundColor(Color.parseColor("#ffff00"))
}
}
}