Tutorial — Create Your Own Template for Android Studio

Anang Kurniawan
GITS Apps Insight
Published in
4 min readJan 30, 2019

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.

Default template in Android Studio

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 your files.ftl will transform into a new file with .java/.xml/.kt extension, based on the logics that you have defined, and also the id that you define in template.xml.
  • globals.xml.ftl
    It is a file that contains global variables such as paths, packages, and directories. 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 the id in the xml file when we create a layout file on Android.
  • name
    just like hint on edittext.
  • type
    kind of input you want, here are some of types that you can use.
    - String, same as edittext.
    - Enum, make the input become a dropdown/spinner.
    - Boolean, makes an input in the form of a checkbox.
  • constraints
    It’s like an inputType on edittext. Can be filled with class|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 from template.xml. This command is also used to generate new files from template files.
  • open
    A command to open files that have been generated by instantiate.

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:
Add New Template
  • You will then be prompted to fill in the variables. See the example below:
Template Form Interface
  • 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.

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.

--

--