#TECH

Android custom templates to boost your development

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.

android_studio

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 snippet for us.

The below is a common structure of Fragment which many of us might be using since a long time-

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rssitem_detail,
                container, false);
        return view;
    }
}

Now, writing this for every module, for every functionality is a test of the developer’s patience.

The solution to this problem is provided by the Android Studio, i.e Custom Templates. In this blog, we will take the example of 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.

    #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
    
    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import java.util.List;
    
    #parse("File Header.java")
    public class ${NAME} extends RecyclerView.Adapter<${VIEWHOLDER_CLASS}> {
        private final Context context;
        private List<${ITEM_CLASS}> items;
       
        public ${NAME}(List<${ITEM_CLASS}> items, Context context) {
            this.items = items;
            this.context = context;
        }
    
        @Override
        public ${VIEWHOLDER_CLASS} onCreateViewHolder(ViewGroup parent,
                                                 int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.${LAYOUT_RES_ID}, parent, false);
            return new ${VIEWHOLDER_CLASS}(v);
        }
    
        @Override
        public void onBindViewHolder(${VIEWHOLDER_CLASS} holder, int position) {
            ${ITEM_CLASS} item = items.get(position);
            holder.set(item);
        }
    
        @Override
        public int getItemCount() {
            if (items == null){
                return 0;
            }
            return items.size();
        }
        
        public class ${VIEWHOLDER_CLASS} extends RecyclerView.ViewHolder {
    
            public ${VIEWHOLDER_CLASS}(View itemView) {
                super(itemView);
            }
    
            public void set(${ITEM_CLASS} item) {
                //UI setting code
            }
        }
     }

  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.
  8. Here is the generated code after filling all the information.

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class EmployeeData extends RecyclerView.Adapter<EmployeeData> {
    private final Context context;
    private List<EmpItems> items;

    public EmployeeData(List<EmpItems> items, Context context) {
        this.items = items;
        this.context = context;
    }

    @Override
    public EmployeeData onCreateViewHolder(ViewGroup parent,
                                         int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.R.layout.item_emp, parent, false);
        return new EmployeeData(v);
    }

    @Override
    public void onBindViewHolder(EmployeeData holder, int position) {
        EmpItems item = items.get(position);
        holder.set(item);
    }

    @Override
    public int getItemCount() {
        if (items == null) {
            return 0;
        }
        return items.size();
    }

    public class EmployeeData extends RecyclerView.ViewHolder {

        public EmployeeData(View itemView) {
            super(itemView);
        }

        public void set(EmpItems item) {
            //UI setting code
        }
    }
}


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.

I hope this helped you in understanding how android custom templates work. Let me know in the comments below if you face any difficulties in implementing the above code.

You might also like