From the course: Android Development Tips
Use support functions from Android KTX - Android Tutorial
From the course: Android Development Tips
Use support functions from Android KTX
- [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.
Contents
-
-
-
(Locked)
Communicate with Snackbar messages6m 15s
-
(Locked)
Manage view components with Butter Knife6m 41s
-
Create a reusable dialog class9m 22s
-
(Locked)
Convert a layout to ConstraintLayout7m 13s
-
(Locked)
Manage global data with singletons5m 37s
-
(Locked)
Convert image files to WEBP format6m 5s
-
(Locked)
Create an Espresso user interface test5m 18s
-
(Locked)
Create classes for Android with Kotlin5m 12s
-
(Locked)
Run background tasks in intent services8m 51s
-
(Locked)
Send broadcast messages with EventBus8m 44s
-
Handle events with Java 8 syntax5m 51s
-
(Locked)
Improve forms with design layouts7m 55s
-
(Locked)
Display draggable rows in a list of data8m 1s
-
(Locked)
Manage persistent data with Realm10m 23s
-
(Locked)
Play an audio file from assets8m 56s
-
(Locked)
Send an SMS message from an app7m 56s
-
(Locked)
Get a list of audio files on a device8m 32s
-
(Locked)
Play sounds with SoundPool8m 45s
-
(Locked)
Display the build date of an APK file6m 47s
-
(Locked)
Build Kotlin classes with properties7m 32s
-
(Locked)
Manage versioning with Gradle variables6m 8s
-
(Locked)
Use reporting tools from the Google Play Console4m 44s
-
Make web service calls with Retrofit10m 39s
-
(Locked)
Make asynchronous calls with Retrofit4m 6s
-
(Locked)
Send request parameters with Retrofit4m
-
(Locked)
Upgrade a project to Android Studio 3.04m 47s
-
(Locked)
Clean up calls to findViewById()2m 58s
-
(Locked)
Create adaptive launcher icons5m 47s
-
(Locked)
Use XML and downloadable fonts6m 40s
-
(Locked)
Explore a device's file system4m 55s
-
Profile an app's memory usage3m 52s
-
(Locked)
Manage an SQLite database with Room9m 17s
-
(Locked)
Manage background threads with Executors5m 32s
-
(Locked)
Track vital stats in a published app3m 4s
-
(Locked)
Inspect database files with DB Browser4m 41s
-
(Locked)
Execute tasks with JobScheduler7m 29s
-
(Locked)
Create Room entity classes in Kotlin8m 22s
-
(Locked)
Create Room DAO classes in Kotlin4m 13s
-
(Locked)
Upgrade to Android Studio 3.13m 22s
-
(Locked)
Convert a project to use Kotlin3m 20s
-
(Locked)
Handle events with Kotlin lambdas6m 13s
-
Manage menus with when3m 44s
-
(Locked)
Define global constants with Kotlin6m 12s
-
(Locked)
Use Kotlin data and enum classes5m 24s
-
(Locked)
Create inheritance hierarchies in Kotlin6m 8s
-
(Locked)
Manage data collections in Kotlin5m 45s
-
(Locked)
Sort data collections with Kotlin5m 33s
-
Filter data collections with Kotlin3m 52s
-
(Locked)
Create Kotlin extension functions2m 31s
-
Use support functions from Android KTX4m 30s
-
(Locked)
Use virtual devices with Google Play3m 50s
-
(Locked)