Jovche Mitrejchevski’s address: MRw.so /5bWdIl

What is a custom activity template? (note: In Android Studio, there is a concept that you can insert any chunk of text content as a template wherever you want, as long as you type in a keyword. This is an activity template. By using the activity template, we can plug some of the commonly used constructs into our code.

How can Live Templates help us?

The activity template is very convenient and can speed up our code writing. Over time, we wrote the same or similar template code for loops, conditional controls, declarations, and even the entire class template (recyclerView. Adapter, recyclerView. ViewHolder, etc.). Some people use activity templates extensively for live coding demonstrations. It saves a lot of time and eliminates many possible errors.

How to create an activity template

To configure the active Templates, we need to open the Live Templates Settings page in Android Studio: Settings -> Editor -> Live Templates. On the Live Templates page, you can see all available active Templates, edit them, and create new active Templates.

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

Go ahead and create a new group and name it Test, which will have the activity template for writing tests, so the name is appropriate.

Next, we select the newly created group Test and click the + button on the right to create a new activity template for this group.

After selecting this option, we can see the active template editor at the bottom of the window

Here, we must first set the abbreviation(abbreviation), which is like a keyword that will trigger the template’s insertion into the editor. We can also give it a concise and appropriate description. What does description do? For example, if we have similar abbreviations for different templates, the description can be very helpful for selecting the right 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 on the Define button to Define a context

As you can see, as shown in the figure above, we have selected Kotlin Class as the context for the template, which means that the template is available in the Kotlin Class file.

Next, let’s go ahead and set the template we actually want for the given abbreviation (that is, test). Apply the following code to the editor’s “Template text” input box:

@org.junit.jupiter.api.Test
fun $EXPR$(a) {
 org.junit.jupiter.api.Assertions.assertEquals($EXPR1$, $EXPR2$)
}
Copy the code

There are other Settings on the right side of the active template editor, but we’ll ignore them for now. Finally, the editor looks like this:

Now, all we have to do is save and finish. Here’s an explanation of the actual template code we apply: use fully qualified names for the Test class and assertEquals () methods:

 org.junit.jupiter.api.Test
 org.junit.jupiter.api.Assertions.assertEquals
Copy the code

When we use this template in the editor, Android Studio automatically imports and completes the code.

To see what it looks like, open an arbitrary Kotlin Class file (since we set the context to 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 select the available templates, because I’ve pre-configured an active template for the JUnit4 test feature, so I can see two available options and also choose which ones I want to use.

Simply press ⏎ (Enter) or ⇥ (Tab), and Android Studio inserts the selected template into the editor and places the cursor on the first $EXPR $variable (in this case, the function name) of the template.

Be sure to note that when the cursor types over a particular $EXPR $variable, the typed text appears with the same name in all variables. In our example, we have a total of three variables, each of which is distinguished by a number: $EXPR $, $EXPR1 $, and $EXPR2 $. In fact, numbers are not used as the order of the next/previous jump, but just to distinguish them.

Share some activity templates

Recently, I found an open source project called AndroidLiveTemplates: github.com/pranaypatel… If you have a good campaign template, the library is also welcome to submit PR.

That’s all for this article, I hope it’s useful to you!

If you like my article, please follow my public account Android technology grocery store, Jian Shu or Github! Wechat official account: Android technology Grocery Store

Jane: www.jianshu.com/u/35167a70a…

GitHub:github.com/pinguo-zhou…