Make coding faster on Android Studio with Templates — Part 3 (Activity Templates)

Ikhwan Koto
4 min readMay 15, 2020

--

Hello, welcome to Part 3 about Templates on Android Studio. In this part, we will discuss the Activity Templates.

This is the last part about Templates on Android Studio. Even though the title is written as Activity Templates, but we can do more than it. With one template, we can create many files automatically. This is an example:

If you haven’t read the previous section, you can visit the following link:

Part 1: https://medium.com/@ikhwan220397/make-coding-faster-on-android-studio-with-templates-part-1-live-templates-3fd25ffee044

Part 2: https://medium.com/@ikhwan220397/make-coding-faster-on-android-studio-with-templates-part-2-file-and-code-templates-c5c4da8f8e78

If you want to read this post in Bahasa, you can open this link:

https://www.yukngoding.id/2020/04/26/buat-ngoding-lebih-cepat-di-android-studio-dengan-templates-part-3-activity-templates/

Back to the topic. How to create an Activity Template? Let’s start :D

1.Open Folder

For windows user: {ANDROID_STUDIO_LOCATION}/plugins/android/lib/templates/

For macOS user: /Applications/Android Studio.app/Contents/plugins/android/lib/templates/

Sceenshot templates folder

2.Create folders and files

In this post, we will create a template for an Activity with the MVP pattern. We will create a new folder in the activities folder. For example, we named the folder with YnMVP.

YnMVP Directory

We created some files too. Let's discuss it one by one.

3. template.xml

This file used to receive input from the user. This is a screenshot for template.xml we are going to create.

Result of template.xml

Copy this code shown below:

<template>

  • name. Used for the name of our template. It will display on the list of templates.
  • description. The description will be shown at the top of our form.

<category>

  • We will create a template for activity, so we set the value with “Activity”

<formfactor>

  • We made this template for Android, so set the value to “Mobile”. Fill in the value with the type of template to be created.

<parameter>

  • id. This used for identity. To access input values from the user, we can get it from id.
  • name. A name will be shown on the left side of the form.
  • type. The type of input desired from the form
  • constraints. Additional criteria for our form
  • default. The text will fill the form when the first launch. It is like the MainActivity text that fills the form when we created an activity.
  • help. An additional description that appears on the bottom side.
  • suggest. It is like while we fill the name of the activity, the name of the layout change automatically.

<thumb>

  • An image will be shown when we create the template.

4. globals.xml.ftl

In this file, we declare additional IDs that will be used for other code from our template.

  • ${layoutName} and ${mvpClass} are IDs that come from template.xml. It save the value to default IDs (like: simpleLayoutName) which will be used id creating templates.
  • In this file and the other file
  • In this file and the next few files, there are some codes “include” for default template activity. The default id (such as: hasNoActionBar) will be used in some of the included files.

5. recipe.xml.ftl

Codes in this file have a responsibility to create files for our template.

  • recipe_manifest.xml.ftl added so the activity will register in AndroidManifest.xml
  • instantiate use to create files. from as a reference which template will be made to the file. to as an address and naming of the files will be created.
  • ${mvpClass} and ${layoutName} are IDs from template.xml
  • open use to open files .

6. ViewClass.kt.ftl

This file is a first template used to create an Activity.

  • ${escapeKotlinIdentifiers(packageName)} use to write package names according to the location the file was created.
  • ${mvpClass} and ${layoutName} are IDs from template.xml

7. ContractClass.kt.ftl

  • ${escapeKotlinIdentifiers(packageName)} use to write package names according to the location the file was created.
  • ${mvpClass} is ID from template.xml

8. PresenterClass.kt.ftl

  • ${escapeKotlinIdentifiers(packageName)} use to write package names according to the location the file was created.
  • ${mvpClass} is ID from template.xml

9. Run

After file created, open Android Studio (if it already open, restart it first). With a tutorial on this post, we can create files like the video shown at the beginning of this post.

We are at the end of this post. Thank you to spend your time reading my post. Visit my profile to read other interesting posts :D

Bye-bye ~

--

--