Android Question Force Landscape for Tablet and Portrait for Phone

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello All,

How can we achieve the subject?

I want to have one app for phone and tablet but I want to force the app to be used in portrait when run on a phone and on landscape when run on a tablet. Is it possible?

Regards,
Haris
 

Haris Hafeez

Active Member
Licensed User
Longtime User
Worked out a solution like the below:
B4X:
Private Sub InitForTablet
    Dim nativeActivity As JavaObject = GetContext
    nativeActivity.RunMethod("setRequestedOrientation", Array As Object(0)) 'force to landscape
    Activity.LoadLayout("tablet_main")
End Sub
Private Sub InitForPhone
    Dim nativeActivity As JavaObject = GetContext
    nativeActivity.RunMethod("setRequestedOrientation",Array As Object(1)) 'force to portrait
    Activity.LoadLayout("phone_main")
End Sub


Sub GetContext As JavaObject
    Dim jo As JavaObject
    jo.InitializeContext
    Return jo
End Sub

Use the InitForPhone when I detect it is a phone and InitForTablet otherwise.
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
B4X:
dim phonewidth, phoneheight as int
dim p as phone
phonewidth=GetDeviceLayoutValues.Width
   phoneheight=GetDeviceLayoutValues.Height
if phonewidth > phoneheight then
p.SetScreenOrientation(0)
else
p.SetScreenOrientation(1)
not tested but i think will work
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I use:
B4X:
Public DeviceSize As Double


DeviceSize = lv.ApproximateScreenSize
If DeviceSize >= 6 Then
    ' tablet
Else
    ' phone
End If
I use a variable because I use it in different routines.
Public DeviceSize As Double in Process_Globals
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…