Writing More Code by Writing Less Code with Android Studio Live Templates

An Android Tool Time Pro Tip

If you’ve written much Android code, you’ve probably made this mistake at least once:

Toast.makeText(MainActivity.this, "This will not be displayed");



What if I told you there was a way to guarantee you can avoid this mistake, and also use fewer keystrokes?

What is this magic I speak of? Live Templates!

If you’re an experienced IntelliJ user, you’re probably already aware of Live Templates — in which case skip ahead to the Android-specific templates included in Android Studio

Unless you’re getting paid by the keystroke, no one wants to write repetitive boilerplate code. It’s easier to show than explain, so here’s how they work.

Live template to (correctly) create and display a new Toast

As you can see, Live Templates are shortcuts displayed as code-completion options that, when selected, insert a code snippet that you can tab through to specify any required arguments.

For example, as shown above — typing “Toast” then hitting the Tab key inserts the code for displaying a new Toast with argument placeholders that you can enter, before hitting tab and moving on to the next argument.

Android Studio Live Templates: A Handy Reference

IntelliJ includes over dozens of Live Templates, and Android Studio features another 48 specific for Android development. Here’s a few of my favorites for easy reference