Skip to content
Aditya Sharat edited this page Jul 2, 2015 · 36 revisions

Getting a LayoutBuilder

A LayoutBuilder is used to build views from a Proteus Layout JsonObject. To get an instance of a LayoutBuilder create a new instance of LayoutBuilderFactory.

LayoutBuilderFactory layoutBuilderFactory = new LayoutBuilderFactory();

This factory returns singleton instances of different types LayoutBuilders.There are 3 types of builders:

  • SimpleLayoutBuilder
// get the instance of the SimpleLayoutBuilder
layoutBuilder = layoutBuilderFactory.getSimpleLayoutBuilder(Context context);
  • DataParsingLayoutBuilder
// get the instance of the SimpleLayoutBuilder
layoutBuilder = layoutBuilderFactory.getDataParsingLayoutBuilder(Context context);
  • DataAndViewParsingLayoutBuilder
// get the instance of the DataAndViewParsingLayoutBuilder
layoutBuilder = layoutBuilderFactory
                         .getDataAndViewParsingLayoutBuilder(Context context, 
                                                             JsonObject viewProvider);

Here the viewProvider contains the set super set of Proteus Layouts which will be used to serve [Proteus Layouts]](Layout) for unknown view types.

Building Views

Call the LayoutBuilder.build method to build a ProteusView from a Proteus Layout JsonObject.

ProteusView proteusView = layoutBuilder.build(parentView, layoutJsonObject, null, 0);
  • parent The intended parent view for the View that will be built.
  • layout The JsonObject which represents the layout for the View that will be built.
  • data The JsonObject which will be used to bind data to the View. (pass null for SimpleLayoutBuilder.
  • childIndex The index of this view in its parent (pass 0 as default).

Do get the View built by the LayoutBuilder call ProteusView.getView.

View view = proteusView.getView();

Example layout JSON

{
  "type": "LinearLayout",
  "layout_width": "match_parent",
  "layout_height": "wrap_content",
  "background": "#ffffff",
  "orientation": "vertical",
  "padding": "16dp",
  "children": [
    {
      "type": "TextView",
      "layout_width": "wrap_content",
      "layout_height": "wrap_content",
      "layout": "Hello World",
      "textColor": "#444444"
    }
  ]
}

After going through the Proteus Layouts wiki you know that only a sub set of standard Android Views and their respective attributes have been added and handled within Proteus. There are two ways to handle custom view types and attributes:

  • Use the LayoutBuilderCallback interface;
  • Register your ViewParser with that instances of the LayoutBuilder

Related: