A solution to Challenge: Limited Cheats while using Toasts instead of a TextView

import android.widget.Toast.*

 // MainActivity.kt in OnCreate
     var tokens = 3
                 cheatButton.setOnClickListener {

                if (tokens <=3 && tokens!=0) {  // Can use also (!(tokens > 3 || tokens == 0))
                    tokens -= 1
                    makeText(this, "Cheats remaining $tokens", LENGTH_SHORT).show()
                    Log.d( TAG, "TOKENS $tokens")
                val answerIsTrue = quizViewModel.currentQuestionAnswer
                val intent = CheatActivity.newIntent(this@MainActivity, answerIsTrue)
                startActivityForResult(intent, REQUEST_CODE_CHEAT)}

                else { cheatButton.isEnabled = false
                     makeText(this, "Oh well, you are out of cheats", LENGTH_SHORT).show() }
            }
1 Like

One suggestion for improvement would be to use a string resource for accessibility instead of hardcoding in the string text.

1 Like

String text is not working.

You need to make sure you resolve the string ID from the resources:

resources.getString( R.string.myStr )
1 Like

Also be sure to check all the names and values.

I suppose you need to handle configuration changes also … to prevent cheating the Cheat button :wink: