Create Turn Summary Text

Kotlin and Android Development featuring Jetpack — by Michael Fazio (35 / 125)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Update the UI | TOC | Handle AI Turns with Coroutines and First-Cla ss Functions 👉

We added the large <TextView> near the bottom of the Game screen in the previous chapter to give users info about the current turn, how a turn ends, and a summary of scores when a game ends. Right now, when a turn starts, that area says nothing. And once a turn ends, that area then says…nothing. Yes, that value’s still empty from the previous chapter and is, well, useless. Let’s fix that!

We already have this block of code inside updateFromGameHandler:

​ ​//result is a TurnResult object​
​ currentTurnText.value = generateTurnText(result)

As a result, we have two main additions:

  • A clearText variable, which tells us if we should clear the <TextView>.
  • A new generateTurnText method.

Step one should be done about…now (seriously, go ahead and quickly add the variable under the currentStandingsText declaration). Step two, however, gets a bit more complex. We’re going to be displaying different messages based on what’s happening with a given turn.

Let’s address everything in the function except the new text, then deal with the messages. The function has three primary pieces: clear out the existing text if the previous event was the end of a turn, flag if the…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.