
Templates:
Templates are prewritten modules that can be used for repeated use. You can create code templates with the help of Android design studio to get elements that follow the Android design & development standards. This makes the process of Android app development easy. In this post, we are going to see the step by step process of creating a template with the help of Android Studio.
For creating new templates:
In the Android studio.
Right click on the source code folder and select “New”, then select “Edit File Templates”.
Then you will see a window like in the picture below:
Here you can see some predefined templates but we don’t need this so click on “+” icon in the top left corner. After this action, your screen should look like this.
In the field called “Name:” enter a name for your template. The 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 the variable name will be displayed during class creation, also 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:
</p> <p class="p3"><span class="s1"><i>package ${PACKAGE_NAME}; </i></span><span class="s1"><i>import android.support.v4.app.Fragment; </i></span><span class="s1"><i>import android.support.v4.app.FragmentManager; </i></span><span class="s1"><i>import android.support.v4.app.FragmentStatePagerAdapter; </i></span><span class="s1"><i>import java.util.ArrayList; </i></span><span class="s1"><i>public class ${NAME} extends FragmentStatePagerAdapter { </i></span><span class="s1"><i>private ArrayList fragmentArrayList = new ArrayList(); </i></span><span class="s1"><i>public ${NAME}(FragmentManager fm, ArrayList fragmentArrayList) { </i></span><span class="s1"><i>super(fm); </i></span><span class="s1"><i>this.fragmentArrayList = fragmentArrayList; </i></span><span class="s1"><i>} </i></span><span class="s1"><i>@Override </i></span><span class="s1"><i>public Fragment getItem(int position) { </i></span><span class="s1"><i>return fragmentArrayList.get(position); </i></span><span class="s1"><i>} </i></span><span class="s1"><i>@Override </i></span><span class="s1"><i>public int getCount() { </i></span><span class="s1"><i>return fragmentArrayList.size(); </i></span><span class="s1"><i>} </i></span><span class="s1"><i>}
Example 2.
In second example shown below, we will be using singleton class and predefined variables.
</p> <p class="p3"><span class="s1"><i>#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end </i></span><span class="s1"><i>#parse("File Header.java") </i></span><span class="s1"><i>public class ${NAME}{ </i></span><span class="s1"><i>private static ${NAME} ourInstance = new ${NAME}() </i></span><span class="s1"><i>public static ${NAME} getInstance() </i></span><span class="s1"><i>return ourInstance; </i></span><span class="s1"><i><span class="Apple-converted-space"> </span>} </i></span><span class="s1"><i><span class="Apple-converted-space"> </span>private ${NAME}() { </i></span><span class="s1"><i><span class="Apple-converted-space"> </span>} </i></span><span class="s1"><i>}</i></span></p> <p class="p3">
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 than step – writing variable entries.
Conclusion:
We have seen in detail about the creating a template in Android studio. Creating a template helps in reducing the development process considerably and also eases the coders to create extensive projects.
–
MANIKANDAN K,
Android application developer,
Mallow technologies private limited.
Nice Tutorial
Thank you !!