Android RecyclerView — Creating a Custom File Template

Before you read this article, I’d highly recommend you to read my previous article here about RecyclerViews if you haven’t already.

If you’ve ever dealt with a RecyclerView, you’ll know that writing an Adapter and a ViewHolder involves a lot of boilerplate code that may be tedious and time-consuming.

Well not anymore. Here is a custom file template that will help you build an adapter, an interface, and a view holder together when you create a model.

Let’s have a look at what File Templates are before we start building our own. The default contents for new files that you create are specified in file templates. Templates contain starting code and formatting anticipated in all files of that type, depending on the type of file you’re generating (according to industry or language standards, your corporate policy, or for other reasons).

File and code templates use the Velocity Template Language (VTL), which includes the following constructs:

  • Fixed text (markup, code, comments, and so on), which is rendered as-is.
  • Variables, which are replaced by their values.
  • Various directives, including #parse, #set, #if, and others.

The following example shows the default template for creating a Java class in Android Studio:

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
public class ${NAME} {
}

In this template:

  • The #if directive checks whether the package name is not empty, and if so, adds the name to the package statement passed as the ${PACKAGE_NAME} variable.
  • The #parse directive inserts the contents of another template named File Header.java.
  • Then the template declares a public class with the name passed as the ${NAME} variable (name of the new file).

We will be using ViewBinding in our template so make sure you enable it in your build.gradle(Module) file.

...
android {
...
viewBinding {
enabled = true
}
}

Create a new file template

  1. Open your IDE settings and select Editor | File and Code Templates.
  2. On the Files tab, click +, and specify the name, file extension, and body of the template.

You may give it whatever name you wish as long as the extension is kt.

To make a click listener interface, go to the toolbar's add file icon (next to the + button) and type in the following:

File Name — ${NAME}OnClickListener
Extension-
kt

Similarly, to create an adapter, specify the following :
File Name — ${NAME}RecyclerAdapter
Extension-kt

and lastly, for the view holder, specify the following
File Name — ${NAME}ViewHolder
Extension-kt

Using the Template

To use this template, you simply have to go to new and search for Recycler Item and then type in the name of the model.

Because our template uses view binding, we’ll need to create a layout file with the same name as R.layout.item_[name] in order to have the same binding class name in the template. Instead of creating a binding class name by default, you may customise the template to accept one.

Conclusion

Custom File Templates can help you save time by reducing the amount of boilerplate code you have to write. You may learn more about file templates by clicking here.

--

--