Kotlin File Template for RecyclerView Adapter

Shalauddin Ahamad Shuza
MindOrks
Published in
3 min readApr 3, 2018

Android studio is extremely powerful with a lot of tricks under it’s sleeves which are waiting to be explored. File template is one of them. What is file template? A file template is a source that already contains some code to get you started.

In this article, we are going to create a file template for RecyclerView Adapter implementation which is something I often have to look up when trying to create it.

Steps to add RecyclerView Adapter file template

Press Ctrl+Shift+A (windows) or Cmd+Shift+A (Mac) to open action window and type “Add Template”

Click “Plus icon” (+)

Give some name to the template (I have given RecyclverView Adapter) and make sure you have given the “kt” Extension

Then we will be able to write your template code in the code section (below the name). There are couple of variables which we can use to refer file name, package name, date and many more. Some of them are

${NAME} is the file name that we have given at the time of file creation

${PACKAGE_NAME} is the package name

${DATE} the current date of system

${PROJECT_NAME} is the name of current project

${USER} is currently user system login name

We can also define any custom variable that we will assign at the time of creation of our custom file. Here we will use ${Model_Class} to refer the model class and ${Item_Layout_ID} to refer the XML file for view holder

Now paste the following code and click “Ok”

Here I have created an inner class for the view holder and an interface to get the callback of item click.

Create RecyclerView Adapter

Now our RecyclerView Adapter file template is ready to use. Open new file dialog and we will see the file template (for me it is “RecyclerView Adapter”)

Just enter all the details

That’s it. Now we can get ride of writing all the boilerplate code again and again. Happy Coding.

--

--