Live Template: Boost your Code Efficiency in Android Studio

Fuad Reza
4 min readAug 17, 2023

--

Photo by Mimi Thian on Unsplash

Have you ever write a same code for over and over again that takes so much time. Like writing models or entity class for your project, or views. Let me introduce you to some magic called “Live Template”.

What is “Live Template” in Android Studio?

Live Template is an shortcut for developer to make a specific pattern or template of code by simply typing a abbreviation inside your file/class. It’s there for you as a developer to save time while doing something same over and over again. It makes you code faster and help you do something that maybe you don’t remember.

How to View all Live Template?

By going to setting inside of Android Studio > Setting > Editor > Live Template, you can see so many built-in template you can use inside you project.

Live Template Setting in Android Studio
Live Template Setting in Android Studio

Select and expand programming language or framework you use (here I select flutter). Then select already created template to see what is it for and what the template looks like. Example: stless template for flutter to create Stateless widget.

stless template for Flutter
stless template for Flutter

How to Create Live Template?

Click Plus button on the right side and choose Live Template.

Adding Live Template
Adding Live Template

Then, fill the abbreviation name for that template. It is a keyword for you to type to show the template inside of your file/class. Also put your description of the template you want to create.

Abbreviation and description field
Abbreviation and description field

After that you type the template in the Template text field. Type a variables name using dollar symbol “$” in front of the name and at the end of the name. The purpose of this is to become a pointer of your keyboard when using the template.

Template text with variables
Template text with variables

You can also edit the variables to define the default value or give an expression. But for now we leave it default.

Edit template variables window
Edit template variables window

Then define the context of the template by clicking the Define button, so the IDE knows where you can use that template. Here you select the programming language you use for the template. I use dart for this template.

Define context selection
Define context selection

That’s it, you can save that template and begin to use it in your project by clicking Ok.

How to use Live Template?

Go to your file/class. Type the abbreviation name you created before and then tap Enter if it showing.

Typing the abbreviation inside file/class
Typing the abbreviation inside file/class

Now your keyboard pointer will move to the place where you create the variables of your template. Simply type as you want and then move the keyboard pointer by tapping Tab.

Typing the live template
Typing the live template

Voila! You just unlock your new power to make you code faster and efficiently. Hopefully with this technique you can save some time from doing the same thing and allocate that time for something more important like solving error or thinking of a solution.

Here’s example of my live template I use for my Flutter project to create a model or entity class:

My Live Template of creating model/entity class for flutter
My Live Template of creating model/entity class for flutter

--

--

Fuad Reza
0 Followers

Flutter Developer | Mobile App Developer