3.3 Mobile Metawidgets

Metawidget supports multiple mobile frameworks, so that you can choose the one that matches your preferred UI environment.

3.3.1 AndroidMetawidget

AndroidMetawidget is an Android View. For an introduction to AndroidMetawidget, see Section 1.3.3, “Mobile Address Book”.


There are two steps to installing AndroidMetawidget within an Android application:

  1. Add metawidget-all.jar to your CLASSPATH (i.e. under your project's Libraries tab in Eclipse)

  2. Optionally configure the Metawidget, as described below.

Given the resource constraints of a mobile device, consider creating a custom metawidget-all.jar that only includes the classes you need (as detailed in Section 9.5.1, “JAR Size”).


AndroidMetawidget is preconfigured with sensible defaults for Android. You can change this configuration programmatically or by creating a metawidget.xml file in res/raw and specifying it at the tag level:

<view class="org.metawidget.android.widget.AndroidMetawidget" config="@raw/metawidget"/>

Customizing Look and Feel

AndroidMetawidget uses Android's standard res/values/styles.xml mechanism. Styles can be set either programmatically or passed inside metawidget.xml using the @ notation. For example:

	<textViewLayoutDecorator xmlns="java:org.metawidget.android.widget.layout"


AndroidMetawidget supports localization through the setBundle method. This method takes your R.string class, which is generated by Android from your res/values/strings.xml file. For example, if your strings.xml file was:

	<string name="dob">Date of Birth<string>

And your domain object had a property:

class Person {
	public Date getDob() {
		return mDob;

Then you can set Metawidget to use your R.string class either programmatically or using metawidget.xml:


And at runtime Metawidget will translate the Person.getDob method into a property called dob, resolve the integer R.string.dob and use that to look up the localized text in strings.xml. For different locales, put the strings.xml file in adjacent res/values-xx folders - for example res/values-en.

Getting and Setting Values

By default, Android does not come with a binding mechanism to map View values to business object values. There are a number of third-party binding libraries, but many are either discontinued or rely on statically declared UIs. As a workaround, by default AndroidMetawidget supplies a SimpleBindingProcessor (see the section called “SimpleBindingProcessor”).

Alternatively, to map values manually, remove SimpleBindingProcessor and do:

metawidget.setValue( mPerson.getFirstname(), "firstname" );
mPerson.setFirstname( (String) metawidget.getValue( "firstname" ) );

For a complete working example, see Section 1.3.3, “Mobile Address Book”.

3.3.2 JQuery Mobile Metawidget

metawidget-jquerymobile.js wraps the JavaScript Metawidget into a JQuery Mobile component. For an introduction to metawidget.js, see Section 1.2, “Part 1 (JavaScript version) - The First Metawidget Application”.


There are three steps to installing metawidget-jquerymobile.js within an HTML 5 hybrid application:

  1. Add metawidget-core.min.js into /lib/metawidget/core and metawidget-jquerymobile.min.js into /lib/metawidget/jquery.mobile

  2. Add script tags to the top of your page:

    <script src="lib/metawidget/core/metawidget-core.min.js" type="text/javascript"></script>
    <script src="lib/metawidget/jquery.mobile/metawidget-jquerymobile.min.js" type="text/javascript"></script>
  3. Wrap Metawidget around a DOM element:

    $('#metawidget').metawidget('buildWidgets', toInspect);
  4. Optionally configure the Metawidget, as described below.


metawidget-jquerymobile.js is preconfigured with sensible defaults. You can change this configuration either at construction time:

$('#metawidget').metawidget( {
	layout: new metawidget.layout.SimpleLayout()
} );

Or by passing JQueryMobile options:

$('#metawidget').metawidget('option','layout',new metawidget.layout.SimpleLayout());