Android Studio Live Templates

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? Android Studio 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.

Android Studio 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

A small sampling of Android Studio Live Templates
A small sampling of Android Studio Live Templates

Live templates can also insert larger code snippets; such as starter which creates a static start(…) helper method to start an Activity:

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

Similarly, newInstance that creates a new Fragment instance with arguments, and ViewConstructors, adds generic view constructors to your custom View.

You can use the File > Settings > Editor > Live Templates menu option to see the full list.

Of course, if your favorite boiler plate isn’t already there, remember that you can:

Create Your Own Live Templates

Go to File > Settings > Editor > Live Templates. Click the Android group, and press the plus button to add a new Live Template.

You’ll want to choose an abbreviation to use the template, a description to remember what it does, and of course the code you’d like it to insert — like this example for writing a boolean Shared Preference value.

android.content.SharedPreferences sharedPreferences =     
  getPreferences(Context.MODE_PRIVATE);
android.content.SharedPreferences.Editor editor = 
  sharedPreferences.edit();
editor.putBoolean(getString(R.string.$stringVal$), $spVal$);
editor.apply();

Notice that we’re fully qualifying the class paths, and most importantly that we replace the parts of our code snippet that will be different each time with variables indicated by wrapping the names with matching $ symbols.

With our new Live Template defined, we can type the abbreviation — select it from the autocompletion hint by pressing tab — and it will paste in our code snippet!

What code are you going to Live Templatize? Tell me in comment.

By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at ceo.tellmehow@gmail.com

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...