Boost Development: Custom Android Templates

Every time when we work on a new project, we have to write a lot of code repeatedly and over a period of time, it becomes very tedious. I experienced it while creating an Android Application where I had to write too much boilerplate code whenever I created an Activity or Fragment. Not only it takes time, but also causes frustration to the developer.To overcome that, Android Studio has some inbuilt templates which are also helpful when we start working on any application. For example, when we create Activity or Fragment, we just need to write the name of the class and that’s it. Android Studio will write some necessary snippets for us.The solution to this problem is provided by the Android Studio, i.e Custom Templates. In this blog, we will take the example of the RecyclerView adapter and will create a template. Next time when we will create RecyclerView and want to create an adapter, we don’t need to write the boilerplate code again. This will surely help us in speeding up our development.

So what is a Template?

Templates are the files that have some boilerplate code. For example, when we create Activity, Service, Fragment from the list of predefined options, we see a lot of boilerplate code already written from the predefined templates.

Let’s create our own live template of creating RecyclerView adapter with inner ViewHolder class with the below mentioned steps-

      1. Right click on the package folder and then select New->Edit File Templates
      2. Click + button to create a new template. You can name this template as you like.
      3. Paste the following code in this file.
      4. ${<VARIABLE_NAME>} is used to create variables in the templates. When you use this template, there will be a prompt asking to enter values for them to create code.
      5. The #if directive is used to check that package is empty or not. It adds the name passed as the ${PACKAGE_NAME} variable for the non-empty package.
      6. The #parse directive is used to insert the contents of another template.
      7. Now again click on package folder and then select New and you will be able to see your template there. Clicking on that template will open a prompt box to fill the values defined in the above variable.

    Isn’t that cool? We can also create similar templates and can use anywhere in the project to avoid the boilerplate code. Android Studio is a very powerful tool and features like templates are really helpful in speeding up the development process. These templates are really helpful if you’re working with other team members so that all of them can use it to their advantage.

 

Share:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn
<b><strong>Karan Makan</strong></b>

Karan Makan

Technology Engineer and Entrepreneur. Currently working with International Clients and helping them scale their products through different ventures. With over 8 years of experience and strong background in Internet Product Management, Growth & Business Strategy.

On Key

Related Posts

Hook Up on Tinder

Since dating can be stressful, there is the possibility of humor to try to reduce tensions. In a new study published in the Proceedings of

error: Content is protected !!