If you’re tired of writing the same repetitive code in Android Studio, there’s a powerful feature you might be overlooking: Live Templates. This productivity gem can help you insert commonly used snippets with just a few keystrokes — speeding up development and reducing errors.
In this article, we’ll explore what Live Templates are, how to use them, and how to create your own custom ones.
📌 What Are Live Templates?
Live Templates are predefined code snippets in Android Studio that you can insert into your code using short abbreviations. When you type a keyword (like logt
) and hit Tab, Android Studio expands it into a complete code block.
They work for Java, Kotlin, XML, and even documentation formats.
⚡ Why Use Live Templates?
- 🚀 Speed Up Development – Write less, do more.
- ✅ Avoid Typos – Predefined templates reduce manual typing errors.
- 🎯 Focus on Logic – Spend less time on boilerplate and more on your app’s logic.
🛠 Common Built-In Live Templates
Here are some default Live Templates that every Android developer should know:
Abbreviation | Expands To |
---|---|
logt |
Log.d(TAG, ""); with auto-generated TAG |
Toast |
Toast.makeText(context, "", Toast.LENGTH_SHORT).show(); |
fori |
A standard for loop |
sout |
System.out.println(); |
psf |
public static final |
new |
Inserts a new object creation snippet |
🧪 How to Use Live Templates
- Open a Kotlin or Java file in Android Studio.
- Start typing a template keyword like
logt
. - Press
Tab
orEnter
(depending on your settings). - The snippet auto-fills with placeholders you can quickly tab through and customize.
✅ Example:
Log.d(TAG, "message");
Android Studio will automatically create a TAG
constant if it doesn’t already exist.
🧰 How to View and Customize Live Templates
- Go to File > Settings (or Preferences on Mac).
- Navigate to Editor > Live Templates.
- You’ll see a categorized list like Java, Kotlin, XML, etc.
- Select any template to:
- Edit its abbreviation.
- Change the code.
- Set usage conditions (e.g., only in methods).
✍️ Creating Your Own Live Template
🔧 Steps:
- In Live Templates, click
+
> Live Template. - Set:
- Abbreviation:
mylog
- Description: My custom log statement
- Template Text:
Log.d("$TAG$", "$MSG$")
- Abbreviation:
- Click Edit Variables:
- Set
TAG
asexpression: className()
- Set
MSG
asexpression: complete()
- Set
- Define Applicable Contexts (e.g., Kotlin → Statement).
🚨 Pro Tips
- Use Tab Stops (
$VAR$
) to jump between placeholders easily. - Create XML-specific templates for layout design.
- Share your templates across your team using export/import.
📦 Bonus: Exporting Templates
Want to use your templates on another computer or share with your team?
- Go to File > Export Settings.
- Select Live Templates.
- Save and import them on another Android Studio instance.