Android Custom Alert Dialogs + Kotlin extension functions + Kotlin Higher Order functions = Life become Easier! — Kotlin DSL

Mohammed Audhil
AndroidPub
Published in
2 min readApr 17, 2018

Being an Android developer, tech enthusiast, I always like to explore/experiment/implement all new stuffs in my day to day professional life. I was thinking to write a re-usable Alert Dialog classes in Kotlin to be used across app, as a result, I found an elegant way, and sharing that with you all.

Prerequisites,

  1. You need to be aware of Kotlin Extension functions.
  2. Kotlin Higher Order functions.

Let’s get started,

Step 1,

An abstract base class, I won’t go into much detail, since code is self-explanatory.

BaseClass — Mother of all dialogs

Step 2,

after extending the above class,

NotesDialog — subclass
custom alert dialog — design

Step 3,

Extension functions, make sure to be able to call from both Activity & Fragment.

Extension Functions + Higher-Order functions

Step 4,

And call from “SomeFragment.kt”,

believe your eyes, it’s that simple.
the alert dialog

Sharing another example as bonus,

The helper class is left as an exercise to the reader

And, WTH is

showVLog(“ — — — — ”) ? 

Again, its another extension function.

kotlin really makes life easier.

That’s it, how else can we improve this from here? suggestions are invited.

Happy Coding!

--

--

Mohammed Audhil
AndroidPub

Software Engineer @Blackhawk_Networks | more: https://github.com/Audhil (Pray. Eat. Code. Sleep.)