Live Templates in Android Studio
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.
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.
Now tape “Enter” and you see that a code for new Composable function is generated.
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”.
Now, we can define our custom Live Template as below:
We see four sections, let’s explore them one by one:
- Abbreviation : The name of our custom Live Template. This is what you will write into the editor to access this Live Template.
- 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.
- Template Text : The is the content of our Live Template.
- Context : Define where this Live Template will be applicable (Kotlin, XML, C, Groovy, …)
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.
Tape “Enter”
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
try / catch
IconButton (Compose)
“modifier” parameter (Compose)
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:
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 !