Sitemap

Creating File Templates in Android Studio

5 min readAug 28, 2022
Photo from Unsplash

If any of you out there have been following an architecture like i do, we all suffer from creating similar files with similar boiler plate (but necessary) code again and again. Its exhausting! And since as developers we are taught to try to create re-usable code every time, so why can’t we do that with files. One would assume that big, standard IDE’s might have something which can facilitate the reusability of these files or in other words offer some sort of File templates for us to use.

Well I don’t know about any other IDEs, but Android Studio does provide that and even more. You can customise the existing Templates like that of an Activity, an Adapter or even an XML file. On top of that, you can create your own templates and customise them as per your own needs. In the following section we are going to do just that and more.

Edit existing Templates

You can open the File Template menu by right-clicking on any of the project folders on the left side of Android Studio and click on New, scroll down do Edit Templates at the bottom, well almost at the bottom.

RIGHT CLICK -> NEW -> EDIT TEMPLATES

If you have clicked as mentioned and shown above, you’ll end up with a Pop up window which should look like the one below:

Under the Files tab, you can see lot of templates for basic classes, interfaces, Enums etc of different languages which are all editable. So you can change the structure of any of these files and once you are done, you can click on Apply and the changes will be applied to your IDE.

You can find the Android Framework specific files under Other tab which can be edited as well.

Now to the exciting part

Create New File Template

As you read above that we can create File templates of our own as well. In fact, we can create multiple files at once. That part gets me excited.

On the Edit Template screen, under the Files Tab, you can see a + sign.

Once you click on it, a window will open to the right where you can see the following fields:

1. Name: This is the name of the template that will get created.

2. Extension: This will be extension of the file that you will create using this Template.

3. File Name: This will be the name of the file that you will create using this extension.

Now you have a couple of options on how you can set the File Name. You can either give it a static name, and then every file that you’ll create will have the same name. Or you can make it dynamic such that every time you choose this template to create a file, you can give it any name that you want.

To do that, you need to enter the following into the File Name field

${NAME}anyStringThatYouWantToAddToTheFileName

The NAME here is a custom variable. So whenever you create a file, the IDE will ask you to enter the value for this variable or any other variable first before creating this file. You can use this variable and any number of such variables inside the File as well, all you need to do is to follow this format ${VARIABLE_NAME}.

Now in the empty text area below, you can enter any code, instructions that you want. You can use the VARIABLES to make it more dynamic and reusable. One such example is below:

You can see how the NAME variable is being used inside the code by suffixing and prefixing it with strings and line of code.

All you need to do now is click on Apply and your template will be saved. You can now create a File using this template by simply right clicking on any one of the project folders in the left side of the IDE, clicking on new and find your template at the bottom somewhere as you can see below:

Click on your template and your file will be created. If you have added some variables in the template, then a Dialog will appear asking for its value, before creating the file.

……there’s more!!

We can create multiple files at once, remember!!

Create multiple Files

Go to the Edit Templates window again. This time select your recently created template. Now in the top right corner of the window, below the Files Tab and next to the + icon, you will find the icon to add file.

Once you click this icon, the same window will appear as before, when we were creating the main template file.
Follow the same process again as you did before. Also, you can use the same variables here as you might have used in the first file template. That way, you’ll only have to enter the value once. You can create multiple files within the same template by repeating the steps.

1. Go to Edit Templates

2. Select your template.

3. Click on Add File icon.

4. Enter the details and content for the file and click on Apply.

Now when you start creating a File using your New template, all the other files will be created in same folder automatically.

P.s: Does anyone know if there is a way we can create Files in multiple folders?? Let me know if there is.

Let me know if anything needs to be corrected as well.

At the end, please give a clap if you learned something.

--

--

Muheeb Mehraj
Muheeb Mehraj

No responses yet