Android Question How does B4A choose a variant layout

agraham

Expert
Licensed User
Longtime User
OK, I've searched the forum and watched the tutorials and I know that the most recent advice is that I should use a single variant with anchors and Designer Script but I'm still curious as to how B4A chooses a variant so I have two questions.

1) Does the single variant advice include using the same variant for both portrait and landscape? My app will have totally different layouts for portrait and landscape so it would seem easier to have the two variants and adapt them individually by scripting.

2) I assume that B4A at run-time chooses the variant closest in normalised aspect ratio to the device but what is the exact definition of 'closest'?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
a simple layout will fit in both portrait and landscape but i think a real input form need separated layouts.
if me using the size that the designer show in the statusbar than it had use this variant for otherwise it was a surprise.
my experience was also that the preview what u see is what u get is not true with android devices. b4x and also in delphi.
and because this i can't use the same layout for phone & tablet.
secondly do not trust any emulator.

in past for other apps i had position / resize the controls by code (usually the width of textboxes) and tested it from minimum to maximum window size.
it was the best flexible solution. if u need more in a small window use tabs or scrolling :)

i believe the ide use also this file where some device sizes are listed
AbstractLayouts.txt this list appear in the combobox below the designer tab name.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is completely fine to have more than one variant. The recommendation is to start with anchors and designer script before adding more variants. Variants are difficult to maintain.

The algorithm chooses the variant that its width and height are closest to the normalized device screen size.
- It penalizes variants that are taller or wider than the screen size.
- It prefers variants with the same orientation and variants with the same scale (though you should always use a scale of 1.0).
 
Upvote 0

techgreyeye

Member
Licensed User
Longtime User
It is completely fine to have more than one variant. The recommendation is to start with anchors and designer script before adding more variants. Variants are difficult to maintain.

The algorithm chooses the variant that its width and height are closest to the normalized device screen size.
- It penalizes variants that are taller or wider than the screen size.
- It prefers variants with the same orientation and variants with the same scale (though you should always use a scale of 1.0).

Hi Erel,

I'm intrigued by how the algorithm works also.

In a project I have 4 variants as below

320x480 scale 1 (160DPI)
480x320 scale 1 (160DPI)
1280x800 scale 1 (160DPI)
800x1280 scale 1 (160DPI)

I was testing the layouts using Nox. Initially my screen size was set to 1280x800 DPI 240 in Nox, but my app was displaying the 480x320 variant.
At 1280x800 DPI 200 it still displays the 480x320 variant.
At 1280x800 DPI 190 it displays the 1280x800 variant.

Another test I tried, my display setting is 1280x800 DPI 240, so the 480x320 scale 1 layout is being used. I removed the 480x320 layout, and the 320x480 layout is used rather than the 1280x800 - so something is being giving higher priority than the orientation, which I would assume was the most important factor in deciding on a layout.

I just tried removing the 320x480 layout, screen at 1280x800 DPI 240 and it isn't using a layout at all.
 
Last edited:
Upvote 0
Top