Android Studio File Templates

Android Studio File Templates

Howdy guys and Happy New Year! I decided to start the new year by writting an article here about File Templates.

If you are lazy like me, then you are genious! One of the wealthiest people in the world, Bill Gates, said “I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” If Bill said this, it must be true! Except if it has something to do with Windows Vista (Window’s freaks don’t hate me).

So being lazy is natural, we always try to accomplish our job faster and with minimal effort. As an Android developer, I often use templates. Templates are cool because you don’t need to write the same lines of code a thousand times. But let’s go step by step. First we will make a simple ViewPager adapter template and RecyclerView adapter template.

So let’s get started.

Templates basics

For creating new templates, right click on the source code folder and select “New”, then select “Edit File Templates”.

Then you will see a window like in picture below:

Here you can see some predefined templates but we don’t need this so click on “+” icon in top left corner. After this action your screen should look like this.

In the field called “Name:” enter a name for your template. Name should be “ViewPagerTemplate”.

Now we are ready for some fancy template coding in the big area below template name. For defining some custom variable, write ${VARIABLE NAME}. Entry for variable name will be displayed during class creation, also here are some predefined ones: ${PACKAGE_NAME}, ${NAME}, etc.

Example 1.

In this example we will use only predefined variables. ViewPagerAdapter template code is shown below:

Example 2.

In second example shown below, we will be using custom and predefined variables. Use “RecyclerViewAdapter

As shown in these two examples, creating templates is easy. You only need to worry about where to put the custom or predefined variables.

How to use Templates?

First select the package where you want to generate your template code. Then choose “New” and select a template.

After selecting a template, you have one more step — writing variable entries.

As you can see, we need to set entries for every custom and some predefined variables. For class generating simply hit “OK”.

You need to create the demo_layout file and pass your ArrayList data also for this to work.

Make sure you have RecyclerView dependency in your gradle. Most of you might miss this step!


Creating templates is easy and saves a lot of time, but if you are a Junior Android developer don’t use templates until you learn what those generated class do.

I hope you found this article interesting, till next time!

Happy Coding!

More From Android Study:

Leave a Reply

Your email address will not be published. Required fields are marked *