How templates can save your time?

What is boiler code?

In computer programming, boilerplate code or boilerplate is the sections of code that have to be included in many places with little or no alteration.

- en.wikipedia.org

What are templates?

Live templates let you insert frequently-used or custom code constructs into your source code file quickly, efficiently, and accurately. They contain predefined code fragments.

- jetbrains.com

Tutorial

Let's tale a look how to create live template for Parcelable.Creator object.

I. Open Project Settings (Ctrl + Alt + S) and navigate to Live Templates category.

II. Click on Add (green plus sign) and select Template Group. Enter group name Android and press Ok.

III. Select just created Android template category and click on Add (green plus sign) and select Live Template.

IV. Click on Define button, next to No applicable context yet red text and select Java - Declaration.

V. Fill Abbreviation and Template text

Abbreviation

creator  

Template text

public static final Parcelable.Creator<$TYPE$> CREATOR = new Parcelable.Creator<$TYPE$>() {

    @Override
    public $TYPE$ createFromParcel(Parcel in) {
        return new $TYPE$(in);
    }

    @Override
    public $TYPE$[] newArray(int size) {
        return new $TYPE$[size];
    }
};

public $TYPE$(Parcel parcel) {  
  // parcel.readInt();
}

VI. In Template text we have a variable - $TYPE$, click on Edit variables button and change it as on image below.

VII. Press Ok and open your code editor. Now type creator and press Ctrl + J. This will open live template pop-up. When you press Enter template will be generated.

Other samples

Find View

Gson

Hope you enjoy this little article.

comments powered by Disqus