'kotlin quiz i can't color answer because don't run ContextCompat
Could you help me with the following code, I can't get it to paint the correct option green, or show the animation, of the CorrectResponse function. If I delete the defaultColors() function, the green color is shown, but it remains in the next question. when I write the default function it doesn't execute the code: tv_option_one.background = ContextCompat.getDrawable(this, R.drawable.correct_option_border_bg) I have tried to insert a sleep to defaultColors() to see the color "correct_option_border_bg(green)" but it is not displayed. Any advice ?? Thank you.
class QuestionActivity : AppCompatActivity() {
var quizzes: MutableList<Quiz>? = null
var contadorPregunta = 0
var questions: MutableMap<String, Question>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
// VISUALIZACION PREGUNTAS
private fun bindViews() {
if (contadorPregunta == questions!!.size) { // last question
Toast.makeText(this, "HAS COMPLETADO TODAS LAS PREGUNTAS", Toast.LENGTH_SHORT).show()
val intent = Intent(this, ResultActivity::class.java)
startActivity(intent)
}
else{
defaultColors()
}
val pregunta = indicePreguntas[contadorPregunta]
val question = questions!!["question$pregunta"]
question?.let {
tvDescription.text = it.description
tv_option_one.text = it.option1
tv_option_two.text = it.option2
tv_option_three.text = it.option3
tv_option_four.text = it.option4
val respuesta = it.answer
tv_option_one.setOnClickListener {
if (tv_option_one.text == respuesta) {
Toast.makeText(this, "RESPUESTA CORRECTA", Toast.LENGTH_SHORT).show()
tv_option_one.background = ContextCompat.getDrawable(this, R.drawable.correct_option_border_bg)
respuestaCorrecta()
} else {
tv_option_one.background = ContextCompat.getDrawable(this, R.drawable.wrong_option_border_bg)
respuestaFallada()
}
}
tv_option_two.setOnClickListener {
if (tv_option_two.text == respuesta) {
Toast.makeText(this, "RESPUESTA CORRECTA", Toast.LENGTH_SHORT).show()
tv_option_two.background = ContextCompat.getDrawable(this, R.drawable.correct_option_border_bg)
respuestaCorrecta()
} else {
tv_option_two.background = ContextCompat.getDrawable(this, R.drawable.wrong_option_border_bg)
respuestaFallada()
}
}
tv_option_three.setOnClickListener {
if (tv_option_three.text == respuesta) {
Toast.makeText(this, "RESPUESTA CORRECTA", Toast.LENGTH_SHORT).show()
tv_option_three.background = ContextCompat.getDrawable(this, R.drawable.correct_option_border_bg)
contadorPregunta++
indicePreguntas[contadorPregunta]
} else {
tv_option_three.background = ContextCompat.getDrawable(this, R.drawable.wrong_option_border_bg)
respuestaFallada()
}
}
tv_option_four.setOnClickListener {
if (tv_option_four.text == respuesta) {
Toast.makeText(this, "RESPUESTA CORRECTA", Toast.LENGTH_SHORT).show()
tv_option_four.background = ContextCompat.getDrawable(this, R.drawable.correct_option_border_bg)
} else {
tv_option_four.background = ContextCompat.getDrawable(this, R.drawable.wrong_option_border_bg)
respuestaFallada()
}
}
}
}
private fun defaultColors() {
tv_option_one.background = ContextCompat.getDrawable(this, R.drawable.default_option_border_bg)
tv_option_two.background = ContextCompat.getDrawable(this, R.drawable.default_option_border_bg)
tv_option_three.background = ContextCompat.getDrawable(this, R.drawable.default_option_border_bg)
tv_option_four.background = ContextCompat.getDrawable(this, R.drawable.default_option_border_bg)
ivAcierto.visibility = View.INVISIBLE
}
private fun respuestaCorrecta() {
// Ver animacion
ivAcierto.visibility = View.VISIBLE
ivAcierto.animate().apply {
duration=1000
rotationYBy(1400f)
}.start()
contadorPregunta++
indicePreguntas[contadorPregunta]
Thread.sleep(2_000)
bindViews()
}
private fun respuestaFallada() {
//Thread.sleep(1_000)
// FALLO EN EL TEST
val dialog = AlertDialog.Builder(this)
.setTitle(R.string.dialog_one_style_title)
.setMessage(R.string.dialog_one_style_message)
.setPositiveButton(R.string.dialog_one_style_positive_btn) { view, _ ->
view.dismiss()
val intent = Intent(this, ResultActivity::class.java)
val json = Gson().toJson(quizzes!![0])
intent.putExtra("QUIZ", json)
startActivity(intent)
}
.setCancelable(false)
.create()
dialog.show()
}
}
Solution 1:[1]
I would like to give a couple of notes here: please use English when you write code, cause I have no idea what "pregunta" is and don't do this !! ... find other way to deal with nullables.
In respuestaCorrecta you call bindViews, which calls defaultColors and your correct color is getting overrided. As I can see, you want to delay calling bindViews by adding 2 sec sleep...
This is a bad idea. See your code runs in a main thread, in the same thread where rendering of Android Views happens... So making it sleep for 2 sec will make sure that no view will render for this period of time, and a user will not see anything in the app - no animation, no color changes, etc.
Thread.sleep cannot be used within main thread. However, if you want something to change after 2 sec, you can post an update. Like this:
tv_option_one.postDelayed({
bindViews()
}, 2000)
lambda is what will be called after a delay
2000 is a delay in millis
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
