Your First Kotlin Android App - Part 25: Handling | Ray Wenderlich

If you want to react to events, you need to write some actions. This video will show you how.

This is a companion discussion topic for the original entry at

With Android Studio version 3.3.1, we can populate the onClick property in the Attributes navigator with a method name that is to be executed when the button is tapped. Would this not be preferable to adding a listener in the onCreate method in the Activity?

@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]

concerning method “incrementScore()”
why do I have to store the new score into a variable “newScore”?
I can simply write it in the UI directly, like this:
gameScoreTextView.text = getString(R.string.your_score, score.toString())
It is working fine and I will save one line of code and a variable …

Thanks for the comment @gundrabur. It’s always good to write less code for sure! But for instruction in a course like this, it’s also good to add some code verbosity to add clarity for people with less development experience. Experienced developers will tend to optimize this choice.

Thanks for the question @marathoner1234. In many ways this is often up to the preference of the developer. In certain cases, it’s more efficient to add the listeners in XML, in other cases, in Kotlin.

Thanks for the information. I am new to Kotlin and thought I had misunderstood something. By the way: Great workshop. I love it!

Hi, on line 29 of the tutorial, Android Studio is throwing me the error “Format string ‘your_score’ is not a valid format string so it should not be passed to String.format”

Upon compiling, I click the button on the emulator and my app crashes.

Any thoughts? my code looks identical to the tutorial.

This course hasn’t been updated yet. It’s in the works, but you want to update the line to look like the following:

val newScore = getString(R.string.your_score, Integer.toString(score))

Notice that I’m calling Integer.toString only this time, I’m passing in the score. You’ll do this in other areas as well. As always, if you get stuck, feel free to reach out.



I’m getting two errors while trying to run the app

Can you help in this?

lateinit is used when you don’t have the reference of a variable when the object is created. These are for variables that are not supposed to be null. By declaring a variable as a lateinit, you are telling the compiler that when the object is created, the value will be null, but that null will be gone will the object is actually used.

You are using score as an int. This contains a value. It’s not like an object that requires a reference. When using primitive types (int, double, float, bool), you provide a default value. To solve this error, you just remove the lateinit keyword because it’s not needed. I hope that helps.