Live Code Templates in Android Studio

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. You guessed it right. I am talking about Live Templates.

For example, to show a simple Toast in android apps, we use something like this:

Toast.makeText(context, "The Message For The Toast");

Each time, if we want Toast, we have to write exactly the same code with only Context and message different. Android Studio allows us to create templates or as they are called Live Templates, through which we can write same code with minor differences much more quicker than typing it all as shown in the picture below:

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. For the Toast example above, we can do it using this line of code below:

android.widget.Toast.makeText($className$.this, "$text$", android.widget.Toast.LENGTH_SHORT).show(); 

To add this code for Live Templates in Android Studio, go to File -> Settings -> Editor -> Live Templates, and you will see all the existing templates by Android Studio and your custom templates as well like the image below.

Then you can create new template and paste the above line and you’re done. You can also change the keywords for already existing templates as well like this:

You can learn more about templates on this video from Google.

Everything Is AWESOME

If you liked this article, you can read my new articles below:

Wajahat Karim is Pakistan’s first Google Developer Expert in Android. As an experience Android developer, he deeply cares about it and keeps writing and speaking about it. He has written two worldwide 300+ pages books on Android development with more than 100 articles around the internet either on his website or his Medium publications. He is also a passionate contributor in open source and has created many Android libraries used by thousands of developers in their apps worldwide. As active public speaker, he spends lots of time giving talks in conferences and motivating people on Android development. You can find Wajahat most active on Twitter @WajahatKarim where he regularly shares all the good stuff about Android and community building.

Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan