Table of Contents
How to create a template in Android Studio?
As we promised in the last class, we are going to learn how to create a template in Android Studio. The templates are used to save a piece of code or code snippet in Android Studio. The basic purpose of them is to ease our work and increase the speed & efficiency of work. For example, we have learned about text widget and their properties. We can make its Template with all necessary properties and then use it in our upcoming work in seconds while deleting unneeded properties.
Steps to Create a Template in Android Studio
For this purpose, we can type live templates in search of Android Studio as a shortcut. The proper way is to go into the File menu of Android Studio, Settings, Editor, and then click on live templates. Here we will find all available templates in the relevant categories. For example, STL template is in the Flutter category which we used for the stateless widget in the previous class. We can also add our templates and categories with keywords here. The first step is to create a template in an existing Flutter or Dart category or by creating a new category like “Ali”. We use the minus button to delete an existing template or category.
Use of Abbreviation
After creating a category, or clicking on an existing category, we click on + sign to add a template. Then we have to assign an abbreviation for our template. The recommended way is to set it with my_ prefix. For example, we are creating a base code template for a code that we use every time. We can make its template with “my_basecode” abbreviation.
Description of Templates
After making an abbreviation, we need to add a description to give details of this code template. For example, we can write descriptions for above mentioned base code template as “to create a basic screen structure”. It all depends upon us and on the template. After that, we will type or copy/ paste our base code in Template text. For example:
MaterialApp( home: Scaffold( appBar: AppBar( title: Text(""), ), ), );
The next step is to select dart language in define and check the option ‘reformat according to style’. Now we just need to click on apply and OK to complete this process.
How to use Live Templates in Flutter?
We just need to type my_base and our complete base code will appear automatically in Android Studio. Similarly, we can make all standard live templates in Android Studio to make life easier. For example, we think that the respective properties of Text fields are necessary in almost all the apps. We can add all these properties and make a standard widget. Then copying its code we will make its template with the abbreviation my_textfield. It increases our code efficiency. Similarly, we can create a button or anything with templates in Android Studio following the above-explained steps.
Advanced options for Android Studio Templates
Though we have completed the process, an advanced option can also be used here. When we create a template, then we need to change data or specific options in it. In a long template, we can forget this data. So we use $ sign to address this issue. For example, in an app bar, we have to update its title every time. For this purpose we will close every changing item in $ sign i.e. $APP_BAR-TITLE$. Let’s see its example:
MaterialApp( home: Scaffold( appBar: AppBar( title: Text("$APP$"), ), ), );
Now after adding that template in Android Studio, we need to use the tab or enter to move to the next changing code. For the students working in flutlab.io, text files can be created as templates.
How to use templates on All devices?
We can login into Android Studio with our Gmail ID from the right top side. It will allow us to use templates on other devices using the same ID. It also helps in recovering code in case Windows corrupts.