Lines Matching full:code
53 <p>This document describes the format and syntax for Android code templates. These templates provide starting points for entire projects (e.g. <code>NewAndroidApplication</code>) or application components such as activities (e.g. <code>BlankActivity</code>).</p>
57 <p>Templates are customizable. Each template exposes several options (called parameters) that allow developers to customize the generated code. The most common workflow for <em>using</em> a template is as follows:</p>
67 <p>Templates make heavy use of <a href="http://freemarker.sourceforge.net/">FreeMarker</a>, a Java templating engine used to enable things like control flows and variable substitutions inside files. It's similar to PHP, Django templates, etc. For those more acquainted with C/C++, think of it as a <a href="http://en.wikipedia.org/wiki/C_preprocessor">preprocessor</a> language (i.e. <code>#ifdef</code>).</p>
69 <p>By convention, any file in the template directory structure that is to be processed by FreeMarker should have the <code>.ftl</code> file extension. So if one of your source files is <code>MyActivity.java</code>, and it contains FreeMarker instructions, it should be named something like <code>MyActivity.java.ftl</code>.</p>
73 <p>An example, templated version of an Android manifest, normally named <code>AndroidManifest.xml.ftl</code> is shown below.</p>
96 <p>In this example excerpt from the <code>BlankActivity</code> template:</p>
99 <li>The expression <code>${activityClass}</code> is bound to the value of the 'Activity Class' template parameter.</li>
100 <li>The expression <code>${activityToLayout(activityClass)}</code> uses the <code>activityToLayout</code> method built into the template engine to convert an activity class such as <code>MyFooActivity</code> to <code>activity_my_foo</code>.</li>
101 <li>The <code>isLauncher</code> boolean variable and <code>parentActivityClass</code> string variables are bound to the values of the 'Launcher Activity' and 'Hierarchical Parent' template parameter, respectively.</li>
110 <p>A template is a directory containing a number of XML and FreeMarker files. The only two mandatory files are <code>template.xml</code> and <code>recipe.xml.ftl</code>. Template source files (PNG files, templated Java and XML files, etc.) belong in a <code>root/</code> subdirectory. An example directory structure for a template is below:</p>
140 <p>Each template directory must contain a <code>template.xml</code> file. This XML file contains metadata about the template, including the name, description, category and user-visible parameters that the IDE will present as options to the user. The XML file also indicates the name of the <a href="#toc_recipexmlftl">recipe XML file</a> (which gets processed by FreeMarker), and the <a href="#toc_globalsxmlftl">global variables XML file</a> if there are global variables besides the template parameter values that should be visible to all FreeMarker-processed files (<code>.ftl</code> files).</p>
142 <p>An example <code>template.xml</code> is shown below.</p>
214 <p>Below is a listing of supported tags in <code>template.xml</code>.</p>
221 <dt><code>format</code></dt>
222 <dd>The template format version that this template adheres to. Should be <code>4</code>.</dd>
224 <dt><code>revision</code></dt>
227 <dt><code>name</code></dt>
230 <dt><code>description</code></dt>
233 <dt><code>minApi</code></dt>
234 <dd>Optional. The minimum API level required for this template. The IDE will ensure that the target project has a <code>minSdkVersion</code> no lower than this value before instantiating the template.</dd>
236 <dt><code>minBuildApi</code></dt>
243 <p>This tag is deprecated for use in <code>template.xml</code>. Use <a href="#toc_recipe_dependency"><code><dependency></code></a> in <code>recipe.xml.ftl</code> instead.</p>
248 <dt><code>name</code></dt>
250 <li><code>android-support-v4</code></li>
251 <li><code>android-support-v13</code></li>
254 <dt><code>revision</code></dt>
265 <dt><code>value</code></dt>
267 <li><code>Applications</code></li>
268 <li><code>Activities</code></li>
269 <li><code>UI Components</code></li>
279 <dt><code>id</code></dt>
280 <dd>The identifier representing this variable, made available as a global variable in FreeMarker files. If the identifier is <code>foo</code>, the parameter value will be available in FreeMarker files as <code>${foo}</code>.</dd>
282 <dt><code>name</code></dt>
285 <dt><code>type</code></dt>
286 <dd>The data type of the parameter. Either <code>string</code>, <code>boolean</code>, <code>enum</code>, or <code>separator</code>.</dd>
288 <dt><code>constraints</code></dt>
289 <dd>Optional. Constraints to impose on the parameter's value. Constraints can be combined using <code>|</code>. Valid constraint types are: <ul>
290 <li><code>nonempty</code> — the value must not be empty</li>
291 <li><code>apilevel</code> — the value should represent a numeric API level</li>
292 <li><code>package</code> — the value should represent a valid Java package name</li>
293 <li><code>app_package</code> — the value should represent a valid Android app package name</li>
294 <li><code>module</code> — the value should represent a valid Module name</li>
295 <li><code>class</code> — the value should represent a valid Java class name</li>
296 <li><code>activity</code> — the value should represent a fully-qualified activity class name</li>
297 <li><code>layout</code> — the value should represent a valid layout resource name</li>
298 <li><code>drawable</code> — the value should represent a valid drawable resource name</li>
299 <li><code>string</code> — the value should represent a valid string resource name</li>
300 <li><code>id</code> — the value should represent a valid id resource name</li>
301 <li><code>unique</code> — the value must be unique; this constraint only makes sense when other constraints are specified, such as <code>layout</code>, which would mean that the value should not represent an existing layout resource name</li>
302 <li><code>exists</code> — the value must already exist; this constraint only makes sense when other constraints are specified, such as <code>layout</code>, which would mean that the value should represent an existing layout resource name</li>
305 <dt><code>suggest</code></dt>
306 <dd>Optional. A FreeMarker expression representing the auto-suggested parameter value (a 'dynamic default'). When the user modifies other parameter values, and if this parameter's value has not been changed from its default, then the value changes to the result of this expression. This may seem to be circular since parameters can <code>suggest</code> against each other's values, but these expressions are only updated for non-edited values, so this approach lets the user edit either parameter value, and the other will automatically be updated to a reasonable default.</dd>
308 <dt><code>default</code></dt>
311 <dt><code>visibility</code></dt>
314 <dt><code>help</code></dt>
321 <p>For parameters of type <code>enum</code>, represents a choice for the value.</p>
324 <dt><code>id</code></dt>
327 <dt><code>minApi</code></dt>
328 <dd>Optional. The minimum API level required if this option is chosen. The IDE will ensure that the target project has a <code>minSdkVersion</code> no lower than this value before instantiating the template.</dd>
330 <dt><code>[text]</code></dt>
336 code><thumb></code> elements should be contained inside a <code><thumbs></code> element. The text contents of this element represent the path to the thumbnail. If this element has any attributes, they will be treated as selectors for parameter values. For example, if there are two thumbnails:</p>
345 <p>The template 'preview' thumbnail will show <code>template_tabs.png</code> if the value of the <code>navType</code> template parameter is <code>tabs</code> and <code>template.png</code> otherwise.</p>
352 <dt><code>type</code></dt>
353 <dd>The type of icon wizard to create. Valid values are <code>notification</code>, <code>actionbar</code>, <code>launcher</code>.</dd>
355 <dt><code>name</code></dt>
356 <dd>The base icon name to output, e.g. <code>ic_stat_my_notification</code>.</dd>
363 <p>An example <code>globals.xml.ftl</code> is shown below.</p>
378 <p>The recipe XML file contains the individual instructions that should be executed when generating code from this template. For example, you can copy certain files or directories (the copy instruction), optionally running the source files through FreeMarker (the instantiate instruction), and ask the IDE to open a file after the code has been generated (the open instruction).</p>
380 <p class="note"><strong>Note:</strong> The name of the recipe file is up to you, and is defined in <code>template.xml</code>. By convention, however, it's best to call it <code>recipe.xml.ftl</code>.</p>
382 <p class="note"><strong>Note:</strong> The global variables in <code>globals.xml.ftl</code> are available for use in <code>recipe.xml.ftl</code>.</p>
384 <p>An example <code>recipe.xml.ftl</code> is shown below.</p>
433 <!-- Decide which activity code to add -->
461 <dt><code>mavenUrl</code></dt>
463 <code>com.android.support:appcompat-v7:+</code></dd>
468 <p>The only required argument is <code>from</code> which specifies the location of the source files to copy under the <code>root/</code> directory. All necessary ancestor directories are automatically created if needed.</p>
470 <p>The default destination location is the same path under the output directory root (i.e. the location of the destination project). If the optional <code>to</code> argument is provided, this specifies the output directory. Note that if the from path ends with <code>.ftl</code>, it will automatically be stripped. For example <code><instantiate from="res/values/strings.xml.ftl" /></code> is adequate; this will create a file named <code>strings.xml</code>, not <code>strings.xml.ftl</code>.</p>
472 <p>This argument works recursively, so if <code>from</code> is a directory, that directory is recursively copied.</p>
476 <p>Same as <code><copy></code>, but each source file is first run through FreeMarker.</p>
480 <p>This instruction will run the source file through FreeMarker and then merge the contents of the output into an existing file in the project, or create a new file. The most common use case for this is to add components to the <code>AndroidManifest.xml</code> file of the destination project, or to merge resources such as strings into an existing <code>strings.xml</code> file.</p>
484 <p>Instruct the IDE to open the file created by the specified <code>file</code> argument after code generation is complete.</p>
488 <p>Ensures the directory provided in the <code>at</code> argument exists.</p>
492 <p>The actual template files (resources, Java sources, Android Manifest changes) should be placed in the <code>root/</code> directory, in a directory structure that roughly resembles what the output directory structure should look like.</p>
494 <p>One difference is that instead of placing source files in <code>src/com/google/...</code> you can just use a naming convention like <code>src/app_package/</code> to indicate that files under this directory will be placed in the destination project's source file package root.</p>
505 <p>This function converts an activity class-like identifer string, such as <code>FooActivity</code>, to a corresponding resource-friendly identifier string, such as <code>activity_foo</code>.</p>
509 <dt><code>activityClass</code></dt>
510 <dd>The activity class name, e.g. <code>FooActivity</code> to reformat.</dd>
514 <p><a href="#toc_layouttoactivity"><code>layoutToActivity</code></a></p>
518 <p>This function converts a camel-case identifer string, such as <code>FooBar</code>, to its corresponding underscore-separated identifier string, such as <code>foo_bar</code>.</p>
522 <dt><code>camelStr</code></dt>
523 <dd>The camel-case string, e.g. <code>FooBar</code> to convert to an underscore-delimited string.</dd>
527 <p><a href="#toc_underscoretocamelcase"><code>underscoreToCamelCase</code></a></p>
531 <p>This function escapes a string, such as <code>foo=bar</code> such that it is suitable to be inserted in a Java <code>.properties</code> file as a property value, such as <code>foo\=bar</code>.</p>
535 <dt><code>str</code></dt>
536 <dd>The string, e.g. <code>foo=bar</code> to escape to a proper property value.</dd>
541 <p>This function escapes a string, such as <code>Android's</code> such that it can be used as an XML attribute value: <code>Android&apos;s</code>. In particular, it will escape ', ", < and &.</p>
545 <dt><code>str</code></dt>
550 <p><a href="#toc_escapexmltext"><code>escapeXmlText</code></a></p>
551 <p><a href="#toc_escapexmlstring"><code>escapeXmlString</code></a></p>
555 <p>This function escapes a string, such as <code>A & B's</code> such that it can be used as XML text. This means it will escape < and >, but unlike <a href="#toc_escapexmlattribute"><code>escapeXmlAttribute</code></a> it will <b>not</b> escape ' and ". In the preceeding example, it will escape the string to <code>A &amp; B\s</code>. Note that if you plan to use the XML text as the value for a <string> resource value, you should consider using <a href="#toc_escapexmlstring"><code>escapeXmlString</code></a> instead, since it performs additional escapes necessary for string resources.</p>
559 <dt><code>str</code></dt>
564 <p><a href="#toc_escapexmlattribute"><code>escapeXmlAttribute</code></a></p>
565 <p><a href="#toc_escapexmlstring"><code>escapeXmlString</code></a></p>
569 <p>This function escapes a string, such as <code>A & B's</code> such that it is suitable to be inserted in a string resource file as XML text, such as <code>A &amp; B\s</code>. In addition to escaping XML characters like < and &, it also performs additional Android specific escapes, such as escaping apostrophes with a backslash, and so on.</p>
573 <dt><code>str</code></dt>
574 <dd>The string, e.g. <code>Activity's Title</code> to escape to a proper resource XML value.</dd>
578 <p><a href="#toc_escapexmlattribute"><code>escapeXmlAttribute</code></a></p>
579 <p><a href="#toc_escapexmltext"><code>escapeXmlText</code></a></p>
587 <dt><code>str</code></dt>
593 <p>This function converts an Android class name, such as <code>FooActivity</code> or <code>FooFragment</code>, to a corresponding resource-friendly identifier string, such as <code>foo</code>, stripping the 'Activity' or 'Fragment' suffix. Currently stripped suffixes are listed below.</p>
604 <dt><code>className</code></dt>
605 <dd>The class name, e.g. <code>FooActivity</code> to reformat as an underscore-delimited string with suffixes removed.</dd>
609 <p><a href="#toc_activitytolayout"><code>activityToLayout</code></a></p>
613 <p>This function converts a resource-friendly identifer string, such as <code>activity_foo</code>, to a corresponding Java class-friendly identifier string, such as <code>FooActivity</code>.</p>
617 <dt><code>resourceName</code></dt>
618 <dd>The resource name, e.g. <code>activity_foo</code> to reformat.</dd>
622 <p><a href="#toc_activitytolayout"><code>activityToLayout</code></a></p>
626 <p>This function converts a full Java package name to its corresponding directory path. For example, if the given argument is <code>com.example.foo</code>, the return value will be <code>com/example/foo</code>.</p>
630 <dt><code>packageName</code></dt>
631 <dd>The package name to reformat, e.g. <code>com.example.foo</code>.</dd>
636 <p>This function converts an underscore-delimited string, such as <code>foo_bar</code>, to its corresponding camel-case string, such as <code>FooBar</code>.</p>
640 <dt><code>underStr</code></dt>
641 <dd>The underscore-delimited string, e.g. <code>foo_bar</code> to convert to a camel-case string.</dd>
645 <p><a href="#toc_camelcasetounderscore"><code>camelCaseToUnderscore</code></a></p>
655 <p>The Java-style Android package name for the project, e.g. <code>com.example.foo</code></p>
667 <p>The minimum API level the project supports. Note that this value could be a string so consider using <a href="#toc_minapilevel"><code>minApiLevel</code></a> instead.</p>
671 code based on the project's API level, for example:</p>
679 <p>The API level that the project is building against, guaranteed to be a number. This is generally used to guard the generation of code based on what version of the Android SDK the project is being built against, for example:</p>
688 <p>The target output directory for the <code>AndroidManifest.xml</code> file. This varies depending on the project's directory structure (Gradle-style or Ant-style).</p>
699 <p><a href="#toc_slashedpackagename"><code>slashedPackageName</code></a></p>
703 <p>The target resource directory root (<code>res/</code> folder) for the project. This varies depending on the project's directory structure (Gradle-style or Ant-style).</p>
713 <p>When creating activity layouts, make sure to include the activity name in the root view in the layout as part of the <code>tools</code> namespace, as shown in the following example:</p>