Display Messages with the Toast Class

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Debug Your App | TOC | Log Messages to Logcat 👉

Again, I feel compelled to clarify that Toast messages are intended to be small, brief alerts to users that something just happened. This may be some kind of processing or that they’ve been logged out of their account. With that being said, they can be handy for troubleshooting as well, similar to something like window.alert with web development.

In this section, we’re going to cover how Toast messages work plus a couple of String-related concepts: a new toString on a data class and the joinToString function on a Collection. We’re going to use that function to get our player info first, then we’ll see how Toast messages work.

Use joinToString() to Get NewPlayer Info

To get the info about the players, we’re going to take advantage of two great Kotlin features: null safety (null-safe calls + the null-coalescing operator) and the aforementioned joinToString function. One of the best things about Kotlin is that it requires you (at compile time) to handle all null scenarios in your code.

This helps avoid most situations where a NullPointerException could be thrown, crashing your app and leaving your users frustrated. In this function, we’re going to get the List<NewPlayer> from PickPlayersViewModel (by using 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.