Your First Kotlin Android App: An App From Scratch
Jul 5 2022 Kotlin 1.6, Android 12, Android Studio Bumblebee | 2021.1.1
Part 3: Coding in Kotlin
20. Understand Kotlin's Standard Library
Kotlin comes with many built-in data types and functions 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 in your own apps. In fact, you’ve been using many elements from the kotlin standard library already.
The data types
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 that reinventing the wheel. But its a big library so how can you know what’s available?
Luckily, the kotlin documentation available online 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 variable by adding the following line of code below the
private var targetValue = 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 the Random class import and other import statements used by this file. The Log class was also imported for us. 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 variable named
nextInt() function 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 text of the target textView to the value of this variable. You know the routine, right?
First get the textView in the activity with viewBinding through the generated binding class. Then you set the text property to the value you want. Remember you did this in an earlier episode.
Let’s go ahead and do that now.
Enter the following code inside the
onCreate method of the
binding.targetTextView.text = targetValue
In here, you set the text property or attribute if you like to call it that to the
targetValue variable which is going
to be a random value in the range from 1 to 100.
This makes sense, right?
But why do we have an error?
Go ahead and hover over the targetValue variable we passed.
You can see the error says there’s a “Type Mismatch.”
It required a Character sequence but instead we passed in an Integer value.
If you recall from an earlier episode about strings, we stated that a string is a sequence of characters. So a string is expected here. But it makes sense for the target value to be an integer because we’re generating a number in the range from 1 to 100.
The reason the textView requires a string is because the text property in textViews are represented as strings in the UI. That’s why if you look at the xml layout file, you’ll see the text attribute is surrounded with double quotes which represent a string.
So how do we pass in string here when the
targetValue must be a number?
Well, if you look below the error popup, you can see a suggestion that says: “Add toString() call.”
Go ahead and click on it.
This adds a
.toString() method to the targetValue property.
toString function 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 functions just like
toString() which can be used by classes
that inherits from it like the
Int class has this function which returns the string representation of the integer object, in this case the
In simpler terms, the
toString() function here just wraps the
targetValue in double quotes to make it a string so the textView can accept that value.
You’ll be using the
toString() function 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 activity multiple times to see different values in the range of 1 to 100 generated as the target value.