Layout resource

A layout resource defines the architecture for the UI in a Screen or a component of a UI.

XML file saved at:

res/
  layout/
    filename.xml

The filename will be used as the resource ID.

Syntax

<?xml version="1.0" encoding="utf-8"?>
<Layout
        id="@[+]id/id_name"
        verticalStretchMode=["MATCH_PARENT" | "MATCH_CONTENT"]
        horizontalStretchMode=["MATCH_PARENT" | "MATCH_CONTENT"]
        width="[dimension]"
        height="[dimension]"
        [Layout-specific attributes]>
        <View
                id="@[+]id/id_name"
                verticalStretchMode=["MATCH_PARENT" | "MATCH_CONTENT"]
                horizontalStretchMode=["MATCH_PARENT" | "MATCH_CONTENT"]
                width="[dimension]"
                height="[dimension]"
                [View-specific attributes] />
        <Layout>
                <View />
        </Layout>
        <Include refLayout="@layout/layout_resource"/>
</Layout>

Resource reference

To make a reference to a resource, use the following:

  • XML: @layout/layout_resource
  • Java: Res.layout.layout_resource

This XML layout includes another layout as child:

<Include refLayout="@layout/layout_resource" />

This application code retrieves a view:

View myView = ResManager.getLayout(Res.layout.layout_resource);

Elements

<Layout>

A container for other View elements. There are many different kinds of Layout objects and each one lets you specify the layout of the child elements in different ways. Different kinds of Layout objects include VerticalLayout, and FrameLayout.

attributes:

id

Resource ID. A unique resource name for the element, which you can use to obtain a reference to the View from your application.

height

Dimension or keyword. Required. The height for the group, as a dimension value or dimension resource.

width

Dimension or keyword. Required. The width for the group, as a dimension value or dimension resource

More attributes are supported by the Layout base class, and many more are supported by each implementation of Layout. For a reference of all available attributes, see the corresponding reference documentation for the Layout class (for example, the VerticalLayout XML attributes).

<View>

An individual UI component, generally referred to as a “widget”.

Different kinds of View objects include TextLabel, Button, and CheckBox.

attributes:

id

Resource ID. A unique resource name for the element, which you can use to obtain a reference to the View from your application.

More attributes are supported by the View base class, and many more are supported by each implementation of View. For a reference of all available attributes, see the corresponding reference documentation (for example, the TextLabel XML attributes).

<include>

Includes a layout file into this layout.

attributes:

refLayout

Layout resource. Required. Reference to a layout resource.

id

Resource ID. Overrides the ID given to the root view in the included layout.

For the ID value, you should usually use this syntax form: “@+id/name”. The plus symbol, +, indicates that this is a new resource ID and NeoMAD will create a new resource integer in the Res.java class, if it doesn’t already exist. For example:

<TextLabel id="@+id/nameTextbox"/>

The nameTextbox name is now a resource ID attached to this element. You can then refer to the TextView to which the ID is associated in Java:

findView(Res.id.nameTextbox);

This code returns the TextLabel object.

However, if you already have defined an ID resource (and it is not already used), then you can apply that ID to a View element by excluding the plus symbol in the id value.

Values for stretchMode:

The vertical and horizontal stretch mode value can be expressed with the following keywords:

Value Description
MATCH_PARENT Sets the dimension to match that of the parent element.
MATCH_CONTENT Sets the dimension only to the size required to fit the content of this element.