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.
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
Load our layout
Write our handler
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.
Load our layout using the LayoutView (Layout)
Tip: You may load the layout and set an html enabled title as well
And rewrite our handler using the Layout instance
Tip: You can still get an abstract Object by using the LayoutView.Get method
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!
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.
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
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
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
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
B4X:
Layout.LoadLayoutWithTitle("Layout1", "<b><font color=cyan>HTML</font></b> <i>title!</i>")
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
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!
Last edited: