Extension function with Kotlin | No BS Tutorials

Welcome to No BS Tutorials. Here you will get “To the Point” tutorials with no BS involved.

Saksham Khurana
Devs Life
Published in
2 min readSep 28, 2019

--

Photo by David Hellmann on Unsplash (keyword: cable)

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)

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.

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.

--

--

Devs Life
Devs Life

Published in Devs Life

"Developers Life" focuses on providing developers with insights and tips for best coding practices as well as how to make most of their time & resources. Tutorials on various common problems also make a good part of this publication.

No responses yet