Live Templates in Android Studio

Mohamed Ali BEN MANSOUR
4 min readApr 15, 2024

--

Introduction

Code editors provide us several tools to save time and improve our productivity.

In this article we will explore Android Studio Live Templates.

Live Templates are shortcuts that allow generating a pieces of code on writing just abbreviations.
They are very useful for pieces of code that we write recurrently in our programs.

Pre-built live templates

Android Studio has a lot of pre-built Live Templates, to see the whole pre-built list, go to File > Settings > Editor > Live Templates.

Android Studio Pre-Built Live Templates
Android Studio Pre-Built Live Templates

Live Templates are grouped by categories to facilitate access, adding, editing …

Let’s see an example !

Into your Android Studio, inside a Kotlin file and outside of comments, write “comp” and see that a popin appears and shows the “comp” Live Template with it’s description.

“comp” Live Template
“comp” Live Template

Now tape “Enter” and you see that a code for new Composable function is generated.

Generated new Composable function
Generated new @Composable function

Create custom live templates

Now, let’s create some custom Live Templates.

Hilt ViewModel

Suppose that we have an MVVM architecture and we use Hilt for dependency injection. We will have several boilerplate code to define the ViewModel classes.

Solution : Create a Live Template to do that !

Go to File > Settings > Editor > Live Templates.
Select AndroidKotlin, click “+” button and choose “Live Template”.

Add new custom Live Template
Add new custom Live Template

Now, we can define our custom Live Template as below:

“hiltvm” Live Template
“hiltvm” Live Template

We see four sections, let’s explore them one by one:

  1. Abbreviation : The name of our custom Live Template. This is what you will write into the editor to access this Live Template.
  2. Description : A small description to explain the purpose of the Live Template. This will be used as a hint when you write the Live Template into the editor.
  3. Template Text : The is the content of our Live Template.
  4. Context : Define where this Live Template will be applicable (Kotlin, XML, C, Groovy, …)
Context of Live Template
Context of Live Template

For our examples, we will select “Kotlin” as target and all it’s subsections except “Comment”.

Apply and that’s set !

Let’s use this custom Live Template, for that write “hiltvm” into a Kotlin file (outside of comments).

We can see the name and the description of our Live Template.

Accessing “hiltvm” Live Template
Accessing “hiltvm” Live Template

Tape “Enter”

Generated code for “hiltvm” Live Template
Generated code for “hiltvm” Live Template

And that’s it, we have a generated template for a new ViewModel class that will be injected by Hilt, great not ?

Below there are more examples that can be useful.

StateFlow in a ViewModel

“statevm” Live Template
“statevm” Live Template

try / catch

“trycatch” Live Template
“trycatch” Live Template

IconButton (Compose)

“iconbtn” Live Template
“iconbtn” Live Template

“modifier” parameter (Compose)

“mod” Live Template
“mod” Live Template

Advanced example (Compose screen template)

We can define a fully configurable Compose screen template. Suppose we have a ProfileScreen; typically, we need to have a ProfileViewModel, ProfileAction, and ProfileState.

So we define our Live template as below:

“composescreen” Live Template
“composescreen” Live Template

We define the template using variables. The advantage is that when Android Studio generates this template and we start typing the name of the screen in the root function (…ScreenRoot), all occurrences of the $NAME$variable change simultaneously wherever it is used.

Note that for Live Templates related to Compose, we have placed them in the “AndroidCompose” category. That makes sense, right !

Conclusion

Live Templates are a powerful tool that accelerate development and increase productivity. We can use the pre-built list and also create our custom ones.

That’s all about Live Templates, hope you find this article useful !

--

--