Question on Variants and Globals

Roger Garstang

Well-Known Member
Licensed User
Longtime User
As a developer in other languages there are 2 things the manuals didn't really go over that I'd like to know more details on.

1. Screen Variants. I like how the designer can adjust for different sizes by code, but I'm not seeing much use for variants with all the possible resolutions. When the app is running does Android scan through this list and find the closest match or something? Something like first matching the rotation then the closest x and y dimensions or what? So, can I just pick a resolution and have portrait and landscape and use code to shift things in each? It sounds like I don't even need to have but one variant and align it in code, but if I only have a portrait variant, will it still work in landscape?

Also, I haven't tried it yet since still reading, but being able to use the dip values and calculations in the property page would be awesome, and almost eliminate the need for the separate tab.


2. I'm also a little confused on the variables for my views. I've seen examples of manually creating the variables and functions for events and I've seen them created in the template fashion by the designer. So, are they really already there and we just need to define them? It sounds like if I make a button called btnOK that means it is sort of a reserved word now and if make a variable called the same name it is not a variable but the button now. It isn't really clear as to what ties the variable names to the controls/views. So, if I call my variable an int does the compiler issue a warning? There were also things like a btnevent_click function used in the examples and not explained well. It appears to be a universal function to get any button click (Which I guess is why you used tag instead of button text otherwise OK would have been added to the textbox when pressed). What other universal functions are there, and is all this already there and all we have to do is declare them to use them?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
When the app is running does Android scan through this list and find the closest match or something?
Yes.
So, can I just pick a resolution and have portrait and landscape and use code to shift things in each?
You don't need to use code. You can use the designer to build each variant as needed. You can also use the designer script to fine tune each of the variants.

2. The EventName property is what matters. It is either set in the designer or if you add the views by code then it is set in the Initialize method.
If a sub exists with the correct name: <event name property>_<event (eg: click)> then the sub will handle the event.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Ok, I see now looking back at the example the event property for all the buttons were set to the same btnEvent. I answered my own question too about the variable names...I created an int variable named the same as an image control/view in my form/layout. It compiled fine, but said it was the wrong type when executing.

I'm still not seeing the behavior I expect from variants though. I have two declared- a 480x800 and an 800x480. In the Design script for the 480x800 I'm sizing an image to a width of 25%x. In the 800x480 I size it to 100%x. When compiled and ran it seems to just be using the portrait variant without even executing the design script. Rotating the screen doesn't change anything and still uses the base design without design script applied. Back in the IDE it does the same thing. If I manually pick the variant and click the design script tab I see it apply to it, but I have to manually click the variant then the design tab (Having the Run Script Button in the Main Tab would be a nice addition). So, how do we pick a variant to use when running since Android doesn't appear to be selecting it?

Almost looks easier to just put the design script in Activity_Create and determine the rotation there, but then I lose the visual design. Do I have to manually call RerunDesignerScript or something? I thought it was already called from what the documentation was saying. Seems weird for it to require width and height too, shouldn't it know this already? There is no variant selection too, so it may just run the first one since I don't see android selecting the right one for the rotation.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Do I have to manually call RerunDesignerScript or something?
No.

The most important value is missing in your post and it is the variant scale.
Note that there is a known bug in v1.9 that there must be at least a single line of code in the general script window (you can write i = 0). Though I don't think that this is the issue here.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Yes, and manually calling RerunDesignerScript didn't make a difference, so the code isn't running with it either. Scale is 1. I'll check later what areas have code. I know there is at least 1-2 lines in the Main module to load the layout, but if you mean the designer script it may all be in the variant specific section. I'll try to add something to the other section later to see if that makes a difference.

One suggestion for the Designer Script tab would be a before and after global/general designer script. I assume the global/general runs first then the per variant, but it could be useful for some common code to run last too. Like when I size the image width to be a percentage of x I adjust the height to be a ratio of the image width after to keep the aspect ratio. It is the same ratio in each variant, so could be in the common code area...but needs to run after the per variant code. Makes redundant code in the per variant code now to have the same thing in each.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Ok. I don't think that is the actual problem though since the code still isn't running on rotation which can happen on any device, but that does bring to mind additional questions.

In using the Designer Scripts it is recommended to use something in a scale of 1. The device I'm primarily building for is a Motorola ET1 with I believe a 1024 x 600 screen. I want the software to be able to be used on mid to high end devices devices in the last couple years too which were for the most part 480x800/800x480 depending on rotation. So, I figured I'd pick that resolution with a scale of 1 as a base. So, what should I use as a scale of 1 to build for a minimum 800x480/480x800? The phone selection in the list now would be something like a scale of 1.66666666666~
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can see the device details when you connect the designer to the device. You can also run this code to find it:

B4X:
Msgbox(GetDeviceLayoutValues, "")

I guess that this device scale is 1.5. This means that it should work properly with the standard phone variant which is 320x480, scale=1.0.

You do not need to create any additional variants in this case.
 
Upvote 0
Top