device screen dimensions

Avon

Member
Licensed User
Longtime User
How are the screen dimensions for the device obtained please?
 

pluton

Active Member
Licensed User
Longtime User
Maybe like this:
Designer - Basic4android Wiki

B4X:
Sub Globals
   Dim DeviceType As Int
   Dim Dim480x320x160 As Int: Dim480x320x160 = 1 ' 4" phones lo-res
   Dim Dim800x480x240 As Int: Dim800x480x240 = 2 ' 4" phones
   Dim Dim800x480x160 As Int: Dim800x480x160 = 3 ' 7" tablets
   Dim Dim1280x800x160 As Int: Dim1280x800x160 = 4 ' 10" tablets
   Dim LayoutVal As LayoutValues
End Sub
...
Sub Activity_Create(FirstTime As Boolean)
   LayoutVal = GetDeviceLayoutValues   
   Dim Dens As Float   
   Dens = Density
   If LayoutVal.Width >= 1280 AND Dens = 1 Then
      LayoutWidth = 1280
      Activity.LoadLayout("1280x800x160")
      DeviceType = Dim1280x800x160
   Else If LayoutVal.Width > 599 Then
      LayoutWidth = 800
      If Dens = 1.5 Then
         Activity.LoadLayout("800x480x240")
         DeviceType = Dim800x480x240
      Else
         Activity.LoadLayout("800x480x160")
         DeviceType = Dim800x480x160
      End If
   Else
      LayoutWidth = 480
      Activity.LoadLayout("480x320x160")
      DeviceType = Dim480x320x160
   End If
End Sub
 
Upvote 0

billy047

Member
Licensed User
Longtime User
Sub Globals
Dim lv As LayoutValues
Dim LayLoad As Byte
End Sub

Sub Activity_Create(FirstTime As Boolean)
If firsttime = True Then
'Determina el layout
lv = GetDeviceLayoutValues
If (lv.width=320) AND (lv.height=480) AND (lv.Scale=1) Then
activity.LoadLayout("lay320x480s1")
LayLoad=1
Else If (lv.width=480) AND (lv.height=800) AND (lv.Scale=1) Then
activity.LoadLayout("lay480x800s1")
LayLoad=2
Else If (lv.width=480) AND (lv.height=800) AND (lv.Scale=1.5) Then
activity.LoadLayout("lay480x800s15")
LayLoad=3
Else If (lv.width=800) AND (lv.height=1280) AND (lv.Scale=1) Then
activity.LoadLayout("lay800x1280s1")
LayLoad=4
End If
End Sub

Regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Two comments about this code:
B4X:
Sub Globals
Dim lv As LayoutValues
Dim LayLoad As Byte
End Sub

Sub Activity_Create(FirstTime As Boolean)
If firsttime = True Then
'Determina el layout
lv = GetDeviceLayoutValues
If (lv.width=320) AND (lv.height=480) AND (lv.Scale=1) Then
activity.LoadLayout("lay320x480s1")
LayLoad=1
Else If (lv.width=480) AND (lv.height=800) AND (lv.Scale=1) Then
activity.LoadLayout("lay480x800s1")
LayLoad=2
Else If (lv.width=480) AND (lv.height=800) AND (lv.Scale=1.5) Then
activity.LoadLayout("lay480x800s15")
LayLoad=3
Else If (lv.width=800) AND (lv.height=1280) AND (lv.Scale=1) Then
activity.LoadLayout("lay800x1280s1")
LayLoad=4
End If
End Sub

Regards
1. This code will not work properly as you are only loading the layout when FirstTime is True. You should always load the layout. FirstTime is true only when the process is created.
2. You should use layout variants instead of multiple layout files. It will be easier to manage and cleaner.
 
Upvote 0
Top