Leave a rating/review
Notes: 21. Understand Kotlin's Standard Library
Kotlin Standard Library documentation
You can replace your code with the one below to generated a new target value based on the current system time. More info in the link here: Random number generating same value on rebuild - Stack Overflow
val randomGenerator = Random(System.currentTimeMillis())
var targetValue by rememberSaveable { mutableStateOf(randomGenerator.nextInt(1, 100)) }
Kotlin comes with many built-in data types and functions that you can use in your own apps to complete a variety of common programming tasks. This is known as the Kotlin Standard Library.
You can think of the Kotlin Standard Library as a big box of books filled with code that you can use while building your apps. In fact, you’ve been using many elements from the Kotlin standard library already.
The data types Int and String are all part the standard library.
The part of the library that includes these fundamental types and funcitons is included in your Kotlin project automatically.
When you’re developing your Kotlin based apps, you’ll frequently want to use utilities from the Kotlin standard library rather than reinventing the wheel. But its a big library, so how can you know what’s available?
Luckily, the online Kotlin documentation is an amazing resource for getting to know about what’s available and how to use it.
The documentation has a section dedicated to the Kotlin standard library and the packages contained in it. The documentation is definately worth exploring as you follow along the learning path.
Now back in your app you’ll be changing the target value to use a randomly generated number. For this you’ll create a new variable to store the target value and the value of this variable will be randomly generated.
Create the state object variable by adding the following line of code below the sliderValue variable:
var targetValue by rememberSaveable { mutableStateOf(Random.nextInt(1, 100)) }
While typing this, an import statement was added.
To see the import statement that was just added, scroll to the top of the file.
Then click on the plus button beside the import text.
And you can see the Random class import and other import statements used by this file.
The Log class was previously imported for us too.
Android studio helps to import all these classes automatically.
Breaking code into files and packages that we can import is very important in programming. Without the concept of import statements, all the code contained in those files would have to be inside this file. And this would result in a very long file and would prevent us from using these code in multiple places in our project or in external projects.
Okay, I’ll close that up.
The code we just entered creates a state object named targetValue.
The nextInt() method of the Random class generates a number in the range from 1 to 100.
Next, to use this value inside your app’s UI, you need to set the target text widget to the value of this variable. You know the routine, right?
GamePrompt is a custom composable so we need to pass in the target value to where it is called inside the GameScreen function.
So pass in the targetValue argument like so:
GamePrompt(targetValue = targetValue)
And Android Studio complains because the GamePrompt function doesn’t have a targetValue parameter in its definition.
So let’s add that now.
Hold down Cmd and click on GamePrompt to naviagate to it.
Then update the function definition to the following:
@Composable
fun GamePrompt(targetValue: Int, modifier: Modifier = Modifier) {
//...
}
After that, set the target text to use this parameter as its text like so:
Text(
"$targetValue", // New Code
fontSize = 32.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(8.dp)
)
We use a string template here because the target value is an Integer and the Text composable only accepts a string as its text.
So using string templates helps us interpolate that value to be displayed in the string and take note of the dollar sign.
If you dont use string templates here, you’ll get a type mismatch error on the Text composable function.
And do note that you can also achieve this conversion using the toString() method we used in the previous part.
Just comment the current targetValue line using Cmd + / and add it in like so:
Text(
// "$targetValue",
targetValue.toString(),
//...
)
The toString() method is available to all classes in kotlin.
This is simply because all classes in Kotlin have a parent class called Any.
And the Any class has a bunch of methods just like toString() which can be used by classes that inherits from it like the Int class.
So the Int class has this method which returns the string representation of the integer object, in this case the targetValue.
In simpler terms, the toString() method here just wraps the targetValue in double quotes to make it a string so the text widget can accept that value.
You’ll be using the toString() method as you code more and more in kotlin.
Okay!!! Let’s try it out. Run your app.
You can see a new target value is set. Restart the app multiple times to see different values in the range of 1 to 100 generated as the target value.
And just in case you dont get a different value on each rebuild, make sure you’re running your app on a device or emulator running Android 13 and up.
There was a bug in an older version of Kotlin with the Random class and how it generated values for Android.
This affects older Android versions.
I’ve provided a link and code snippet in the author notes for a work-around for older Android versions.