Home > Net >  Save previous background value of checkbox in Kotlin
Save previous background value of checkbox in Kotlin

Time:02-23

I have a stop button that turns all my checkboxes red. When I select a checkbox it will turn blue, and when I uncheck it it will turn black.

I want my checkboxes to turn to red when their previous color was red and I want my checkboxes to turn black when their previous color was black.

This is the code I have right now to change the colors.

Change checked color

for (checkBox in checBoxes)
        {
            checkBox.setOnClickListener(View.OnClickListener {
                if (checkBox.isChecked) {
                    checkBox.setBackgroundResource(R.drawable.blue_button)
                } else {
                    checkBox.setBackgroundResource(R.drawable.black_button)
                }
            })

        }

Stop button

stopBtn.setOnClickListener {
for (checkBox in checBoxes)
    {
      checkBox.setBackgroundResource(R.drawable.red_button)
    }

}

CodePudding user response:

You can create boolean that will tell if user have clicked stop button like this:

val isStopped = false

Than make your onClickListener lambda function like this:

stopBtn.setOnClickListener {
isStopped = true
for (checkBox in checBoxes)
    {
      checkBox.setBackgroundResource(R.drawable.red_button)
    }

}

And then:

for (checkBox in checBoxes)
        {
            checkBox.setOnClickListener(View.OnClickListener {
                if (checkBox.isChecked) {
                    checkBox.setBackgroundResource(R.drawable.blue_button)
                } else {
                if(isStopped)
                    checkBox.setBackgroundResource(R.drawable.red_button)
                else
                    checkBox.setBackgroundResource(R.drawable.black_button)
                }
            })

        }

CodePudding user response:

This is my updated code @dženan-bećirović

Stop button


stopBtn.setOnClickListener {
for (checkBox in checBoxes)
    {
      checkBox.setBackgroundResource(R.drawable.red_button)
      checkBoxBG = checkBox.background
    }

}

Change checked color

for (checkBox in checBoxes)
        {
            checkBox.setOnClickListener(View.OnClickListener {
                if (checkBox.isChecked) {
                    checkBoxBG = checkBox.background
                    checkBox.setBackgroundResource(R.drawable.blue_button)
                } else {
                       when {
                        checkBoxBG?.getConstantState()?.equals(getDrawable(CheckboxColorStatus.RED.colorValue)?.getConstantState()) == true -> {
                            checkBox.setBackgroundResource(R.drawable.red_button)
                        }
                        checkBoxBG?.getConstantState()?.equals(getDrawable(CheckboxColorStatus.BLACK.colorValue)?.getConstantState()) == true -> {
                            checkBox.setBackgroundResource(R.drawable.black_button)
                        }
                        checkBoxBG?.getConstantState()?.equals(getDrawable(CheckboxColorStatus.YELLOW.colorValue)?.getConstantState()) == true -> {
                            checkBox.setBackgroundResource(R.drawable.yellow_button)
                        }
                    }
                }
            })

        }
  • Related