Android Question Question about variants

Diego Roman

Member
Licensed User
Hi, could you please help me with the following question please:

For example, If 2 vertical variants are created, then how does android decide which one to use?
If I make a variant of a small size and another large variant, but the device has a medium screen size, which one uses Android and why?

Thanks
 

Sagenut

Expert
Licensed User
Longtime User
You have to choose which one to use.
And you should try to create as less variants as possible.
Usually just two:
Portrait (vertical) and Landscape (horizontal) if needed.
With the use of anchors and designer script you will be able to make the layout to adapt to every screen (nearly).
Taking your case, if you absolutely need to have 2 different layouts for small and large screen then you will need to detect the size of the screen by code and load the respective layout.
 
Upvote 0

Diego Roman

Member
Licensed User
Ok, but if I have 2 variants inside the same layout,so, how to load the right variant? Or should be only one variant per layout?
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Ok, but if I have 2 variants inside the same layout,so, how to load the right variant? Or should be only one variant per layout?
The best options is to have only one Layout for Portrait (vertical), and if needed one more Layout for Landscape (horizontal).
Edge case will bring you to have 2 more Layouts to manage Tablet screens, but for now focus on to obtain 1 single layout that adapt everywhere correctly.
Working with Anchors and Designer Script should allow you to obtain a nearly universal layout that will adapt to every screen.
Sadly in the Android world there isn't a standard screen ratio for displays.
So sometimes maybe it will happen the need to manage some particular case.
But even these cases could manage correctly your layout if well designed.
Anyway, in the case that you still want to have 2 different layouts for small screens and large screens you could try something like this:
B4X:
Root = Root1
Dim ScreenSize As Double = GetDeviceLayoutValues.ApproximateScreenSize 'Detect the approximate screen size of the device in inches
If ScreenSize < 4.5 Then 'Condition for Small Screens
    Log("Small Screen")
    Root.LoadLayout("small_screen_layout")
Else 'Condition for Large Screens
    Log("Large Screen")
    Root.LoadLayout("large_screen.layout")
End If
This is just a simple example to make the decision.
But I suggest you to work to make one good adaptable layout.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Variants should be choosen automatically by the system, but you cannot be sure that the choice will be the best one.
Better to have 2 different layouts (if really needed) and you choose the correct one.
Better again to have one that work everywhere. :)
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
If your application does not need to support orientation change, then the multi-layout option is acceptable. If it needs to support, then more problems can arise. I use Variants, several of them for the same orientation. The system will select the one that is best displayed based on screen resolution, pixel density, screen scale and other parameters. So far I haven't had any problems with that approach, but I have with multiple loading of Layouts with the same objects.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I would say that for smartphones that value could be set to 6.5", higher for tablets.
And we should consider smart watches.
Consider that it returns an approximate size.
6 inches is a big screen (normal nowadays šŸ˜… ).
I think that apps for Smart Watches have a dedicated section in Google Play.
So they must be developed optimized apart on purpose.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
1671285618084.png


Direi che da 6", meglio 6.5", molto probabilmente saranno tablet.
 
Upvote 0
Top