-2

Here is my code and I need a comparison function to replace true so it can set different colors. I've tried getBackground() with many errors and I have tried other options on Stack here with no success.

  private fun makeColored(view: View) {
        when (view.id) {

               
            R.id.box_one_text -> if (true) view.setBackgroundColor(Color.DKGRAY) else view.setBackgroundColor(Color.WHITE)

1 Answers1

1

To the best of my knowledge, this is the answer I could provide although there might be better answers.

button.tag = "DEFAULT"
button.setOnClickListener { view ->
    makeColored(view)
}

Your makeColored function should be:

private fun makeColored(view: View) {
    when (view.tag) {
        "DEFAULT" -> {
            view.setBackgroundColor(Color.GREEN)
            view.tag = Color.GREEN
        }
        Color.RED -> {
            view.setBackgroundColor(Color.BLUE)
            view.tag = Color.BLUE
        }
        Color.BLUE -> {
            view.setBackgroundColor(Color.GREEN)
            view.tag = Color.GREEN
        }
        Color.GREEN -> {
            view.setBackgroundColor(Color.RED)
            view.tag = Color.RED
        }
    }
}