title: Android-App in Kotlin: Updating imageView only updates last option
body: <p>I'm trying to &quot;animate&quot; a dice rolling in the Dice-Roller App from the Kotlin Learning path from Google. I try to set an ImageView with an drawable, &quot;sleep&quot; a little bit and update it again, this a few times till it stops at the random generated number.</p>
  <p>When i use (Option 1):</p>
  <pre><code>rollButton.setOnClickListener {
              rollButton.isClickable = false
              rollDice()
              Thread.sleep(1000)
              rollDice()
              Thread.sleep(1000)
              rollDice()
              rollButton.isClickable = true
          }
  </code></pre>
  <p>it just sleeps and dont updates 3 times. The image just updates once after 2 seconds.</p>
  <p>Same when i try to handle it inside the <code>rollDice()</code> with (Option 2):</p>
  <pre><code>        // Play Roll - Animation
          diceImage.setImageResource(R.drawable.dice_1)
          Thread.sleep(1000)
          diceImage.setImageResource(R.drawable.dice_2)
          Thread.sleep(1000)
  
          // Update the ImageView with the correct drawable resource ID
          diceImage.setImageResource(drawableResource)
  </code></pre>
  <p>App: ( <a href="https://developer.android.com/codelabs/basic-android-kotlin-training-dice-roller-images?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-four%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-dice-roller-images#6" rel="nofollow noreferrer">https://developer.android.com/codelabs/basic-android-kotlin-training-dice-roller-images?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-four%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-dice-roller-images#6</a> )</p>
  <p>Code:</p>
  <pre><code>class MainActivity : AppCompatActivity() {
  
      /**
       * This method is called when the Activity is created.
       */
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          // Find the Button in the layout
          val rollButton: Button = findViewById(R.id.button)
  
          // Set a click listener on the button to roll the dice when the user taps the button
          rollButton.setOnClickListener {
              // HERE WOULD BE OPTION 1  -------------------!!!!!!
              rollButton.isClickable = false
              rollDice()
              rollButton.isClickable = true
          }
  
          // Do a dice roll when the app starts
          rollDice()
      }
  
      /**
       * Roll the dice and update the screen with the result.
       */
      private fun rollDice() {
          // Create new Dice object with 6 sides and roll it
          val dice = Dice(6)
          val diceRoll = dice.roll()
  
          // Find the ImageView in the layout
          val diceImage: ImageView = findViewById(R.id.imageView)
  
          // Determine which drawable resource ID to use based on the dice roll
          val drawableResource = when (diceRoll) {
              1 -&gt; R.drawable.dice_1
              2 -&gt; R.drawable.dice_2
              3 -&gt; R.drawable.dice_3
              4 -&gt; R.drawable.dice_4
              5 -&gt; R.drawable.dice_5
              else -&gt; R.drawable.dice_6
          }
  
          // Play Roll - Animation
          // HERE IS OPTION 2 -------------------!!!!!!
          diceImage.setImageResource(R.drawable.dice_1)
          Thread.sleep(1000)
          diceImage.setImageResource(R.drawable.dice_2)
          Thread.sleep(1000)
  
          // Update the ImageView with the correct drawable resource ID
          diceImage.setImageResource(drawableResource)
  
          // Update the content description
          diceImage.contentDescription = diceRoll.toString()
      }
  }
  
  /**
   * Dice with a fixed number of sides.
   */
  class Dice(private val numSides: Int) {
  
      /**
       * Do a random dice roll and return the result.
       */
      fun roll(): Int {
          return (1..numSides).random()
      }
  }
  </code></pre>
  <p>I tried now for 3 days to find an answer, any ideas?</p>