Orientation guidelines

fabero

Member
Licensed User
Longtime User
I know, I can block orientantion for example if I block landscape, if device rotate 90° the app remain like in vertical.

But I want to know how to:

1) cacth the orientation mode, that is horizontal or vertical; or else catch if the app is in horizontal or vertical?

2) how to define the layout if is horizontal or vertical? There is a way to define both layout?

3) How use the defined layout based of the orientation.
 

fabero

Member
Licensed User
Longtime User
I know, the choice is not evident nor easy. My goal is by far not to promote two applications but to carefuly evaluate the problem to make a good choice.

Best regards.

I approve your work. The choice is mine, but know all possibilities is the best thing to make my personal right choice.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Seems to be more simple. Doing 2 apps.

If you are saying that doing 2 apps would be simpler than doing 2 layouts in one app, I have to disagree: every time you change the code in one app, you have to make the same change in the other. That is a problem I wouldn't want. This is why they let you have different layouts for the same app in the first place.

Even if keeping the code in sync in two different apps is not a problem for you, then if you are going to distribute the app from Marketplace, Amazon, etc., you will have twice as much work to do for that.

And you will still need code in each app checking the size and density of the device unless you just want your app to crash (or at least look very funny) when someone tries to use the tablet version on a phone or vice-versa. So if you are going to have the code testing size and density anyway, you might as well use that code to let you have one app that works on both phones and tablets.
 
Upvote 0

fabero

Member
Licensed User
Longtime User
If you are saying that doing 2 apps would be simpler than doing 2 layouts in one app, I have to disagree: every time you change the code in one app, you have to make the same change in the other. That is a problem I wouldn't want. This is why they let you have different layouts for the same app in the first place.

Even if keeping the code in sync in two different apps is not a problem for you, then if you are going to distribute the app from Marketplace, Amazon, etc., you will have twice as much work to do for that.

And you will still need code in each app checking the size and density of the device unless you just want your app to crash (or at least look very funny) when someone tries to use the tablet version on a phone or vice-versa. So if you are going to have the code testing size and density anyway, you might as well use that code to let you have one app that works on both phones and tablets.

I remember that, Android market can distinguish your app and give you the right apk to download...
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Klaus, two questions :

1) I thought if you used the designer, the only 'smartphone' variant was the original HVGA default, and that no matter what the res of the target phone, b4a would automatically adjust. Why multiple variants in one file?

2) along the same lines... how can you have multiple variants in a single layout file? Are some of the ui elements assigned to one variant and others to another? How does this work? I'm obviously missing something.

Thanks...
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
1) I thought if you used the designer, the only 'smartphone' variant was the original HVGA default, and that no matter what the res of the target phone, b4a would automatically adjust. Why multiple variants in one file?

2) along the same lines... how can you have multiple variants in a single layout file? Are some of the ui elements assigned to one variant and others to another? How does this work? I'm obviously missing something.

I think this is all explained at Designer - Basic4android Wiki but the short answer to (1) is that Designer can only adjust for differences in scale, but not between devices with the same scale but different sizes. This is illustrated with screenshots in the Introduction section of the link above.

(2) If you assign a ui element (view) to one variant, it is automatically added to all other variants IN THE SAME layout file, but you can have different layout files for the same app for different device size and have some different views in each (depending on your code). In cases where your code requires each view to have some of the same views, for a layout which does not use a particular view, you can make it invisible or (as I do) give it a Left that puts in out of the range of the device, like 1000 on an 800x480 screen.
 
Last edited:
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
That's what I thought... for a second, it sounded like someone said you could have one variant for portrait and another for landscape and the system would choose the correct one based on the actual device orientation. But that'd look awful messed up in landscape :) it'd be best to have two different layout files and choose the correct one in activity_create based on the orientation. Unless im mistaken, you can't have two different layouts in the same .bal file. I'm going to have another look at the layout docs...
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
That's what I thought... for a second, it sounded like someone said you could have one variant for portrait and another for landscape and the system would choose the correct one based on the actual device orientation. But that'd look awful messed up in landscape :) it'd be best to have two different layout files and choose the correct one in activity_create based on the orientation. Unless im mistaken, you can't have two different layouts in the same .bal file. I'm going to have another look at the layout docs...

Actually, you CAN have different layouts for portrait and landscape in the same file and the system will choose the correct one; but you cannot have different views in different variants stored in the same file. The whole point of having multiple variants in the same files is that changes in one (adding or removing views) are made in the other(s) automatically. With layout files in different files, if you make changes to the layout in one, you have to make them to each of the others individually if you want the changes in all the layouts, but the advantage is that if you want to, you CAN have different views in the different layout files.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
1) You can define in the Designer with Other any layout variant you want.
You can, for example, define a special layout for a 480/800 scale1.5 device if you want to take advantage of the extra space you have at the bottom of the screen. You can do this in a layout variant or by code.
I explain this in chapter 5 Screen sizes and resolutions in the Beginner's Guide. In the example it's done by code, but could be done in a layout variant.
What you cannot have in a same layout file, is having two layout variants with the same number of pixels but different densities (480*800 scale 1.5 and 480*800 scale 1). This is wrong, it is possible.

2) If you want to have one layout for portrait and one for landscape you can have two layout variants in the same layout file. All the views are in the different layout variants but at different places and could have different dimensions.
This is also valid for any other resolution.
There is an example in chapter 8.6 Layout variants in the Beginner's Guide with one layout file but two layout variants one for portrait and another one for landscape.
This could of course also be done by code.
You could also add all views by code without any layout file and set, in the code, all the positions and dimensions according to the different resolutions, densities and orientations.

In version 1.5 of B4A there was a layout 480 x 800 scale 1.5 (240) as a predefined layout, I just saw that this one is no more a predefined layout in version 1.6.

Best regards.

EDIT: This statement above:
What you cannot have in a same layout file, is having two layout variants with the same number of pixels but different densities (480*800 scale 1.5 and 480*800 scale 1).
is wrong, it is possible.
 
Last edited:
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
From documentation:

When a New Variant is created, it inherits all the views of the original layout. However, each layout variant holds a different set of values for the position and size of the views. If you change the text of any view it will be changed in all variants automatically. However if you change the position or size of a view it will only affect the current variant.

Jackpot! It really sucks that it'll be tonight before I can play with this :(
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
What you cannot have in a same layout file, is having two layout variants with the same number of pixels but different densities (480*800 scale 1.5 and 480*800 scale 1).

I just tried that in Designer and it seems to work okay. I have an 800x480x1.5 layout for 4" phones and I added an 800x480x1 variant in the same file for a 7" tablet. Of course, the initial layout on the scale 1 variant was scrunched up into the corner on the 7" tablet, but when I moved views around, then looked back at the 1.5 variant, they were still in their correct place.

What would be the problem of having them both in the same layout file?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You are right, I tested it too.
It was an assumption of mine but the systen is more clever than I thouht.
So it is possible to have two layout variants with the same number of pixels but different densities in a same layout file.

Best regards.
 
Upvote 0
Top