Skill Level Intermediate
- [Instructor] Kotlin allows you to define and add functions to existing classes. These are called extension functions and they're a great way to encapsulate boilerplate code that otherwise might have to be repeated many times. You can define extension functions anywhere, including in independent files. I'll go to my base package in this project and I'll create a new Kotlin file that I'll name Extensions, and I won't turn it into a class. Now, to declare a function, I'll use the keyword fun and then I'll start with the class that I'm adding the function to.
I'm going to add this to the view class from android.view. Now, I can name my function and I'll call it flipVisibility. The goal of this function is to examine the current visibility setting for a view that is a visual widget and then change it. So, if it's visible, I'll make it invisible, and if it's invisible, I'll make it visible. I'll use some simple conditional logic. I'll say if this.visibility equals the constant View.VISIBLE, then I want to make it invisible.
And, I'll do that by saying, this.visibility equals View.INVISIBLE. Then, I'll add an else clause and in the else clause, I'll say, this.visibility equals view.VISIBLE. And, that's all there is to it. You don't have to do anything else to register the code somewhere. That function is now available from all view objects. Now, I'll go to the main activity class. I already have code in place to get a reference to a view. In this case, text view component called mLog and I'll add a call to my new extension function here in the run code function.
I'll say mLog and I'll put in my double exclamation. That's my assertion that this object isn't null. And then, from there, I'll call flipVisibility and notice that Android Studio already knows that that function exists. I'll run that code and when I click the button, the log disappears, but when I click it again, it becomes visible. And, each time I click it, it disappears and then reappears and so on. So, this is a simple example of an extension function.
You can add as many extension functions as you need to. For good code organization, you might keep them all in a single file or you might organize them in a way that works better for your particular project.
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.
Android App Development: Communicating with the Userwith Joe Marini1h 12m Intermediate
Building Android Apps with Cloud Serviceswith Albert Lo2h 37m Intermediate
Android App Development: Data Persistence Librarieswith Annyce Davis4h 52m Intermediate
New This Week:
1. Android Development Tips Weekly
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.