1. Java Rosa
  2. javarosa

Wiki

Clone wiki

javarosa / ITextAPI

How To Use IText and IText special forms

The text in this document refers to the example xform at the bottom of the page.

You need to access the instance of FormEntryPrompt (or FormEntryCaption) being used by your application then simply call getQuestionText();.

So for example, if the FormEntryController was on the first question in the xform below and you wanted to retrieve the Itext relevant to the question you might use something like this:

String foo = fep.getQuestionText();  //this method first attempts to retrieve the 'long' form then tries for the default form.

In our example, foo would now contain the long form of the first question ("Patient ID").

If you want to retrieve a specific short form (such as say audio) for a question, do:

foo = fep.getSpecialFormQuestionText("audio");

It's possible to make this call for any textID (by using getSpecialFormQuestionText(TextID,form), instead of for the current textID.

Similarly, for items in a single or multi-select, use (assume the FormEntryController is currently at the select1 question in the example xform below)

String itemOneText = fep.getSelectChoiceText(ItemOne);  //where ItemOne is the first SelectChoice item
String itemOneImageText = fep.getSpecialFormSelectChoiceText(ItemOne, "image");

//now itemOneText will = "Icon 4"
//and itemOneImageText = "jr://images/four.gif"

Notes: Generally, these methods will return null if the specified itext type is not found. When attempting to retrieve default text, use methods getQuestionText() or getSelectChoiceText(). These have the following fallback strategy: try get 'long' text, try get default itext, try get labelInnerText, if all fails return null. Bear in mind that "" is a legitimate value that can and will be returned if found during the fallback operation.

Please see the JavaDocs for more in depth information. Convenience methods such as getAudioText() also exist, feel free to use them!

<h:html xmlns:h="http://www.w3.org/1999/xhtml"
        xmlns="http://www.w3.org/2002/xforms"
	  xmlns:ev="http://www.w3.org/2001/xml-events"
	  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	  xmlns:jr="http://openrosa.org/javarosa">
<h:head>
    <h:title>RichMedia testing Images</h:title>
    <meta jr:name="rm-subforms-test-images"/>
    <model>
		<instance>
			<icons>
				<id />
				<name />
				<find-mirc />
				<non-local />
				<consTest />
			</icons>
		</instance>

		<bind nodeset="/icons/name" required="true()" />
		<bind nodeset="/icons/find-mirc" required="true()" />
		<bind nodeset="/icons/consTest" type="xsd:int" constraint=". > 10" />
		
		<itext>
			<translation lang="English" default="">
				<text id="id">
				  <value form="long">Patient ID</value>
				  <value form="short">ID</value>
				  <value form="audio">jr://audio/hah.mp3</value>
				</text>
				<text id="name">
				  	<value>Full Name</value>
					<value form="short">Name</value>
					<value form="image">jr://images/four.gif</value>
				</text>
				<text id="find-mirc">
					<value form="long">Please find the mirc icon</value>
					<value form="short">MircIcon</value>
				</text>
				<text id="pandora">
					<value form="image">jr://images/four.gif</value>
					<value form="long">Icon 4</value>
					<value form="short">AltText</value>
				</text>
				<text id="mirc">
					<value form="image">jr://images/three.gif</value>
					<value form="long">Icon 3</value>
					<value form="short">AltText</value>
				</text>
				<text id="gmail">
					<value form="image">jr://images/two.gif</value>
					<value form="long">Icon 2</value>
					<value form="short">AltText</value>
				</text>
				<text id="powerpoint">
					<value form="image">jr://images/one.gif</value>
					<value form="long">Icon 1</value>
					<value form="short">AltText</value>
				</text>	
				<text id="constraint-test">
					<value>Should Be Less than 10</value>
				</text>
			</translation>

		</itext>
	
	</model>
</h:head>
<h:body>
	<input ref="id"><label ref="jr:itext('id')" /></input>
	<input ref="name"><label ref="jr:itext('name')" /></input>
	<select1 ref="find-mirc">
		<label ref="jr:itext('find-mirc')" />
		<item><label ref="jr:itext('pandora')"/><value>pand</value></item>
		<item><label ref="jr:itext('mirc')" /><value>mirc</value></item>
		<item><label ref="jr:itext('powerpoint')" /><value>powerp</value></item>
		<item><label ref="jr:itext('gmail')" /><value>gmail</value></item>
		<item><label>Non-localized select text item label</label><value>other</value></item>
	</select1>
	<input ref="non-local"><label>Non-Localized label inner text!</label></input>
	<input ref="consTest"><label ref="jr:itext('constraint-test')" /></input>
</h:body>
</h:html>

Updated