As an Android developer, you are used to write very often same kind of code on your project. Here, we don’t talk of the creation of reusable code to avoid rewrite same code between several projects. Here, we talk about simple lines of code that are written all the time. Let’s take a well known sample. You want to display a simple Toast on the UI. You must write the complete following line :

Toast.makeText(MainActivity.this, "Hello you are on !",

Worst, write the same things all the time is error-prone. Thus, you’ve probably made this mistake for a Toast :

Toast.makeText(MainActivity.this, "Hello you are on !",

It compiles but your Toast won’t be displayed …

To make your life easier and boost your productivity, Android Studio has the concept of Live Templates. It helps you to write repetitive boilerplate code and avoid a lot of trivial errors. Let’s take a look a the following usage on Android Studio for a Toast :


Animation comes from a Google Developers sample

As you can see, you start to type “Toast”, then the Live Templates is available as a shortcut displayed as code-completion. If you select it, it inserts a code snippet that you can complete to specify required arguments. To jump between arguments, you must use the Tab key.

Even better, Android Studio has several others Live Templates. It features exactly 48 specific Live Templates for Android. You can find some samples in the following table :



Live Templates can insert more than just one line like for a Toast. With the “starter” abbreviation, you can create a complete static start() helper method to start your Activity :

public static void start(android.content.Context context) {
   android.content.Intent starter = new Intent(context, $ACT$.class);


To see the complete list of available Live Templates, you have just to go on File > Settings > Editor > Live Templates.

You’re going to tell me : “it’s great but if my need is not covered by a Live Template ?”. Don’t worry, you can create your own Live Templates on Android Studio. To achieve that, you must go on File > Settings > Editor > Live Templates. Than, click on the Android group, and press the plus button add a new Live Template.

You will need to add an abbreviation to use the template, a description, and obviously the code you wish to insert. You can imagine to add a Live Template to write a String Shared Preference value :

android.content.SharedPreferences sharedPreferences =
android.content.SharedPreferences.Editor editor =
editor.putString(getString(R.string.$stringVal$), $spVal$);


Notice that you can replace the parts of the code snippet that will be different each time with some variables marked thanks to $ symbols. Once, your new Live Template is added, you can use it thanks to its abbreviation. With that great feature offered by Android Studio, your productivity will be boosted and you could say goodbye to boilerplate code !