Posted on

Save your time with Android Templates


Android Templates

If you use Data Binding library in every new project you create and sick of repeating yourself by writing the same boiler plate code again and again and wander why it’s not enabled by default, then you’re like me and today I will talk about how to make IDE do the work for you!

This is not an introduction to FreeMarker or Android File Templates, it’s rather a quick how-to-guide. If you haven’t heard of FreeMarker, it’s a template engine, Android Studio uses it and we will work with it now.

Main steps

So let’s get started. Open your AndroidStudio/plugins/android/lib/templates directory, here they are. There’re 3 steps we need to work on: Modifying gradle.build, layouts.xml, YourActivity.java.

Gradle.build

So there’s bunch of different templates in Android Studio. But we are going to change two of them: Module Template and Activity Template. In this section we’re modifying a Module Template. There are few folders in the templates directory. The ones we need to focus now are activities and gradle-projects. Activities directory responsible for Activity Templates and gradle-projects for Project and Module Templates. Go to gradle-projects/NewAndroidModule. That’s a Module Template used in here

Android Templates

and when you click new/module or here, on a second step of Creating new project

Android Templates

Ok, back to work. You’re in AndroidStudio/plugins/android/lib/templates/gradle-projects/NewAndroidModule now,  let’s open /root/build.gradle.ftl and add

There’s lot’s of code and your editor might not pick the colors, I inserted it right after

You can find it and add at the next line. Oh, and if you use VectorDrawableCompat then you can add

for Gradle Plugin 2.0+. Now when you create a new Android project you don’t need to go to build.gradle and add dataBinding enabled sync then each time. Saves lot’s of time already, isn’t it? And actually you you kill two rabbits with one shot – you save time and cognitive energy. Cognitive energy? What’s that? I suggest you view this video then, but let’s keep going with our templates, shall we?

Layouts.xml

Now since we enabled Data Binding the next step is to wrap all XML layouts with layout tag, which is too mechanical for me if you ask, why not ask IDE to do that?

Those layouts differ from each Activity Templates that we talked about before. So basically we’ll have to modify each layout for those Activity Templates

Android Templates

Let’s work on Basic Activity template now and then apply the same principle for the rest.  Go back in directories to templates folder.  There’re some commonly used layouts though, Basic Activity and Empty Activity templates use those. Go to templates/activities/common/root/res/layout. There’s two files: app_bar.xml.ftl will be renamed to activity_name.xml if you choose Basic Activity template and simple.xml.ftl will be content_name.xml if you don’t use a Fragment

So wrap app_bar.xml.ftl and simple.xml.ftl with

If you choose to create Basic Activity with a Fragment, then go to templates/activities/BasicActivity/root/res/layout and wrap those two files with <layout> as well

YourActivity.java

Alright, we’re almost done, the last thing to do is changing Java Activity class. Since we work on Basic Activity Template now, open templates/activities/BasicActivity/root/src/app_package/SimpleActivity.java.ftl’ that’s your MainActivity template. If you haven’t used FreeMarker, that’s OK, we don’t need much code and it’s pretty intuitive. Replace your onCreate method with this

The key thing in here is

${layoutName} returns main_activity_that_this (whatever name you entered) and as you know the Data Binding generated class is in camel-case, has no underscores. So using this simple syntax we first separate name into different words, capitalize them and then make it a one word.

Imports

Now just add imports on top

Now to import Binding class you need to add

But applicationPackage could be null, so we wrap it with if statement 

There’s also packageName filed, but it gives you all the nested directories as well like com.example.appname.ui.data and that’s not where Data Binding generated class would be.

Base Activity Template using Fragment

And if you want your fragment to use Data Binding by default as well – change SimpleActivityFragment.ftl’s onCreteView() method to 

and add these imports

Then you’ll be able to access views inside content_main.xml with binding.content.myView

Automate RecyclerView

Now let me ask this question: how often do you use RecyclerView in your projects? Pretty often, right? It takes a while to set everything up, but there’s a Fragment template called List Fragment where it generates everything for you: Fragment class, layouts with RecyclerView in it, RecyclerView Adapter and ViewHolders. But it’s not Data Binding friendly! If you read my previous post about implementing RecyclerView with Data Binding, then you have a clue how it’s done. So let’s automate it!

Layouts

Open templates/other/ListFragment./root/res/layout and wrap both files with <layout> Also if you want to use variables in layout, then make item_list_content.xml look like this

List_Fragment.java template

Done with layouts, now let’s go to Java classes. Open templates/other/ListFragment/root/src/app_package/List_Fragment.java.ftl and replace onCreateView() with

and add imports on top just like we did before for DataBindingUtil, but generated class import should be this, the difference is fragment_layout_list – that’s a list layout variable name saved when you were setting a layout name. If you use a wrong name it will crash your IDE

RecyclerViewAdapter.java template

Let’s get to RecyclerViewAdapter.java.ftl.  Replace onCreateView() with

onBindViewHolder() with

ViewHolder class with

and add DataBindingUtils import like before and this import

If you made this far, congratulations! Now your Base Activity and List Fragment templates are going to be with Data Binding enabled by default. Are you exited about the possibilities that you’ve learned in last 10 minutes and want to implement it all, but too lazy? I get you, that’s why I have this repo which you can download and replace your templates folder files with mine, just don’t forget to back up yours first!

Thanks for reading, hope you find it useful. Don’t forget to subscribe to my newsletter!