DEV Community

Chandrasekar Kuppusamy
Chandrasekar Kuppusamy

Posted on • Originally published at Medium on

Mastering Android Studio Templates Creation

What is Android Studio Template?

Android Studio provides code templates which helps developers to follow the set of android optimised, best design and standards in a simplified way by reducing the burden of writing the same piece of code again and again. Yeah! You can create your own Android Studio Template to follow the common standards and practices across the teams.

Which Language is used for Android Studio Template Creation?

The answer is Apache FreeMarker template engine http://freemarker.org/

Getting Started with Android Template Creation

It is just like to the art of cooking wherein all ingredients, compositions and styles are grouped into a perfect recipe. Yes! we do have three major compositions in template creation:

  1. templates.xml  — The UI master which represents how your template screen should look like
  2. recipe.xml  — The container box where all your template files (.ftl) are ready to evolve into Android code (.java , .xml) files based on your checks, logic and id defined in templates.xml
  3. globals.xml  — The place where you keep all your directories, path and package structures in a simple variable.

Ooo! It’s easy to illustrate the definition but hard to reproduce in a practical way. Yes! Let’s iterate all the steps one after the another in an Android Development terminology.

How to create UserInput, Checkable, Dropdown view’s in templates.xml?

Yeah! Technically speaking creating EditText , CheckBox and Spinner UI components.

id  — It’s pretty easy to understand which is something similar defining id’s for the xml layouts in Android (i.e @+id/btn_send )

name  — It is nothing but a hint of the EditText (i.e android:hint=”Enter your mobile number”)

type  — This is actually a game changer! It defines the actual view property whether it should be a EditText or Spinner or CheckBox.

type=”string” — Hey! Create EditText

type=”enum” — Hey! Create Spinner (Dropdown)

type=”boolean” — Hey! Create CheckBox

constraints  — It is something like defining android:inputType=”text|textEmailAddress|number|textPassword”

class: We all know it’s a common phenomenon (i.e Activity, Fragment, Presenter, Model, Utility Class names)

unique: This make sure that same name is not duplicated in your package. (i.e If MainActivity already exists in the project, It avoids duplicating the Activity name again by showing simple suggestions like Main2Activity )

nonempty: Just checks whether the field contains value or not. (i.e !string.isEmpty())

visibility  — It defines whether this particular View should be visible or gone based on the id of other View’s.

For Example: (Android Logic)

default  — Which is something similar to assigning default text in EditText (android:text=”MainActivity”)

help  — Gives the detailed suggestion and usage of this view at the bottom of the template panel.

Here you go! The final output of the above code snippet would be like as below.

Similarly,

CheckBox

Spinner (Dropdown)

How recipe.xml in Template creation work?

The recipe.xml can be used efficiently by grouping various key components together like

  1. dependency  — This tag is used to add dependencies to the app build.gradle file

For Example: To add RecylerView as dependencies in your project

2. merge  — As the name says, this merges your template code into your project files.

For Example:

The below code merges manifest contents from templates to your app AndroidManifest.xml file. Similary, merge can be used to combine your template strings.xml, colors.xml, styles.xml to your app files.

3. instantiate  — It provides bridge between Template files and input content received from templates.xml

For Example:

${activityClass}.java — It receives the activity class name inputed in User Input field (EditText) in templates.xml.

MainActivity.java.ftl — Where actual code of Activity contents is placed inside template root folder.

Output:

  1. If user inputs a “MainActivity” in activityClass User Input Field View, MainActivity.java file will be created in the project
  2. Similarly If user inputs “LoginActivity” in activityClass User Input Field View, a LoginActivity.java file will be created in the project

4. copy —  It copies the contents from template to project folder.

For Example:

The above code snippets copies all the contents (i.e drawables, selectors,etc.,)present inside template drawable folder to your project drawable folder.

Similarly,

I am not gonna explain this since you can figure it out on your own ;-)

5. open  — Yes it opens the files you want to open on the launch after codes are generated from Templates.

For Example:

This opens my MainActivity.java or whatever inputed in activityClass User Input View

What is the role of globals.xml ?

Yeah! Similar like accessing constants by defining globally in Java

public class Constants{
      public static BASE_URL =”https://takeoffandroid.com”;
}
Enter fullscreen mode Exit fullscreen mode

globals.xml helps to maintain paths, directories or project names globally by creating a id (i.e variables).

For Example:

Queries ?

You’re always welcome to share, fork, and play with my open source Android studio template handcrafted for RecyclerView’s and TabLayout’s boiler plates.

Recommended Reading

Introducing AndroidStarters


Top comments (0)