Android Studio Kotlin Live Templates
How to make your life as an Android developer easier with Live Templates

Live Templates are a powerful tool to facilitate the life of a developer. The feature is included in Android Studio and is available for Kotlin since version 4.0.0.
Summarized, Live Templates provide a shortcut to easy access often used code snippets with the option for customization on demand. If you didn’t know of this feature before and you are an Android Developer I can almost guarantee you that you unknowingly already used it.
Code snippets defined in form of Live templates can be included in the auto code completion (IntelliSense). Out of the box, there are already plenty of them provided for you within Android Studio 4.0.0 and above.
Live Templates
To look up which Live Templates Android Studio already offers you for your Kotlin code, go to File → Settings → Editor → Live Templates → Kotlin. Besides the Kotlin tab, you can also take a look at the AndroidKotlin tab.

For example, you are tired of writing the same code when you just want to show a Toast message over and over again? Instead of writing the whole statement, just type in “toast” and hit “STRG + space” or “cmd+ space” when you are using a MAC.

As you can see the auto-completion offers you a Live Template for creating a new Toast . Hit enter and you will see the following:

Your cursor automatically jumps to the context input parameter field. You now just need to put in your Context and hit Enter. The text parameter as the next field will automatically gain focus and you just need to type in your desired message. Hit Enter once again and your whole statement is finished. That was quick, wasn’t it?
Like you could see with this example, Live for these templates means that you can dynamically put parameters into the template “live” while coding.
Creating your own Live Templates
Think a bit. What recurring code snippet you write over and over again?
As long as the code snippet is contained in only one file, you can create almost any code snippet you could think of.
The only limitation is that Live Templates cannot create files. If you have a use case where you want to create a whole file with the help of a template, you better take a look at File and Code Templates. We will briefly discuss this topic in a later section within this article.
No matter what kind of template, all of the mentioned use the Velocity Template Language (VTL). Even if it may look a little bit abstract at the beginning you will quickly get used to it.
Because we just had the example for creating a Toastvia pre-defined Live Template, I will now show you a practical example of how you can create your own Live Template for showing a simple Snackbar.

We now want to write the statement from the screenshot as a Live Template. First, open up Android Studio and go to File → Settings → Editor → Live Templates.
Because we want to add a Live Template for creating an Android component with Kotlin, select the AndroidKotlin tab. The tab has no direct influence on where the Live Template will be shown to you. It is just helpful to categorize your Live Templates. If you want to turn off a Live Template category, just uncheck it.
Now, with the selected AndroidKotlin tab, at the very right click on the + symbol → Live Template. A new screen will pop-up at the bottom of the Settings dialog and asks you for an abbreviation.

The abbreviation will later be the statement you can type in to trigger the auto-completion. Because we want to write a Live Template for creating a Snackbar template I will go for “snack” (without the quotes) as the abbreviation.
The description field is optional. It will be shown to you in the auto-completion besides the abbreviation. So if you ever forget what the respective abbreviation stands for, you can give yourself a hint.
Now we come to the main part of the Live Template. The writing of the VTL. Write the following VTL snippet into the Template text field:






