Live Templates in Android Studio: Getting Started

In this tutorial, you’ll learn how to use and create live templates in Android Studio so you can write code more quickly. By Victoria Gonda.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Reusing Variables

Because you’re following naming conventions, the name of the parameter is always part of the view ID. It would make sense, then, that you should only need to type this once. Because it’s a textual replacement, you can use the same variable in both places, including the text_ part of the ID in the template.

Open back up your shtext template to make that change:

override fun show$VIEW$($VIEWID$: String) {
  text_$VIEWID$.text = $VIEWID$
  text_$VIEWID$.visibility = View.VISIBLE
}

override fun hide$VIEW$() {
  text_$VIEWID$.visibility = View.GONE
}

This brings you back to having two variables to type in. You can do this because you’re always following the format of text_ followed by the name in your view IDs.

Save and use this template to add the year methods:

override fun showYear(year: String) {
  text_year.text = year
  text_year.visibility = View.VISIBLE
}

override fun hideYear() {
  text_year.visibility = View.GONE
}

You lost some of the autocomplete help, but you can solve that later on.

It still feels a little silly to have to type the same word twice, once uppercase and once lowercase. There’s one more fancy trick to use to get around this.

Using Predefined Functions

There are a few predefined functions you can use in your templates to make your life easier. These include things like inserting the current class name and changing the case of a String. You can see the full list of options in the documentation.

What you need for your template is the capitalize() function. You can pass in what you type for the view ID in lowercase and insert the uppercase version in the method name.

To do this, you need to open the Edit variables window for your template. You use predefined functions in the Expression column. Add the expression capitalize(VIEWID) to the Expression column for VIEW, and press Enter.

Use capitalize function

There are three things you can pass into predefined functions that take a parameter:

  1. Another variable.
  2. A String surrounded by quotes.
  3. Another function.

Here, you’re using the first option to pass in the VIEWID variable.

Before you save it and try it out, select the Skip if defined checkbox in the VIEW row. This says that if the value is already defined, such as if by an expression, don’t ask the user to fill it in.

Click OK and save your template. Insert the overview methods to test out your changes:

override fun showOverview(overview: String) {
  text_overview.text = overview
  text_overview.visibility = View.VISIBLE
}

override fun hideOverview() {
  text_overview.visibility = View.GONE
}

Yay! Now you only need to type the name once to create these methods. So little typing, so much code. Only one pair of methods left to implement.

Combining Functions

The last two interface methods you have to implement are showErrorMessage(errorMessage: String) and hideErrorMessage(). Are you able to spot the issue with our current template for writing these?

The method name should be PascalCase, the parameter name should be camelCase, and the view ID should be snake_case. Back to template editing to fix this!

We need all three variables again to have all three behaviors. Revert the template text to include three variables:

override fun show$VIEW$($VALUE$: String) {
  text_$VIEWID$.text = $VALUE$
  text_$VIEWID$.visibility = View.VISIBLE
}

override fun hide$VIEW$() {
  text_$VIEWID$.visibility = View.GONE
}

This adds that $VALUE$ variable back in.

Next, you need to edit the expressions for the variable. You can use both the capitalize() and camelCase() functions for this. Open up the Edit variables window.

First, make sure the VIEWID is the first variable in the list. This is the one you want to type in with the help of autocomplete.

Then, to make sure the parameter is camelCase, give VALUE the expression of camelCase(VIEWID). Make sure Skip if defined is selected.

Finally, use capitalize(camelCase(VIEWID)) for VIEW to use both camelCase and capitalization in the method name. Notice how you’re able to pass one function to another. You should also check Skip if defined for VIEW.

In the end, your variables should look like this:

Final variables

Note: You can also use capitalize(VALUE) for VIEW because VALUE is already camelCase.

Save your variables and template. Try out your template by adding the very last interface methods:

override fun showErrorMessage(errorMessage: String) {
  text_error_message.text = errorMessage
  text_error_message.visibility = View.VISIBLE
}

override fun hideErrorMessage() {
  text_error_message.visibility = View.GONE
}

All the capitalization is “magically” taken care of for you!

You’ve worked hard, and it’s time to see your hard work pay off. Build and run the app. You now have the answer to what movie you should watch next!

Final screenshot

Tips and Tricks

While you’re all set to hit the ground running with live templates, there are some other tips you can learn to go along with your new knowledge.

Default Expand With

If you don’t like expending your live templates with a Tab, you can specify what other key you’d like to use. In the live templates preferences, you can set what you want to use both for the default of all templates, as well as per template.

Default expand with option

Postfix Completion

Very similar to live templates is postfix completion. You can use this by typing a . followed by the keyword in your code. At the top of showTagline(), start typing tagline.isBlank().if.

Postfix completion

Select the if option and watch an if statement expand before your eyes. You can use this if statement to only show the tagline if the String is not blank if you like.

if (tagline.isBlank()) {
  hideTagline()
  return
}

While you can’t create your own Kotlin postfix templates at the time of this writing, you can see the list of available ones at Preferences ‣ Editor ‣ General ‣ Postfix Completion.

File Templates

Just like you have a file for a statement or a method, you can have a template for an entire file. To see the list of these or make your own, go to Preferences ‣ Editor ‣ File and Code Templates. The file templates work slightly different than live templates, so it’s great to look at existing ones for inspiration.

Under the Files tab, try making a new one for a view interface. Click the + at the top of the list, enter View Interface for the name and kt for the extension. Finally, add the following to the body:

#parse("File Header.java")
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}

#end
interface ${NAME} {
  fun showErrorMessage(errorMessage: String)
  fun hideErrorMessage()
  fun showProgress()
  fun hideProgress()
}

Click OK to save. This creates a generic interface with show and hide methods for both error and progress views. Now, when you go to create a new file, you have this option.

Create new view interface