Android Studio Live Templates

Android Studio Live Templates

As a developer, I hate doing repetitive task. So imagine yourself having to write a starter method for you activity. You need to define the method, create the instance of the intent, eventually put your extras, and finally call startActivity. 

Now, think about just eventually adding the extras and nothing else. How does that sound? Let’s speak about Live TemplatesThey’re not so famous but they can help you a lot.

Android Studio Live Templates

Code completion can improve your productivity by reducing how much you have to type, but there are situations when a more powerful tool is needed. Thanks to Android Studio and IntelliJ, live templates make it much easier to focus on just the things you care about.

Android Studio has already a defined set of Live Templates you can useYou can find the list of the available templates inside the Live Templates entry in the Android Studio Preferences/ Settings.

What if I don’t like the wording of one template? It’s very easy. Simply select the entry you would like to change and define the abbreviation is most convenient for you.

Live Templates

Live Templates are code snippets that I can insert into my code by typing their abbreviation and pressing tab. They live in the Editor section of the Preferences.

“They are frequently-used or custom code constructs that you can insert into your source code file quickly, efficiently and accurately.”

You might have bits of code that are very specific to your project or, simply, they are not already on the list mentioned before. So you decide to create your own one, but how do I do that?

Let’s say we don’t have the starter template already and we would like to create it. In this example, we will create a live Template to insert

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

Write the abbreviation you would like to use and optionally add a description.

Define the context where this template should work, e.g. Java, Groovy, xml. Then write the code! 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.

This is the code we are adding:

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 auto completion hint by pressing tab — and it will paste in our code snippet!

The code is the added. Remember this only works within the Java context, because we defined this when creating the live template.

I know, this is wrong! We should not have that code there. But this was supposed to give you an idea on how to use Live Templates. I will write another tutorial about File Templates, remember to subscribe and get updates 🙂

Thanks for reading this and I’ll hope you start using Live Templates for your day to day development. Remember to share, follow on social media and leave comments below 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *