So what is a Template?
Templates are the files that have some boilerplate code. For example, when we create Activity, Service, Fragment from the list of predefined options, we see a lot of boilerplate code already written from the predefined templates.
Let’s create our own live template of creating RecyclerView adapter with inner ViewHolder class with the below mentioned steps-
-
-
- Right click on the package folder and then select New->Edit File Templates
- Click + button to create a new template. You can name this template as you like.
- Paste the following code in this file.
- ${<VARIABLE_NAME>} is used to create variables in the templates. When you use this template, there will be a prompt asking to enter values for them to create code.
- The #if directive is used to check that package is empty or not. It adds the name passed as the ${PACKAGE_NAME} variable for the non-empty package.
- The #parse directive is used to insert the contents of another template.
- Now again click on package folder and then select New and you will be able to see your template there. Clicking on that template will open a prompt box to fill the values defined in the above variable.
Isn’t that cool? We can also create similar templates and can use anywhere in the project to avoid the boilerplate code. Android Studio is a very powerful tool and features like templates are really helpful in speeding up the development process. These templates are really helpful if you’re working with other team members so that all of them can use it to their advantage.
-