Table of Contents#
- Introduction
- Prerequisites
- Step-by-Step Guide to Creating the Live Template
- Testing the
psfstTemplate - Customization Tips
- Troubleshooting Common Issues
- Conclusion
- References
Prerequisites#
- Android Studio (v4.0+ recommended) or IntelliJ IDEA (Community/Ultimate Edition).
- Basic familiarity with Android development and logging (e.g., using
Log.d(TAG, "message")).
Step-by-Step Guide to Creating the Live Template#
Step 1: Access Live Template Settings#
First, open the Live Templates configuration menu:
- Go to
File > Settings(orAndroid Studio > Preferenceson macOS). - In the left sidebar, navigate to
Editor > Live Templates.
This is where you’ll manage existing templates and create new ones.
Step 2: Create a Custom Template Group#
To keep your templates organized, create a dedicated group for Android-related templates:
- In the Live Templates window, click the + icon (top-right) and select
Template Group. - Name the group
Android Custom(or any name you prefer) and clickOK.
Your new group will appear in the list of template groups.
Step 3: Add a New Live Template#
Now, add a live template to your new group:
- Select the
Android Customgroup. - Click the + icon again and select
Live Template.
You’ll now see fields to configure your template:
Step 4: Define the Template Text#
Configure the core details of the template:
| Field | Value |
|---|---|
| Abbreviation | psfst (this is the shortcut you’ll type to trigger the template). |
| Description | Auto-generates Android Log TAG (private static final String TAG). |
| Template Text | private static final String TAG = "$CLASS_NAME$"; |
Why this works:
private static final String TAG: The standard modifier for aTAG(immutable, class-level constant).$CLASS_NAME$: A placeholder variable that will be replaced with the current class name (we’ll define this next).
Step 5: Configure Template Variables#
The $CLASS_NAME$ variable needs to dynamically fetch the class name of the file you’re working in. Here’s how to set it up:
- Click the
Edit Variablesbutton (below the template text box). - In the
Edit Template Variableswindow:- For the
CLASS_NAMEvariable, set the Expression toclassName(). This is a built-in function that returns the name of the current class. - Check the
Skip if definedbox to auto-insert the class name without prompting you.
- For the
- Click
OKto save the variable.
Step 6: Set the Template Context#
By default, live templates work in all file types. We need to restrict this template to Java and Kotlin files (the primary languages for Android development):
- Click the
Definelink (below the template text box, next to "No applicable contexts yet"). - In the context menu, check the boxes for:
- Java: Under
Java > Declaration. - Kotlin: Under
Kotlin > Top-level declarationsandKotlin > Class bodies.
- Java: Under
- Click
OKto confirm.
Final Template Setup#
Your template should now look like this:
- Abbreviation:
psfst - Description:
Auto-generates Android Log TAG... - Template Text:
private static final String TAG = "$CLASS_NAME$"; - Variables:
CLASS_NAME=className(),Skip if defined. - Context: Java and Kotlin files.
Testing the psfst Template#
Let’s verify the template works in both Java and Kotlin files:
Test in a Java Class#
- Open a Java class (e.g.,
MainActivity.java). - Inside the class body (after the class declaration), type
psfstand pressTab(or your configured expansion key).
Expected Output:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity"; // Generated by psfst
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Activity started"); // Use the TAG!
}
} Test in a Kotlin Class#
For Kotlin, the syntax for constants is slightly different (val instead of static final). We’ll adjust the template text for Kotlin (see Customization Tips below), but even with the Java template, typing psfst in a Kotlin file will generate:
class MainActivity : AppCompatActivity() {
private static final String TAG = "MainActivity"; // Works, but not idiomatic Kotlin
} To fix this, we’ll create a Kotlin-specific template in the next section.
Customization Tips#
1. Kotlin-Specific Template#
For idiomatic Kotlin, use val instead of static final. Create a duplicate template with:
- Abbreviation:
psfkt(mnemonic: Private Static Final Kotlin Tag). - Template Text:
private val TAG: String = "$CLASS_NAME$" - Follow the same variable and context steps as before.
Now, in Kotlin files, type psfkt + Tab to get:
private val TAG: String = "MainActivity" 2. Change the Shortcut#
To use a different shortcut (e.g., tag instead of psfst), simply update the Abbreviation field in the template settings.
3. Add a Prefix/Suffix to the TAG#
To make logs easier to filter (e.g., add a project prefix), modify the template text:
private static final String TAG = "MyApp_" + "$CLASS_NAME$"; // Java
private val TAG: String = "MyApp_$CLASS_NAME" // Kotlin 4. Integrate with Timber#
If you use Timber (a popular logging library), create a template to auto-initialize Timber.tag:
private val TAG = Timber.tag("$CLASS_NAME$") // Kotlin Timber template Troubleshooting Common Issues#
Template Doesn’t Appear When Typing psfst#
- Check Context: Ensure you enabled Java/Kotlin contexts (Step 6).
- File Type: The template only works in Java/Kotlin files. It won’t trigger in XML, Gradle, or other file types.
- Abbreviation Conflict: Another template might use
psfst. Rename your template’s abbreviation (e.g.,mypsfst).
CLASS_NAME Shows as null or Incorrect#
- Cursor Position: The template only works if your cursor is inside a class body (not outside or in a method).
- Expression Error: Verify the
CLASS_NAMEvariable usesclassName()(no typos) inEdit Variables.
Kotlin Template Generates static final (Java Syntax)#
- Context Mismatch: Ensure the Kotlin template is restricted to Kotlin contexts (Step 6). Use a separate template for Kotlin (as shown in Customization Tips).
Conclusion#
Live templates are a game-changer for reducing boilerplate in Android development. With the psfst shortcut, you’ll never manually type a TAG again—saving time and avoiding typos.
Beyond TAG, explore live templates for other repetitive tasks: creating onCreate overrides, ViewHolder patterns, or even Retrofit service interfaces. The possibilities are endless!
References#
- Android Studio Live Templates Documentation
- IntelliJ IDEA Live Templates Guide
- Android Log Class Reference
- Timber Library (Square)
Happy coding, and may your logs be ever informative! 🚀