Mastering Android Studio Templates Creation
Tags: androidappdevelopmen, android, template, androidstudio
Published: 2023-01-06 17:43:23

---
title: Mastering Android Studio Templates Creation
published: true
date: 2017-08-28 18:38:33 UTC
tags: androidappdevelopmen,android,template,androidstudio
canonical_url: 
---

 **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/](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.

{% gist https://gist.github.com/TakeoffAndroid/6200d4fd63f29b3d9b17646dee254fd4 %}

**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)

{% gist https://gist.github.com/TakeoffAndroid/8d9b204c86b154ab9bf8a23f464d6e47 %}

**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.

{% gist https://gist.github.com/TakeoffAndroid/6200d4fd63f29b3d9b17646dee254fd4 %}

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

![](https://cdn-images-1.medium.com/max/1024/1*doQIPZcWIumF3F5SRcg2Mg.png)

Similarly,

**CheckBox**

{% gist https://gist.github.com/TakeoffAndroid/5aacabcf9ef3c5296195d6b2404332ae %}

![](https://cdn-images-1.medium.com/max/264/1*X6rZQbrgFLhZ9cpqkXkvwQ.png)

**Spinner (Dropdown)**

{% gist https://gist.github.com/TakeoffAndroid/116b4b6c2c8db19eb25043674d19d570 %}

![](https://cdn-images-1.medium.com/max/1024/1*AAjCr845H8w8dn2CRy0j1w.png)

### **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

{% gist https://gist.github.com/TakeoffAndroid/0e94eaa77f631d00a67cb25743fd3058 %}

**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.

{% gist https://gist.github.com/TakeoffAndroid/d6a6636f01f9c96f54de6f332dd24590 %}

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

**For Example:**

{% gist https://gist.github.com/TakeoffAndroid/371eeac9ac732ea1c72bc411a2bbd605 %}

${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:**

{% gist https://gist.github.com/TakeoffAndroid/d050d2c465ca8bfb00eacb1728ab5f83 %}

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

Similarly,

{% gist https://gist.github.com/TakeoffAndroid/90a561fc5784d2ce37ef5a24ce0bc832 %}

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:**

{% gist https://gist.github.com/TakeoffAndroid/abbb1f521fe53c90d42f434a98dbda8b %}

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”;
}
```

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

**For Example:**

{% gist https://gist.github.com/TakeoffAndroid/5a039521924699db490d5da21f7925f6 %}

### **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.

- [TakeoffAndroid/RecyclerViewTemplate](https://github.com/TakeoffAndroid/RecyclerViewTemplate)
- [TakeoffAndroid/MaterialTabsTemplate](https://github.com/TakeoffAndroid/MaterialTabsTemplate)

### Recommended Reading

[Introducing AndroidStarters](https://medium.com/androidstarters/introducing-androidstarters-4a59514c82e1)

* * *