Custom ADT templates

Recommended for you: Get network issues from WhatsUp Gold. Not end users.


The ADT template is used when the wizard to create a new Android project or Android components using the template in Eclipse. Characteristics of ADT template:

1 can be obtained by simple visual configuration [later called UI parameters] to generate Android code and resources sample
2 integrated into Eclipse ADT
3.FreeMarker driver


The default template Android

SDK after the download is complete, generally with the part of the template, template location:

1 $your_android_sdk_dir/tools/templates

The template type:  
1.Android Application Templates 
This template is the new Android project wizard [use Eclipse including Android project, the template type using lib project and test project]
2.Android Activity Templates 
A template type using this apparently is the use of Eclipse new Android Activity Wizard
3.Android Object Templates 
This use is to create some other Android components wizard when some template [File -> New -> Other -> Android/Android Object can open this wizard

The Activity sample

Open a new Android Activity wizard, ADT will first list some optional plug-in templates let us choose, as follows:

We'll open the $your_android_sdk_dir/tools/templates/activities folder, will find just and wizard choose one one corresponding, but it should be pointed out, the template folder name is not the template name, here is exactly the same.
As for the basic situation of other Application Templates and Object Templates are the same.


The workflow template


The form template

The combination of Activity templates to slightly explain. Before the start, in addition to the necessary Eclipse + ADT plugin + Android SDK, we need an auxiliary tool-FreeMarker IDE 
FreeMarker IDE is a Eclipse Plug-in, the installation process is introduced in the FreeMarker website.  

The official template is the best data, in order to avoid damage to the original template, we create a template engineering:  
File -> New ->  Project -> General/Project: 
The new project Xe_CustomActivity based on SDK templates, and then copy the BlankActivity folder contents to a new project, so it can be used directly in eclipse.  

We repeat the above use the wizard to create the Activity step, will find that there are two BlankActivity, one is SDK own, one is that we just created, this shows once again that the name and the name of the template folder.  

We check to see if the new project directory structure roughly:  

01 project_name:

02 ...root

03 ......AndroidManifest.xml.ftl

04 ......res

05 .........layout

06 ............*.ftl/*.*

07 ......src

08 .........app_package

09 ............*.ftl/*.*

10 ...template.xml

11 ...recipe.xml.ftl

12 ...globals.xml.ftl

13 ...*.png

Side note:.Ftl says the FreeMarker template language

File description



Can be said to be the template definition, basic structure of the flow process framework template:

01 <?xml version="1.0"?>

02 <template

03     format="3"

04     revision="2"

05     name="Blank Activity" <!-- The template name displayed in the wizard -->

06     description="Creates a new blank activity, with an action bar and optional navigational elements such as tabs or horizontal swipe.">

07     <dependency name="android-support-v4" revision="8" />

08     <category value="Activities" /> <!-- A template type ->

09     <parameter

10         id="activityClass" <!-- The parameter name, in the FTL file can be obtained with ${activityClass} parameter value>

11         name="Activity Name" <!-- UI interface input box before the tag value>

12         type="string" <!-- Parameter type -->

13         constraints="class|unique|nonempty" <!-- Constraint parameter values, this constraint is must be a class name, unique, non empty -->

14         suggest="${layoutToActivity(layoutName)}" <!-- Automatic prompt, for example, can be automatically generated activityClass - layout = input>

15         default="MainActivity" <!--The default value>

16         help="The name of the activity class to create" /> <!-- Help text Wizard dialog box at the bottom of the -->

17     <thumbs>

18         <thumb>template_blank_activity.png</thumb>

19         <thumb navType="none">template_blank_activity.png</thumb>

20         <thumb navType="tabs">template_blank_activity_tabs.png</thumb>

21         <thumb navType="tabs_pager">template_blank_activity_tabs_pager.png</thumb>

22         <thumb navType="pager_strip">template_blank_activity_pager.png</thumb>

23         <thumb navType="dropdown">template_blank_activity_dropdown.png</thumb>

24     </thumbs>


26     <globals file="globals.xml.ftl" />

27     <execute file="recipe.xml.ftl" />


29 </template>


Several important node:

categoryNode: presentation template type, optional value includes three:
1.Applications Android Application Templates
2.Activities Android Activities Templates
3.UI Component Android Object Templates to those with UI component template, so this kind of similar Service no interface component template is not this node.

parameterNode: definition of the parameter input graphic user configuration interface.
Parameter types defined by the type attribute of the parameter node, common types are:
string-Performance for the input box
boolean-Performance for the checkbox
enum-Performance for the drop-down selection box

thumbsNode: define static Preview.

In contrast to the Activity wizard can easily know the meaning of every node:

<globals file="globals.xml.ftl" />Is the global variable definition of project involved.
<execute file="recipe.xml.ftl" />That started the implementation of template rendering.
Therefore, the structure and function of template.xml can be described as:

The purpose of this document is only one, is to provide the global variable [Global Values], a simple example:

1 <global id="resOut" value="res" />

2 <global id="menuName" value="${classToResource(activityClass)}" />

Method references to other files in the ${resOut} and ${menuName}

Menu template name, very image, the definition of process steps, a typical recipe.xml.ftl file:

01 <?xml version="1.0"?>

02 <recipe>

03     <merge from="AndroidManifest.xml.ftl" />


05     <copy from="res/values-v14/styles_ics.xml"

06             to="res/values-v14/styles.xml" />


08     <instantiate from="res/menu/main.xml.ftl"

09             to="res/menu/${menuName}.xml" />


11     <open file="res/layout/${layoutName}.xml" />

12 </recipe>

You can see that the recipe.xml.ftl uses many variables [the] called template variables, then these variables from those places? Mainly from two aspects:  
1.UI Parameters 
2.Global Values 

A template variable data flow


Customized your ADT template, simple to use

1 we will all templates help tips were replaced Chinese, in the template.xml file, modify the value of description is the main attribute:

1 <parameter

2     id="activityClass"

3     name="The Activity name"

4     type="string"

5     constraints="class|unique|nonempty"

6     suggest="${layoutToActivity(layoutName)}"

7     default="MainActivity"

8     help="The Activity class name" />

2 I will not directly using the Android default titlebar, which will define a TextView to customize the title, so I want to add a Page Title configuration in the wizard, in the template.xml to add the following content:  

1 <parameter

2     id="pageTitle"

3     name="My Page Title"

4     type="string"

5     constraints="nonempty"

6     default="The default header"

7     suggest="${activityClass}_page_title"

8     help="Custom page title" />

In the default activity layout file [can add a TextView root/res/layout/activity_simple.xml.ftl]

1 <TextView

2      android:background="#ff5500"

3     android:layout_width="match_parent"

4     android:layout_height="wrap_content"

5     android:text="${pageTitle}" /> will refer to some other libraries, the library will usually with lots of activity layout file, in order to and your layout file to separate, so I usually in front of your layout file to add a prefix, so to modify:  
(1)Define a global variable prefix

(2)Modify the corresponding name in the template.xml and recipe.xml.ftl respectively


1 <global id="xe_prefix" value="xe" />


1 <instantiate from="res/menu/main.xml.ftl"

2     to="${resOut}/menu/${xe_prefix}_${menuName}.xml" />

3 <instantiate from="res/layout/activity_simple.xml.ftl"

4     to="${resOut}/layout/${xe_prefix}_${layoutName}.xml" />


1 @Override

2 protected void onCreate(Bundle savedInstanceState) {

3     super.onCreate(savedInstanceState);

4     setContentView(R.layout.${xe_prefix}_${layoutName});

5     <#if parentActivityClass != "">

6     // Show the Up button in the action bar.

7     setupActionBar();

8     </#if>

9 }


The original address

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Pearl at June 21, 2014 - 3:54 AM