Android Question Orientation Not Reported Correctly

MrKim

Well-Known Member
Licensed User
Longtime User
This shouldn't be that hard.

This code in the Code Snippets PDF:
B4X:
Sub GetOrientation As Int
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "window", "java.lang.String")
r.Target = r.RunMethod("getDefaultDisplay")
Return r.RunMethod("getOrientation")
End Sub
Says:
The orientations are:
Smartphones Tablets
0 = portrait 0 = landscape
1 = landscape turn left 1 = portrait turn left
2 = portrait upside down 2 = landscape upside down
3 = landscape turn right 3 = portrait turn right

And this code from Snippets to determine it is a tablet:
B4X:
Sub Activity_Create(FirstTime As Boolean)
If GetDevicePhysicalSize > 6 Then
'7'' or 10'' tablet
Else
'phone
End If
End Sub
Sub GetDevicePhysicalSize As Float
Dim lv As LayoutValues
lv = GetDeviceLayoutValues
Return Sqrt(Power(lv.Height / lv.Scale / 160, 2) + Power(lv.Width / lv.
Scale / 160, 2))
End Sub

This is accurate on my Insignia 10.1 inch tablet and on my Samsung Galaxy S7 Phone but on my 7"
Nexus Tablet GetOrientation reports the same as the Phone and GetDevicePhysicalSize says 6.9" (Tablet).

I am really trying to follow the rules here. It seems like, 99.9% of the time you can just check if Activity.width is greater than Activity.Height. I am told this is useless without the scale but I am only running 66% using the recommended method.

I just need to move some views around depending on orientation.

How do I reliably get the screen orientation?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you can calculate the approximate screen size with:
B4X:
GetDeviceLayoutValues.ApproximateScreenSize

It seems like, 99.9% of the time you can just check if Activity.width is greater than Activity.Height. I am told this is useless without the scale
There is no relation to scale here. If you want to check whether the orientation is portrait or landscape then you should use:
B4X:
If 100%x > 100%y Then
 'landscape
Else
 'portrait
End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…