Extension function with Kotlin | No BS Tutorials
Welcome to No BS Tutorials. Here you will get “To the Point” tutorials with no BS involved.
A detailed tutorial with thourough explanation will be found on my blog.
Introduction :
As the name suggests Kotlin extension functions extend abilities of your classes by creating your own user-defined functions.
Getting Started:
- We will create 3 different extension functions here to understand the concept
- Create an empty file anywhere in your package (don’t make it class/interface or anything)
- First: create a simple toast (& not writing a long line for such a simple thing)
Now simply you can call it inside your activity as
// activity has context hence we call it directly
toast("")// if wish a long text then
toast("", Toast.LENGTH_LONG)
- Second: Hide or show your view in code
Again simply call that as
// we will use the view-name followed by the functionanyView.gone()
anyView.show()
- Third: open a website in the browser (send intent)
to open website from activity call
startWeb("https://google.com")
Conclusion
Extension functions in kotlin make your life so so easy by not repeating common tasks again and again and include them right along with the respective class itself.
Examples
I have listed some of the extension functions that make my life easier (& I will update them from time-to-time.
Wrap Up
Once again if you wish to know in more detail about this entire tutorial visit my blog.
Thanks a lot for tuning in today, for more tutorials follow this publication or subscribe to my blog.