Mastering Live Templates in Android Studio: Code Smarter, Not Harder

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.

Live Template Example in Android Studio


⚡ 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

  1. Open a Kotlin or Java file in Android Studio.
  2. Start typing a template keyword like logt.
  3. Press Tab or Enter (depending on your settings).
  4. 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.

Log.d Template Example in Android Studio


🧰 How to View and Customize Live Templates

  1. Go to File > Settings (or Preferences on Mac).
  2. Navigate to Editor > Live Templates.
  3. You’ll see a categorized list like Java, Kotlin, XML, etc.
  4. Select any template to:
    • Edit its abbreviation.
    • Change the code.
    • Set usage conditions (e.g., only in methods).

Live Templates Settings Panel


✍️ Creating Your Own Live Template

🔧 Steps:

  1. In Live Templates, click + > Live Template.
  2. Set:
    • Abbreviation: mylog
    • Description: My custom log statement
    • Template Text:
    Log.d("$TAG$", "$MSG$")
  3. Click Edit Variables:
    • Set TAG as expression: className()
    • Set MSG as expression: complete()
  4. 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.