Coding in another layout

jschuchert

Active Member
Licensed User
Longtime User
I started my application with several buttons on my main form (layout named "menu"). These buttons act as a menu for the application. When any of them are pressed, I want another layout to open for entering data and performing calculations. I created another layout for one of the menu buttons and it opens fine after using an example from Klaus as shown below:


Sub Activity_Create(FirstTime As Boolean)
activity.LoadLayout ("menu")
End Sub
.
.
.
Sub btnembed_click
removeviews
activity.loadLayout("embed")
End Sub

Sub removeviews
Dim i As Int
For i=activity.NumberOfViews-1 To 0 Step -1
activity.removeviewat(i)
Next
End Sub

Now I want to begin coding for the next layout. This scenario will be repeated many times throughout the application. Would each layout become a separate activity with its own variables, etc.? It seems like I would be starting another application by doing "File-new-save, etc". I'm sure the answer is right in front of my face with the tutorials but I can't see it yet. I usually do pretty well after I get over the hump. You guys are great to put up with an old man.

Jim
 

klaus

Expert
Licensed User
Longtime User
There are different possibilities depending on how many layouts you have, here are different examples.
If there aren't too many, I would work in the same activity and load the different layouts into panels, like the example TwoPanelLayouts, all the code is in the main module.
If you have many different layouts, but not very much code for each layout you could use different layouts for the same activity, like the example TwoActivityLayouts, all the code is in the main module. Even it that case, personaly I would use the previous principle.
If you have many different layouts with much code, you should use one activity per layout, like the example TwoLayoutActivities, the code for each layout is in it's own module, advantage, better overlook at the code and easier maintenance.
Would each layout become a separate activity with its own variables, etc
It depends on what variables you are speaking of.
If you use several activities. All views of the current activity are destroyed when loading a new activity and also all variables declared in Sub Globals.
General variables must be declared in Sub Process_Globals they remain when changing an activity. If you need to memorize layout parameters for an activity for it's next use, these parameters must be set to variables declared in Sub Process_Globals.

Best regards.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thanks, Klaus. I revisited the 2 activities layout and the light bulb came on. I had not noticed how I can add another activity module through the 'Project' menu item. My application will have in excess of 20 activities (unless I pare it down) so I will use the multiple activities plan. There is quite a bit of code per activity.

If I understand correctly, the code modules are only for storing code (for copying and pasting) but cannot be called into the activity like in other projects I have done. It's not that big of a deal but will increase the size of the project.

Is there a way to turn off the keypad that appears whenever a text box is selected other than pressing "esc"? The one on the right seems to have the same function. I don't have a real device to test or maybe I would find out its purpose.

I now have to learn how to manipulate "Files" because the results of the calculations in my project need to be stored permanently for use from all of the various activities. Anything I need to be aware of? I have gotten so used to text files, I never got around to using SQL. Should I start now?

I have a long way to go so hope I don't get discouraged or not live long enough to finish. I need to keep my mind active to avoid the big "A". It is very comforting to know you and others like you are willing to help.

Jim
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
You can call subs that are in code modules.

I have a General module with save and load routines. To call them I use
B4X:
General.LoadFile ("FileName")
This runs the LoadFile sub (passing "FileName" to it).
Hope that helps.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If I understand correctly, the code modules are only for storing code (for copying and pasting) but cannot be called into the activity like in other projects I have done. It's not that big of a deal but will increase the size of the project.
The code modules are only useful for general code, you can not call any routine in an activity from a code module. But if you have general calculations you could use in different projects, then code modules are the best way.

I now have to learn how to manipulate "Files" because the results of the calculations in my project need to be stored permanently for use from all of the various activities. Anything I need to be aware of? I have gotten so used to text files, I never got around to using SQL. Should I start now?
To save your files I wouldsuggest you to use following directories either File.DirInternal or File.DirRootExternal.
If you have huge data SQL would be the solution. Otherwise use text files.
I haven't used SQL yet.

I have a long way to go so hope I don't get discouraged or not live long enough to finish. I need to keep my mind active to avoid the big "A".
So needed we to do :).

It is very comforting to know you and others like you are willing to help.
Don't worry as long as we have pleasure doing it we will stay here.

Best regards.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
kickaha,

Thank you for your post but I guess I don't fully understand. Let's say I create a 'general' code module and put the following code in it:

Sub removeviews
Dim i As Int
For i=activity.NumberOfViews-1 To 0 Step -1
activity.removeviewat(i)
Next
End Sub


Now I want to use that code in the following activity (and many others):

Sub btnMenu_click
<<<<<<<<<< something like your example should go here
activity.LoadLayout ("menu")
End Sub

That doesn't work because a routine cannot be called from a code module. I thought "LoadFile" might be in the core stuff for b4a but couldn't find it. I would appreciate your addressing this scenario with more detail.

Incidentally, I used to use <code> and </code> and <color=__> and </color> before the actual text to clarify my posts but it doesn't work now. What is the proper syntax.

Thanks, Jim
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
To do what you are trying to do, in your general code module:
B4X:
Sub removeviews (Value As Activity)
Dim i As Int
For i=Value.NumberOfViews-1 To 0 Step -1
Value.removeviewat(i)
Next
End Sub

Then in your activity:
B4X:
Sub btnMenu_click
general.removeviews (activity)
activity.LoadLayout ("menu")
End Sub

so when you click the btnMenu it calls the removeviews with a pointer to the activity, so that it knows what activity to work on.

I have tried this and it works fine.

For your other question, use [ ] brackets and not < >

Also, LoadFile is one of my subroutines (it saves my user prefs in a text file)
 
Last edited:
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thanks again. I'm confident this will work but at the present time, each time I compile the application (with or without the new code) I get a message saying the "application has stopped unexpectedly. Try again." Then a 'Force Close' button. Tried it several times without success. I will reboot and get back to you later.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Kickaha,

Ok, that worked great. Thank you very much. I have many other subs that I will use this on.

I had pasted many global variables from my b4ppc app version into the process globals in this b4a version and had left things that shouldn't have been there. That is what caused the compiling failure.

May I now add you to my list of guardians?

Thanks again.

Jim Schuchert
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Kickaha,

Ok, that worked great. Thank you very much. I have many other subs that I will use this on.

I had pasted many global variables from my b4ppc app version into the process globals in this b4a version and had left things that shouldn't have been there. That is what caused the compiling failure.

May I now add you to my list of guardians?
Thanks again.

Jim Schuchert

Glad it works for you, you may call on me any time and I will help if I can.

Andy
 
Upvote 0
Top