In a previous tip , you learn how to create your own extension functions that you add to existing classes. An entire library of these functions is available, known as Android KTX. You can add the library to your project with a single dependency, and then have access to a wide variety of code-saving shortcuts.
- [Instructor] In last week's tip, I described how to create your own extension functions that you add to existing classes. An entire library of these functions is now available, known as Android KTX. You can add the library to your project with a single dependency. I'll go to my Gradle Script for my app module and down here at the bottom of the dependencies, I'll add a new implementation line and I'll use this URI, androidx.core: core-ktx, and then after another colon, the version number.
As of the time of this recording, Android KTX was in it's original preview release. But, if there's a more recent version, Android Studio should let you know about it. Re-sync Gradle and now the Android KTX library is added to your package. Here are a couple of things you can do with it. I'll go to my main activity. Let's say for example that you have a string that you want to turn into a URI object. In Java that doesn't take that much code. You call a parse method and you can do the same thing in Kotlin, but with Android KTX, a new function called to URI has been added to the string class.
I'll create an object and this will be a string. And, I'll start with http:// and then this could be any well formed URL. I'll say android.com/action/edit. Now, I want to turn that into a URI object. I'll create another variable that I'll call URI and I'll call url.toUri and notice that this says that this is for the string class and it's part of Androidx.net and that's the package that's declared in the Android KTX library.
Now, I'll output that to the screen and I'll say URI set and then I'll output the URI variable. I don't need to explicitly call the to string function in this context. Now, when I click the run code button the first time, my log goes away and that's because I'm still calling my flip visibility function that I created last week. But, when I click the button again, I see that the URL has been turned into a URI and then backed to a string. I'll come back to Android Studio and I'll comment out this line of code.
Since it will be getting in my way. Here's another example. Shared preferences in Android take quite a few lines of code to edit. First, you have to get the reference to a preferences set. Then, you have to edit it. Then, you make your changes. And then, you apply the changes. Using Android KTX, that's all shortened to a single lambda expression call. First, I'll get a reference to a preferences object. I'll call it preferences. And, I'll call the getPreferences function of the activity and I'll pass in a mode of Context.MODE_PRIVATE.
Now, I'm going to add a preference with this single statement. I'll say preferences.edit and notice that there's a new action available here that's just a pair of braces. It doesn't look like a function call. And, I'll be passing in a lambda expression saying this is the code I want you to run. I'll call the putString function which requires two parameters, a key and a value. For the key, I'll pass in FIRST_NAME and then for the value, I'll pass in my first name.
I don't need to call the apply function. That's handled for me by the Android KTX library. Now, I'll retrieve that value. I'll create another variable that I'll call first name and I'll use preferences.getString and I'll pass in the first name key. And then, the default will be an empty string. And then, I'll output that value as part of a larger string. And, when I run the code, I see that I've successfully retrieved the value from the preferences.
So, this is the result of the combination of Kotlin, the language itself, and of the contributions that developers are making to add functionality to the language and to the Android core library. You can see the code and add your feedback and your own contributions at this GitHub repository at github.com/android/android-ktx.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.