If you are lazy like me, then you are genious! One of the wealthiest people in the world, Bill Gates, said “I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” If Bill said this, it must be true! Except if it has something to do with Windows Vista. So being lazy is natural, we always try to accomplish our job faster and with minimal effort. As an Android developer, I often use templates. Templates are cool because you don’t need to write the same lines of code a thousand times and you can make all MVP structures generable. But let’s go step by step. First we will make a simple ViewPager adapter template and RecyclerView adapter template. Before we start check how to make Android application like professional. So let’s get started.

Templates basics

For creating new templates, right click on the source code folder and select “New”, then select “Edit File Templates”. 

Then you will see a window like in picture below:

Here you can see some predefined templates but we don’t need this so click on “+” icon in top left corner. After this action your screen should look like this.

In the field called “Name:” enter a name for your template. Name should be “ViewPagerTemplate”.

Now we are ready for some fancy template coding in the big area below template name. For defining some custom variable, write ${VARIABLE NAME}. Entry for variable name will be displayed during class creation, alse here are some predefined ones: ${PACKAGE_NAME}, ${NAME}, etc.

Example 1.

In this example we will use only predefine variables. ViewPagerAdapter template code is shown below:

package ${PACKAGE_NAME};
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.ArrayList;
public class ${NAME} extends FragmentStatePagerAdapter {
private ArrayList fragmentArrayList = new ArrayList();
public ${NAME}(FragmentManager fm, ArrayList fragmentArrayList) {
super(fm);
this.fragmentArrayList = fragmentArrayList;
}
@Override
public Fragment getItem(int position) {
return fragmentArrayList.get(position);
}
@Override
public int getCount() {
return fragmentArrayList.size();
}
}

Example 2.

In second example shown below, we will be using custom and predefined variables.

package ${PACKAGE_NAME};
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
#parse("File Header.java")
public class ${NAME} extends RecyclerView.Adapter<${NAME}.${VIEW_HOLDER_CLASS}>{
private List<${ITEM_TYPE}> dataList = new ArrayList<>();
@Override
public ${VIEW_HOLDER_CLASS} onCreateViewHolder(ViewGroup parent, int viewType) {
return new ${VIEW_HOLDER_CLASS}(LayoutInflater.from(parent.getContext()).
inflate(${LAYOUT}, parent, false));
}
@Override
public void onBindViewHolder(${NAME}.${VIEW_HOLDER_CLASS} holder, int position) {
}
@Override
public int getItemCount() {
return dataList.size();
}
public void addItems(List<${ITEM_TYPE}> dataList){
this.dataList.addAll(dataList);
notifyDataSetChanged();
}
class ${VIEW_HOLDER_CLASS} extends RecyclerView.ViewHolder {
${VIEW_HOLDER_CLASS}(View itemView) {
super(itemView);
}
}
}

As shown in these two examples, creating templates is easy. You only need to worry about where to put the custom or predefined variables.

How to use Templates?

First select the package where you want to generate your template code. Then choose “New” and select a template.

After selecting a template, you have one more step – writing variable entries.

As you can see, we need to set entries for every custom and some predefine variables. For class generating simply hit “OK”.

Conclusion

Creating templates is easy and saves a lot of time, but if you are a junior Android developer don’t use templates until you learn what those generated class do. In the next blog post, I will show you how to make group templates for MVP (Dagger 2 included).

I hope you found this blog post interesting, till next time!