Android Studio Live Templates
Android Studio has a lot of nice features to make the lives of us Android developers a lot easier. One of those features is Live Templates. Raise your hand if you don’t know what a Live Template is. Got your hand up? Great, keep reading. Don’t have your hand raised? Well… there still might be a nice surprise for you at the bottom of this article.
What are Android Studio Live Templates?Live Templates are small code snippets that you can insert in the editor by typing an abbreviation and pressing tab or enter. They can be configured in Preferences… → Editor → Live Templates. The Template text box contains the code that will be inserted in the editor. It has placeholders for variables that can be configured in Edit variables. When you insert a Live Template the caret will be placed at the position of the variables to overwrite the optional default value and finally accept with the enter key. When you are ready to insert a Live Template, just type the abbreviation in the editor, Android Studio will recognize this abbreviation and show a description next to it. Next, accept the Live Template by pressing the tab or enter key. Android Studio will insert the appropriate code in the editor and put the caret at the first variable. Finally, overwrite the default values if needed, populate empty variables and accept every variable with enter. That’s it, done! You’re ready to write code even faster now! Android Studio comes with a big collection of ready to use Live Templates for both Java and Android specific code. Some great examples are for loops, findViewById(…), Log statements, Toasts, and whole lot more! I suggest you take a look at Preferences… → Editor → Live Templates and explore what is available. You are free to add more Live Templates, and that is where the next part comes in.
Live Templates for TimberAndroid Studio has built-in Live Templates for every kind of Log statement available in the Android Sdk. Since I use Timber (a small extensible logger API) in most of my projects, these build in Live Templates were not at use to me. That’s why I configured my own Live Templates specifically for Timber. These make it easy to log and make new Timber Tree instances.
- timv, timd, timi, timw and time will create Timber log statements for VERBOSE, DEBUG, INFO, WARN and ERROR verbosity.
- timp will plant a new Timber Tree instance.
- timt will allow for a one-time use of a custom tag.
- and finally timm and timr will log the parameters and return value of a method.