B4J Tutorial [B4X] Creating layouts with LayoutUtils and LayoutGenerator

LayoutUtils: https://www.b4x.com/android/forum/threads/172516/#content

LayoutUtils can be used to create new layout files without using the designer. Use cases that I can think of:

- Conversion of non-B4X layouts.
- Automatic creation of layouts. Something like B4XPreferenceDialog but based on customized layouts.
- Modification of existing layouts. A bit like: https://www.b4x.com/android/forum/threads/b4x-thememanager-themecreator.172525/#content
- AI generated layouts.

How does it work?

Each view is made of various properties. As it is difficult and cumbersome to set all properties manually, we use one or more "template" layouts. These templates hold the views, including custom views, that we then duplicate, modify and use to build a new layout.

The various modification rules can be declared with a relatively simple json file, and then be processed by LayoutUtils. LayoutGenerator is a command line tool that provides two features:
1. It can read a layout file and generate a textual description of the layout.
2. It can read a json template file + one or more template layouts and generate a new layout.

The json file includes the following parts:
  1. templates — a list of layout templates containing the views to duplicate. Each item specifies a layout file and its alias.
  2. header and root — specify the source templates for the layout header and root view. The root can also include a modifications object.
  3. views — a list of views to create, processed in order. Each view includes:
    • name — the unique name of the new view.
    • parent — the name of the parent view. Use "root" for the layout’s main view. Other parents must appear earlier in the list.
    • source — the template alias and source view name, for example "t1.Label1". To duplicate a previously created view, omit the template alias, for example "Label1".
    • horizontal and vertical — optional positioning objects. Omitted positions retain their source values.
    • modifications — an optional object containing property names and their new values. Nested properties use dot-separated paths, such as "drawable.color".
    • script — optional code appended to the general designer script.

Example of a layout (generated by ChatGPT):
JSON: Copy
{
  "templates": [
    {"file": "template.bal", "alias": "t1"}
  ],
  "header": {"source": "t1"},
  "root": {"source": "t1", "name": "root"},
  "views": [
    {
      "name": "TopPanel",
      "source": "t1.Panel1",
      "parent": "root",
      "horizontal": {"anchor": "BOTH", "left": 5, "right": 5},
      "vertical": {"anchor": "TOP", "top": 30, "height": 50},
      "modifications": {
        "drawable.borderWidth": 2,
        "drawable.borderColor": "#FF0001D0",
        "drawable.color": "#FFE8F5E9"
      },
      "script": "DDD.SpreadControlsHorizontally(TopPanel, 100dip, 5dip)"
},
    {
      "name": "Button1",
      "source": "t1.Button1",
      "parent": "TopPanel",
      "horizontal": {"anchor": "LEFT", "left": 10, "width": 80},
      "vertical": {"anchor": "BOTH", "top": 2, "bottom": 2},
      "modifications": {
        "text": "Btn #1"
      }
    },
    {
      "name": "Button2",
      "source": "Button1",
      "parent": "TopPanel",
      "horizontal": {"anchor": "LEFT", "left": 90, "width": 80},
      "modifications": {
        "text": "Btn #2"
      }
    },
    {
      "name": "Button3",
      "source": "Button1",
      "parent": "TopPanel",
      "horizontal": {"anchor": "LEFT", "left": 170, "width": 80},
      "modifications": {
        "text": "Btn #3"
      }
    },
    {
      "name": "bar1",
      "source": "t1.AnotherProgressBar1",
      "parent": "root",
      "horizontal": {"anchor": "BOTH", "left": 5, "right": 5},
      "vertical": {"anchor": "BOTTOM", "bottom": 5, "height": 20},
      "modifications": {
        "customProperties.ProgressColor": "#FF0008C8"
      }
    },
    {
      "name": "bar2",
      "source": "bar1",
      "parent": "root",
      "vertical": {"anchor": "BOTTOM", "bottom": 30, "height": 20}
    },
    {
      "name": "TextField1",
      "source": "t1.B4XFloatTextField1",
      "parent": "root",
      "modifications": {
        "customProperties.Hint": "Full name"
      },
      "script": "TextField1.SetLeftAndRight(25%x, 75%x)\nTextField1.VerticalCenter = 30%y"
    },
    {
      "name": "Label1",
      "source": "t1.Label1",
      "parent": "root",
      "horizontal": {"anchor": "LEFT", "left": 10, "width": 200},
      "vertical": {"anchor": "TOP", "top": 220, "height": 40},
      "modifications": {
        "text": "Label1",
        "drawable.borderWidth": 2,
        "drawable.cornerRadius": 5,
        "drawable.color": "#00FFFFFF",
        "drawable.borderColor": "#FF969696",
        "fontsize": 15,
        "style": "BOLD",
        "textColor": "#FFFF00E9",
        "hAlignment": "CENTER_HORIZONTAL"
      }
    },
    {
      "name": "Label2",
      "source": "Label1",
      "parent": "root",
      "vertical": {"anchor": "TOP", "top": 260, "height": 40},
      "modifications": {
        "textColor": "#FFFF0013"
      }
    }
  ]
}

1794896770832.png


Using LayoutGenerator

Describe a layout:
Code: Copy
java -jar LayoutGenerator.jar describe <layout file>

Generate new layout based on json template:
Code: Copy
java -jar LayoutGenerator.jar process <json template> <output layout file>
 

Attachments

Last edited:
LayoutGenerator source code:
Code: Copy
Sub AppStart (Args() As String)
    Log("LayoutGenerator v0.9")
    Dim ValidArgs As Boolean = True
    If Args.Length <> 2 And Args.Length <> 3 Then ValidArgs = False
    ValidArgs = ValidArgs And ((Args(0) = "describe" And Args.Length = 2) Or (Args(0) = "process" And Args.Length = 3))
    If ValidArgs = False Then
        Log($"The following tasks are supported:
describe <layout file> - outputs a textual description of the layout file.
process <json template> <output layout file> - generates a layout file based on the template file."$)
        ExitApplication
    End If
    Dim Input As String = Args(1)
    If Args(0) = "process" Then
        Dim Output As String = Args(2)
        Dim json As Map = File.ReadString(Input, "").As(JSON).ToMap
        Dim jt As JsonTemplate
        jt.Initialize
        Dim res As BLLayout = jt.Process(json)
        Log($"Layout generated: ${res.ViewsMap.Size} views
Saving: ${Output}"$)
        res.WriteToBinary(Output, "")
    Else If Args(0) = "describe" Then
        Dim layout As BLLayout
        layout.Initialize.ReadFromBinary(Input, "")
        Log("Layout file: " & Input)
        Log(DescribeInstruction(Input))
        Log(layout.Describe)
    End If
End Sub


Private Sub DescribeInstruction (Input As String) As String
    Return $"
To generate a new layout, return JSON with this structure:
{
  "templates": [{"file": ${"""" & Input}", "alias": "t1"}],
  "header": {"source": "t1"},
  "root": {"source": "t1", "name": "root"},
  "views": [
    {
      "name": "lblTitle",
      "source": "t1.<view name from description>",
      "parent": "root",
      "horizontal": {"anchor": "BOTH", "left": 16, "right": 16},
      "vertical": {"anchor": "TOP", "top": 20, "height": 40},
      "modifications": {"text": "Welcome"}
    }
  ]
}

Rules:
- Use only source views And property paths present in the template description.
- Each view duplicates its source without children. Names must be unique.
- To duplicate a previously created view, use its name without "t1.".
- Parents And source views from the new layout must appear earlier in views.
- Omitted properties And positioning inherit their source values.
- horizontal: LEFT (left, width), RIGHT (right, width), BOTH (left, right).
- vertical: TOP (top, height), BOTTOM (bottom, height), BOTH (top, bottom).
- Right And bottom values are offsets from the parent's corresponding edges.
- Positioning values are numbers in designer units; Do Not append "dip".
- modifications uses dot-separated property paths And preserves property types.
- Colors accept "#RRGGBB" Or "#AARRGGBB".
- An optional "script" string on a view appends general designer script code.
- Use positioning objects Or scripts, Not modifications, To set view geometry.
- Return valid JSON without comments.
"$
End Sub
 
LayoutUtils: https://www.b4x.com/android/forum/threads/172516/#content

LayoutUtils can be used to create new layout files without using the designer. Use cases that I can think of:

- Conversion of non-B4X layouts.
- Automatic creation of layouts. Something like B4XPreferenceDialog but based on customized layouts.
- Modification of existing layouts. A bit like: https://www.b4x.com/android/forum/threads/b4x-thememanager-themecreator.172525/#content
- AI generated layouts.

How does it work?

Each view is made of various properties. As it is difficult and cumbersome to set all properties manually, we use one or more "template" layouts. These templates hold the views, including custom views, that we then duplicate, modify and use to build a new layout.

The various modification rules can be declared with a relatively simple json file, and then be processed by LayoutUtils. LayoutGenerator is a command line tool that provides two features:
1. It can read a layout file and generate a textual description of the layout.
2. It can read a json template file + one or more template layouts and generate a new layout.

The json file includes the following parts:
  1. templates — a list of layout templates containing the views to duplicate. Each item specifies a layout file and its alias.
  2. header and root — specify the source templates for the layout header and root view. The root can also include a modifications object.
  3. views— a list of views to create, processed in order. Each view includes:
    • name — the unique name of the new view.
    • parent — the name of the parent view. Use "root" for the layout’s main view. Other parents must appear earlier in the list.
    • source — the template alias and source view name, for example "t1.Label1". To duplicate a previously created view, omit the template alias, for example "Label1".
    • horizontal and vertical — optional positioning objects. Omitted positions retain their source values.
    • modifications — an optional object containing property names and their new values. Nested properties use dot-separated paths, such as "drawable.color".
    • script — optional code appended to the general designer script.

Example of a layout (generated by ChatGPT):
JSON: Copy
{
  "templates": [
    {"file": "template.bal", "alias": "t1"}
  ],
  "header": {"source": "t1"},
  "root": {"source": "t1", "name": "root"},
  "views": [
    {
      "name": "TopPanel",
      "source": "t1.Panel1",
      "parent": "root",
      "horizontal": {"anchor": "BOTH", "left": 5, "right": 5},
      "vertical": {"anchor": "TOP", "top": 30, "height": 50},
      "modifications": {
        "drawable.borderWidth": 2,
        "drawable.borderColor": "#FF0001D0",
        "drawable.color": "#FFE8F5E9"
      },
      "script": "DDD.SpreadControlsHorizontally(TopPanel, 100dip, 5dip)"
},
    {
      "name": "Button1",
      "source": "t1.Button1",
      "parent": "TopPanel",
      "horizontal": {"anchor": "LEFT", "left": 10, "width": 80},
      "vertical": {"anchor": "BOTH", "top": 2, "bottom": 2},
      "modifications": {
        "text": "Btn #1"
      }
    },
    {
      "name": "Button2",
      "source": "Button1",
      "parent": "TopPanel",
      "horizontal": {"anchor": "LEFT", "left": 90, "width": 80},
      "modifications": {
        "text": "Btn #2"
      }
    },
    {
      "name": "Button3",
      "source": "Button1",
      "parent": "TopPanel",
      "horizontal": {"anchor": "LEFT", "left": 170, "width": 80},
      "modifications": {
        "text": "Btn #3"
      }
    },
    {
      "name": "bar1",
      "source": "t1.AnotherProgressBar1",
      "parent": "root",
      "horizontal": {"anchor": "BOTH", "left": 5, "right": 5},
      "vertical": {"anchor": "BOTTOM", "bottom": 5, "height": 20},
      "modifications": {
        "customProperties.ProgressColor": "#FF0008C8"
      }
    },
    {
      "name": "bar2",
      "source": "bar1",
      "parent": "root",
      "vertical": {"anchor": "BOTTOM", "bottom": 30, "height": 20}
    },
    {
      "name": "TextField1",
      "source": "t1.B4XFloatTextField1",
      "parent": "root",
      "modifications": {
        "customProperties.Hint": "Full name"
      },
      "script": "TextField1.SetLeftAndRight(25%x, 75%x)\nTextField1.VerticalCenter = 30%y"
    },
    {
      "name": "Label1",
      "source": "t1.Label1",
      "parent": "root",
      "horizontal": {"anchor": "LEFT", "left": 10, "width": 200},
      "vertical": {"anchor": "TOP", "top": 220, "height": 40},
      "modifications": {
        "text": "Label1",
        "drawable.borderWidth": 2,
        "drawable.cornerRadius": 5,
        "drawable.color": "#00FFFFFF",
        "drawable.borderColor": "#FF969696",
        "fontsize": 15,
        "style": "BOLD",
        "textColor": "#FFFF00E9",
        "hAlignment": "CENTER_HORIZONTAL"
      }
    },
    {
      "name": "Label2",
      "source": "Label1",
      "parent": "root",
      "vertical": {"anchor": "TOP", "top": 260, "height": 40},
      "modifications": {
        "textColor": "#FFFF0013"
      }
    }
  ]
}

View attachment 174935

Using LayoutGenerator

Describe a layout:
Code: Copy
java -jar LayoutGenerator.jar describe <layout file>

Generate new layout based on json template:
Code: Copy
java -jar LayoutGenerator.jar process <json template> <output layout file>
Perhaps, there is mermaid file(.mmd) is converted to layout, too.
 
Last edited:
My LLM said LayoutGenerator can be use for retiring the existing hand-built xui.CreatePanel dialogs in my project.
 
Back
Top