Android Question Layout in Class

DawningTruth

Active Member
Licensed User
I'm trying to setup a layout which is activated in a class.

I want it to be used as follows:

ClassName.CreateLayout(Parameter)

Is there a simple tutorial or code example somewhere that shows how to do this? I have searched, but as I'm new to B4A, the examples are too complicated for me.
 

Computersmith64

Well-Known Member
Licensed User
The simplest example I can think of would be:

B4X:
' Main activity
Sub Globals
    Private cTest as clsTest
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1") '*****Instead of loading your layout here
    cTest.Initialize(Activity, "Layout1")
End Sub

'Class clsTest
Sub Class_Globals
    mainAct as Activity
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(act as Activity, layout as String)
    mainAct = act
    mainAct.LoadLayout(layout)
End Sub
- Colin.
 
Last edited:

DawningTruth

Active Member
Licensed User
The simplest example I can think of would be:

B4X:
' Main activity
Sub Globals
    Private cTest as clsTest
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1") '*****Instead of loading your layout here
    cTest.Initialize(Activity, "Layout1")
End Sub

'Class clsTest
Sub Class_Globals
    mainAct as Activity
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(act as Activity, layout as String)
    mainAct = act
    mainAct.LoadLayout(layout)
End Sub
- Colin.
Thx, I'll give that a go?
 

DawningTruth

Active Member
Licensed User
What exactly do you want do do and what for?
Hey Klaus,

It is a matter of modularising the code. The app will have quite a few moving pieces and I need to modularise entire pieces of functionality to keep things manageable. The Main activity must just be the conductor of all the modules.
 

DawningTruth

Active Member
Licensed User
Hi Klaus,

At this stage I do not have such a project as I do not know how to implement such an architecture. However the picture below should give you a clearer concept of the goal.

Essentially there is a Main Controller Module which controls the program flow, logic and events. This accordingly accesses Controllers for the various screens. The Screen controllers control which screens are displayed and handle the events generated by the various screen elements. There is also a Data Management Module which controls services and system data models.

The idea is to implement this in a way that it works for both B4A and B4i.

Hope this clarifies :)
 

klaus

Expert
Licensed User
Finally, I suggest you to use Activities in B4A and code modules in B4i. One B4i code module similar to the B4A Activities.
This allows you to modularize the project.
Use as much as possible B4XViews, this allows common code for both platforms.
Be aware that not all code is cross platform because the operating systems are differen!
I suggest you to have a look at the SQLiteLight2 projects in the B4X SQLite Database booklet.
There is a project for each product. Activities are used in B4A and code modules in B4i.
This could give you an idea how to handle this.

One question in your diagram: the Common Screen Element
What is it supposed to do? As it can be called from different screens meaning activities or code modules.
 

DawningTruth

Active Member
Licensed User
Finally, I suggest you to use Activities in B4A and code modules in B4i. One B4i code module similar to the B4A Activities.
This allows you to modularize the project.
Use as much as possible B4XViews, this allows common code for both platforms.
Be aware that not all code is cross platform because the operating systems are differen!
I suggest you to have a look at the SQLiteLight2 projects in the B4X SQLite Database booklet.
There is a project for each product. Activities are used in B4A and code modules in B4i.
This could give you an idea how to handle this.

One question in your diagram: the Common Screen Element
What is it supposed to do? As it can be called from different screens meaning activities or code modules.
Thx Klaus, that is fantastic advice. Will action.

As to the Common Element, that would typically be the top and bottom menus which would remain the same between activities. The idea is to create them in one place and change them in one place to improve maintainability as the app scales in size and complexity. DRY principle.
 

klaus

Expert
Licensed User
... that would typically be the top and bottom menus which would remain the same between activities.
Be aware that only one activity is active at the same time!
This means that a same menu on all activities means an individual menu on each activity, therefore several same menus.
If you want to have one menu for all screens then using Panels could be a solution.
The drawback is that everything is in the same activity and you need to manage the visibility of the different panels.
Then another point, screen sizes. If your program is supposed to run on smartphones and on tablets, you need to think about what you want to display at the same time on the smartphone and on the tablets.
I have an application where I use different panels and I display one at the same time on the phone in portrait orientation only and two panels side by side on the tablet in landscape orientaion only. This needed different layouts for phone and tablets but almost all code is common.
At first I had developped the program for tablets only, then I wanted it too for phones and I wrote a separate project for this.
But, maintain two projects with almost the same code was not efficient and I merged both projects into one.
 

DawningTruth

Active Member
Licensed User
Be aware that only one activity is active at the same time!
This means that a same menu on all activities means an individual menu on each activity, therefore several same menus.
If you want to have one menu for all screens then using Panels could be a solution.
The drawback is that everything is in the same activity and you need to manage the visibility of the different panels.
Then another point, screen sizes. If your program is supposed to run on smartphones and on tablets, you need to think about what you want to display at the same time on the smartphone and on the tablets.
I have an application where I use different panels and I display one at the same time on the phone in portrait orientation only and two panels side by side on the tablet in landscape orientaion only. This needed different layouts for phone and tablets but almost all code is common.
At first I had developped the program for tablets only, then I wanted it too for phones and I wrote a separate project for this.
But, maintain two projects with almost the same code was not efficient and I merged both projects into one.
Thx Klaus. Good to know the pitfalls in the beginning. Looks like there are some platform constraints architecturally which I must consider at the very beginning.
 
Top