Live Templates in Android Studio

Live Templates

Live templates helps you to write more code with less efforts. Live templates contain predefined code structure. What if you will get whole for loop with just one word? Seems interesting, let’s see how live template can help to increase your productivity.

Live templates are code snippet which can be inserted in your code with their abbreviation and pressing tab. You can find all pre-defined live templates in Editor section of your Android Studio.

Live Templates

 

Let’s understand it with simple example, you have used this line so many times in your code.

but what if we can get this whole line with just one keyword? just write Toast in your code, press tab and whole code will be available for you in your editor.

Live Templates
Live Templates

you don’t even need to write your class name also, now let’s take a look at Live Template syntax of Toast.

Live Templates

  • $className$ will return current class name.
  • “$text$” will be replaced by your given string.

Custom Live Templates

Step 1 : Go to File > Settings > Editor > Live Templates. Click the Android group, and press the plus button to add a new Live Template.

Step 2 : Write your abbreviation and descritption for Live Template.

Step 3 : Write below code in Template text area :

Step 4 : Click on Edit variable and you will see following screen, assign className() expression to className variable.

Live Templates

Step 5 : In bottom of your Template text you will find Define, just click on that and define coding language.

Live Templates

Step 6 : Click on Apply and you are done with your custom template, just use that abbreviation while you are coding and all the code snippets will we there in your code.

I know you all are having your own live templates and own coding styles, so feel free to share those templates in comment and help your fellow developers to increase their productivity.

Two-way DataBinding in Android
Advance DataBinding in Android

  • Arth Tilva

    How to get package name in XML for Live Template, I want to use my custom controls.

  • manohar Reddy

    Great thanks ,i didn’t knew about it till now.

    • Share it with other also so they can also know about it 🙂