Tutorial — Create Your Own Template for Android Studio
As a native Android developer who uses Android Studio, template provided by Android Studio is of course very helpful. The template will be very helpful in the process of writing code, because there is no need to create files or write the same code over and over again.
However, if you and your team use a certain pattern in writing code, of course the template provided in the Android Studio often does not match what you want. And it will take more time if you have to adjust each code generated by the template in the Android Studio to fit your team’s wishes.
For this reason, in this article we will discuss how to create a template on Android Studio that suits your needs, by using FreeMaker.
In this article we will create a Tab Activity with RecyclerView on it. Let’s begin!
1. Open Folder
For Windows user:
{ANDROID_STUDIO_LOCATION}/plugins/android/lib/templates/other/
For Mac user:
/Applications/Android Studio.app/Contents/plugins/android/lib/templates/other/
2. Create a Folder with the Name of Your Template
For example, you can use:
other/Tab Activity with RecyclerView/
3. Create Files and Folders
The next step is create files and folders. Here are what you need to create:
template.xml
Is a file that contains a display from your screen template dialog.recipe.xml.ftl
It is a place where all of yourfiles.ftl
will transform into a new file with.java/.xml/.kt
extension, based on the logics that you have defined, and also theid
that you define intemplate.xml
.globals.xml.ftl
It is a file that contains global variables such aspaths
,packages
, anddirectories
. This file is optional.root/
folder
It is a place where all files containing the code of your template will be saved.
4. Copy Code to template.xml
In template.xml, copy this code:
The above code contains commands to display interfaces for creating templates. Some parameters that you can use include:
id
same as theid
in thexml
file when we create a layout file on Android.name
just likehint
onedittext
.type
kind of input you want, here are some of types that you can use.
- String, same asedittext
.
- Enum, make the input become adropdown/spinner
.
- Boolean, makes an input in the form of acheckbox
.constraints
It’s like aninputType
onedittext
. Can be filled withclass|unique|nonempty
.default
define the default value of the input.help
provide suggestion at the bottom of the template interface.
5. Copy Code to recipe.xml
In recipe.xml, copy this following code:
The code above use to give commands to transform any file in the root/
folder into a new file. Some commands that you can use include:
instantiate
It’s like a bridge between the template files and user input fromtemplate.xml
. This command is also used to generate new files from template files.open
A command to open files that have been generated byinstantiate
.
6. Create Directory in root/
After that, the next step is create these two directories in root/:
src/app_package/
to save the class file that will be generated.res/layout/
to save the layout file that will be generated.
7. Create a Template for Class File in root/src/app_package Folder
In root/src/app_package folder, create a template for class file like these examples:
Activity.kt.ftl
Adapter.kt.ftl
TabFragment.kt.ftl
ViewPagerAdapter.kt.ftl
8. Create a Template for Layout File in root/res/layout Folder
In root/res/layout folder, create a template for layout file like these examples:
activity.xml.ftl
item.xml.ftl
tab_fragment.xml.ftl
That’s it! Your template is done!
Testing the Template
Now, to test that has been created, do the following step:
- Open the Android Studio, or if it’s opened, please restart.
- Right click on a package that you want to create the template in and select it from the New menu, as the picture below:
- You will then be prompted to fill in the variables. See the example below:
- Wait until the Android Studio finish generating your template.
- You can customize the template that you have just created. There is no limit, which limit is only your own creativity.
Reference:
- https://riggaroo.co.za/custom-file-template-group-android-studiointellij/
- https://freemarker.apache.org/
- https://android.jlelse.eu/supercharging-your-app-development-speed-with-custom-file-templates-3e6acb6db6c3
- https://medium.com/androidstarters/mastering-android-studio-templates-ed8fdd98cb78
- https://medium.com/google-developers/writing-more-code-by-writing-less-code-with-android-studio-live-templates-244f648d17c7
- https://www.jetbrains.com/help/idea/using-file-and-code-templates.html
Anang Kurniawan is part of Android team at GITS Indonesia. One of the project he had handled was a mobile application where cattlemen can interact with the veterinarians. Anang is eager to achieve his dream to be a Google Developer Expert.