Android Tutorial LayoutView: The alternative way to load and use your layouts

Introduction

Following the moto design-more-code-less in our encapsulated b4a development speed-wagon, I created a LayoutView class that is also the core of the DialogView in the XtraViews library.

This tutorial is available in the latest XtraViews package and as standalone APK

How to use

Lets take as an example the sample1 project in the layoutview\samples folder. The sample has 4 edge anchored dummy buttons and a button that changes the text of all buttons to a random generated one each time clicked.

upload_2014-7-22_7-14-59.png



This is how we do it without using the LayoutView class:

In order to access the buttons, we must switch to the designer and generate all declarations
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
End Sub
Load our layout
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    Activity.LoadLayout("Layout1")
End Sub
Write our handler
B4X:
Sub BtnChangeButtons_Click
    Button1.Text = "#" & Rnd(100,999)
    Button2.Text = "#" & Rnd(100,999)
    Button3.Text = "#" & Rnd(100,999)
    Button4.Text = "#" & Rnd(100,999)
End Sub


Well, there is nothing wrong with that. But, switching between designer and code or trying to access a layout view without having generated the appropriate declaration can be some times very annoying.

Now, let's see how LayoutView can help.

In our sample project, we instantiate our LayoutView object once.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim Layout As LayoutView
End Sub
Load our layout using the LayoutView (Layout)
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    Layout.LoadLayout("Layout1")
End Sub
Tip: You may load the layout and set an html enabled title as well
B4X:
Layout.LoadLayoutWithTitle("Layout1", "<b><font color=cyan>HTML</font></b> <i>title!</i>")
And rewrite our handler using the Layout instance
B4X:
Sub BtnChangeButtons_Click
    Layout.Button("button1").Text = "#" & Rnd(100,999)
    Layout.Button("button2").Text = "#" & Rnd(100,999)
    Layout.Button("button3").Text = "#" & Rnd(100,999)
    Layout.Button("button4").Text = "#" & Rnd(100,999)
End Sub
Tip: You can still get an abstract Object by using the LayoutView.Get method
B4X:
Dim Button1 as Button = Layout.Get("button1")
Button1.Text = "#" & Rnd(100,999)

Note: The LayoutView class supports all known view classes (Layout.Label("label1"), Layout.SeekBar("seekbar1") and so on).

That means that we instantiate a global LayoutView once and after that we can get a reference to ANY object in our layout at ANY given time without the need to declare that object.

--
Related tutorials:

That's all for now folks! :D
 
Last edited:

ivan.tellez

Active Member
Licensed User
Longtime User
Layout.LoadLayout takes a string as a parameter that defines the layout file. Can you please give me a more detailed use case?

Hi Periklis

I like the encapsulation, creating libs to reuse code, so it will be great to embbed bal files in jar libs and load them with DialogView And DialogView

There are no plans to make possible something like Layout.LoadLayoutFromInputStream ?

Thanks :D
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
@LucaMs Please redownload the latest package. I made it 3.20 compatible :D

Thanks for reporting the issue!

PS: The problem was with the JRE references in the eclipse project.
 
Last edited:

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
@ivan.tellez Unfortunately not in the near feature due to how the internal b4a .bal loader works.
 
Last edited:
Top