{
  "$type": "site.standard.document",
  "canonicalUrl": "https://segunfamisa.com/posts/making-live-templates-in-android-studio",
  "description": "Notes about my favourite Android Studio live template",
  "path": "/posts/making-live-templates-in-android-studio",
  "publishedAt": "2025-07-17T06:00:00.000Z",
  "site": "at://did:plc:a5mekodp4afxadlpr4hp2wci/site.standard.publication/3mm2oa7vz5327",
  "tags": [
    "android",
    "android studio",
    "tooling"
  ],
  "textContent": "_If you are already familiar with live templates, feel free to skip to the live template for kotlin tests_\n\nIf you use IntelliJ-based IDEs like Android Studio, WebStorm, etc., chances are that you have seen some kinds of “shortcuts” (pictured below) show up when you press some key combinations. These are called live templates. \n\n<div style=\"text-align: center;\">\n  <img src=\"/images/live-template-example.png\" alt=\"Live Template Example\" style=\"max-width: 100%; height: auto;\">\n<p style=\"margin-bottom: 1.5em;\"></p>\n</div>\n\nFor example, if you type sout in a Java/Kotlin file, you see a pop-up like the one above. When you press tab, it autocompletes into System.out.println (in Java) or println in Kotlin. \n\nLive templates allow you to generate common code constructs by typing keywords that trigger the template. This feature is not new, and I definitely remember seeing it in the Netbeans IDE when I developed J2ME applications.\n\nIf, like me, you write a lot of Android code in Android Studio, you may also know that if you type comp in a Kotlin file and press the tab key, a composable function is generated.\n\nI have personally found these templates extremely useful for developing faster, and I use them all the time. I even have a few of my own templates, and one of them is the main reason I am writing this article.\n\n—\n\nLive Template for Kotlin tests\n\nOne of the things I do almost every day when I write code is write tests. If you use Kotlin, you might know that backticks are allowed in test function names.\n\nI like to optimise my workflow, and I realised this was an opportunity to save myself a few keystrokes for every test I write. I ended up writing a live template to automatically create a test function with backticks (does anyone else find the backtick key awkwardly placed on the keyboard?).\n\nTo set this up, I follow the instructions on JetBrains’ website. You can do the same:\n\n1.  CMD + ,  to open settings\n2. Navigate to Editor > Live Templates \n3. Select Kotlin group (the templates are usually grouped, but you can also create your own group if you so please)\n4. Click on the little + button or CMD + N , and select 1 or Live Template \n\nNow we can begin to set the details for the live template.\n\nThe first thing is to set an abbreviation and a description for the template. I used ktest for the abbreviation and a description that says “Creates a Kotlin test function with backticks”.\n\nNext, I define the applicable context. Since this is a test function, I set it to Kotlin > Class. If you want to define a live template applicable to other kinds of files, you can also set that there.\n\nThen the template text:\n\nThe $NAME$ and $END$ here are variables in the templating system. \n\n- $END$ indicates the position of the cursor after the template has been executed.\n- $NAME$ is a variable for the function name.\n\nThe overall setup looks like this for me:\n\n<div style=\"text-align: center;\">\n  <img src=\"/images/custom-live-template-setup.png\" alt=\"Kotlin Test Live Template Setup\" style=\"max-width: 100%; height: auto;\">\n</div>\n\nAnd when I type ktest in a test file, this pops up:\n\n<div style=\"text-align: center;\">\n  <img src=\"/images/custom-live-template-usage.png\" alt=\"Kotlin Test Live Template Usage\" style=\"max-width: 100%; height: auto;\">\n  <p style=\"margin-bottom: 1.5em;\"></p>\n</div>\n\nThe templating system\n\nThe templating system is more involved than shown here, and the template could be much more complex. In this example, we are not really using the function name for anything else.\n\nLet’s imagine for a moment that we wanted to add a comment with the function name capitalised. To do that, we could introduce a new variable called $COMMENT$. Then, we can edit the variable and apply one of the pre-defined template functions to the NAME variable.\n\nWhat else is this useful for?\n\nAnother use-case for live templates is for live coding demos or presentations. If you are doing a live coding demo and expect to create certain scaffolding, you can prepare it as a live template before your presentation.\n\nWhat about sharing?\n\nUnfortunately, sharing the template is a bit manual at the moment. JetBrains does allow you to sync templates, but it only syncs across your IDEs. There does not seem to be an immediately obvious way to automatically share them across your team. I think it’s a solvable problem, but I haven’t had time to look into it yet.\n\nYou can see more sharing options on JetBrains website.\n\nI hope you have found this note helpful. I initially started this as a note to share with my coworkers and friends, but I decided to write it here, in the spirit of wisely spending the keystrokes I have left :D.",
  "title": "Live Templates in Android Studio"
}