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.
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.
How to Create Live Template?
Click Plus button on the right side and choose 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.
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.
You can also edit the variables to define the default value or give an expression. But for now we leave it default.
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.
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.
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.
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: