Original Author: Jovche Mitrejchevski
Link: http://mrw.so/5bWdIl
Translator: Yiran Fantaxi
What are Activity Templates? (Note: Many articles translate this as Live Templates, but I think Activity Templates is more appropriate; hereinafter referred to as Activity Templates) In Android Studio, there is a concept where you can insert any block of text as a template wherever you need it by just needing to input a keyword. This is Activity Templates, and by using Activity Templates, we can insert commonly used constructs into the code.
How do Live Templates help us?
Activity Templates are very convenient and can speed up our coding. Over time, we write a lot of the same or similar template code in projects, such as loops, conditional controls, declarations, and even entire class templates (like RecyclerView.Adapter
, RecyclerView.ViewHolder
, etc.). Some people widely use Activity Templates during live coding demonstrations. It saves a lot of time and eliminates many possible errors.
How to Create a New Activity Template
To configure Activity Templates, we need to open the Live Templates
settings page in Android Studio: Settings
-> Editor
-> Live Templates
. On the Live Templates page, we can see all available Activity Templates, edit them, or create new Activity Templates.

To define a new template, click the +
button on the right side of the window. Here are two options: Live Template
and Template group
.

Continue to create a new group and name it test
. This group will have Activity Templates for writing tests, so the name is very appropriate.
Next, we select the newly created group test
and then click the +
button on the right to create a new Activity Template in this group.

After selecting this option, at the bottom of the window, we can see the Activity Template editor.

Here, we must first set the abbreviation
, which is similar to a keyword that will trigger the template insertion in the editor. We can also set a concise and appropriate description for it. What is the purpose of the description? For example, if we have similar abbreviations for different templates, the description is very helpful in choosing the correct template when using them later in the code. In this example, we use test
as the abbreviation and JUnit test function
as the description.
Next, we will define a context in which the new template will be available. At the bottom of the template editor window, there is a yellow warning ⚠️ No applicable context
, meaning there is no available context.

We click the Define
button to define a context.

As shown in the image above, we select Kotlin Class
as the template context, which means this template will be available in Kotlin class files.
Next, let’s continue to operate and set the actual template we want for the given abbreviation (which is test
). In the editor’s Template text
input box, apply the following code:
@org.junit.jupiter.api.Test
fun $EXPR$() {
org.junit.jupiter.api.Assertions.assertEquals($EXPR1$, $EXPR2$)
}
There are other setting options on the right side of the Activity Template editor, but we will ignore them for now. Finally, the appearance of the editor is as follows:

Next, what we need to do is save and complete. Here, let’s explain the actual template code we applied: Use fully qualified names for Test
class and assertEquals()
method:
org.junit.jupiter.api.Test
org.junit.jupiter.api.Assertions.assertEquals
When we use this template in the editor, Android Studio will automatically import and complete the code.
Let’s see what it looks like. Open any Kotlin
class file (because we set the context to be a Kotlin class
file), and in the class body, type the corresponding template abbreviation: test

As we can see, Android Studio pops up a small window where we can choose from the available templates. Since I have already set up an Activity Template for JUnit4
test functions, I can see two available options and choose the one I want to use.
Just press ⏎ (Enter) or ⇥ (Tab), and Android Studio will insert the selected template into the editor and place the cursor at the first $ EXPR $
variable (in this case, the function name).

Be sure to note that when typing content on a specific $ EXPR $
variable, the typed text will appear with the same name in all variables. In our example, we have a total of three variables, each with a number added after it to distinguish: $ EXPR $
, $ EXPR1 $
, and $ EXPR2 $
. In fact, the numbers are not used as the next/previous jump order, but just to distinguish them.
Sharing Some Activity Templates
Recently, I found an open-source project called AndroidLiveTemplates
: https://github.com/pranaypatel512/AndroidLiveTemplates, which provides a bunch of cool and practical Activity Templates. If you have good Activity Templates, the library also welcomes PR submissions.
That’s all for the article. I hope it is useful to you!
—END—
Recommended Reading:
Custom Sliding Scale Progress Bar in Android
How to Get the Height of WebView Content in Android
Custom EditText Cursor and Underline Color in Android
