Android Studio Code Template Setup

Android Studio / IntelliJ IDE Code Generator Template Setup

This tutorial is for setting up code template on Android Studio or IntelliJ based IDE environment. Using this setup we can quickly generate code which has already given in the live template.

Here we have shown how to quickly generate log method without coding.

Android Studio project workspace

This is the basic project setup to show how to generate template code. Below is the landing page of the project workspace.

Setup Live Template

Open Settings window by navigating File -> Settings

In the Settings Window navigate to Live Template through Editor -> Live Template or even you can search template on the search bar to view the Live Template option

In the Live Template screen click on add icon (+) and select Live Template option.

After selecting Live Template Option below form will be visible.

Update Abbrevation as “log” and give any descriptions. Abbrevation text is used to autocomplete code template on Ctrl+Space.

Here for example we have given below sample method to generate on typing log on the editor.

Properties should be given in between two dollar symbols $$. Here we need to access Package and ClassName of the class in which code is being generated. Once the code generated it will automatically pick package name and ClassName of the current class.

We also need to provide the scope of this code template. To provide the scope click on option “Define” which is shown highlighted below

Clicking on Define link will popup Tree options were we need to select “Java” as the scope of this code template to be accessed.

To make properties work map the property name against the Expressions. To do so click on “Edit Variables” option highlighted below.

This  will popup Edit Template Variable window from were we can map the property against the expressions.

Ok we are good to go now. click on Ok to Finish the setup and come back to the editor workspace.

Type log and do Ctrl+Space will pop up code template we have setup just now. Clicking on the log will generate the template code on the workspace.

Below is the final screen which shows generated code log() with predefined template and dynamic properties.