0

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"))
        }
    }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
frnnd
  • 13
  • 7
  • In other words, I want this block of code to run only the second time it's called. Like a post increment. Is there a way to do that? Thanks! – frnnd Jan 05 '20 at 04:22

1 Answers1

0

It does not have a neutral state, but rather a default state. Effectively creating the toggle button does not trigger the check change listener, as the state is not changed, but set.

The most off-the-bat solution would be to extract the code of

   sendCommand("1050000")
   A00.setBackgroundColor(Color.rgb(248, 250, 237))
   A00.setTextColor(Color.rgb(96, 96, 96))

To a separate function and also call it in an activity/fragment callback.

The second part of the question is a repost, see here.

  • Thank you for your answer! Could you give me a brief example on that? "separate function and also call it in an activity/fragment creation". Thanks! – frnnd Jan 05 '20 at 11:05
  • Sure! Make a separate like this, in the same file: `fun setCheckedState(){ sendCommand("1050000") A00.setBackgroundColor(Color.rgb(248, 250, 237)) A00.setTextColor(Color.rgb(96, 96, 96)) }` Then, call it like this: `override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setCheckedState() }` And in your initial function: `A00.setOnCheckedChangeListener { _, isChecked -> if (isChecked) { setCheckedState() } else { ...}` – Andrei-Cristian Rad Jan 05 '20 at 11:12