Android Question Placement of controls at runtime

saeed10051

Active Member
Licensed User
Longtime User
Dear All,
I have an app in which i am putting swiftbutton programatically at a location which is almost at the middle of the screen, i am using following code.
B4X:
Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    
    mpos = (lv.Height/4.3)
    For i = 1 To 26
        
        lpos = 15+(colnum-1)*35+(colnum-1)*xoffset  '35 is swiftbutton width
        tpos = mpos + (rownum -1)*50
        Addbutton(lpos*1dip, tpos*1dip,i,z)
My problem is that it is showing the buttons nicely on my samsung A50, but moving the buttons up on S8 and further up on TabS2. I think this is because of different screen sizes and resolutions.
Is there a way to place a control at the middle (screen height) of screen whatever the screen resolution and size.
 

Geezer

Active Member
Licensed User
Longtime User
activity.height / 2 is the middle ( or 50%y )

Get that value and decrease it by the height of your button
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
Thanks Geezer
But this height that get by actvity.height/2 is in pixels while my layout is in dips. When i apply the above formula the buttons go outside the screen. How can i convert this pixel value to dips
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you are using numeric values for view dimensions you must use dip values like in this line:
lpos = 15dip+(colnum-1)*35dip+(colnum-1)*xoffset '35 is swiftbutton width
instead of
lpos = 15+(colnum-1)*35+(colnum-1)*xoffset '35 is swiftbutton width
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
When you are using numeric values for view dimensions you must use dip values like in this line:
lpos = 15dip+(colnum-1)*35dip+(colnum-1)*xoffset '35 is swiftbutton width
instead of
lpos = 15+(colnum-1)*35+(colnum-1)*xoffset '35 is swiftbutton width
Hi Klaus
i have multiplying by dips in the following line, it is there in my code above also
B4X:
Addbutton(lpos*1dip, tpos*1dip,i,z)
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
i am using following function for adding buttons
B4X:
Sub Addbutton(Left As Int, Top As Int, Tag As Object, listcount As Int) As SwiftButton
    Activity.LoadLayout("1")
    
    SwiftButton1.mBase.Left = Left
    SwiftButton1.mBase.Top = Top
    
    SwiftButton1.Tag = Tag
    SwiftButton1.xlbl.Text = Chr(65+listcount)
    
    Return SwiftButton1
End Sub
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
What is this line supposed to ?
mpos = (lv.Height/4.3)
lv.Height is in pixels.
My advice is to calculate everything in pixels and use dip values for numbers.
Hi Klaus
my problem is that the swift buttons that i am generating at runtime are not showing correctly on some devices while on other they appear perfect. I have attached two files here, one is showing the display of buttons on samsung A50 which is perfect, while the other is showing on samsung A70 which has moved up. I am using the layout height value to handle this placement issue. i have checked the height value for both A50 and A70, it is showing 810 as height. The scale value for A50 is 2.25 while for A70 it is 1.968.
I am using following code
B4X:
Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    'this section is added to get the scale of the screen on which app launches
    Dim r As Reflector
    Dim scale As Float
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getDisplayMetrics")
    scale = r.GetField("density")
    
    mpos = (lv.Height*0.7)/(scale)  'scaling for various devices, button top will be at the 70% of screen height
    
    For i = 1 To 26
        
        lpos = 15+(colnum-1)*35+(colnum-1)*xoffset  '35 is swiftbutton width
        tpos = mpos + (rownum -1)*60
        Addbutton(lpos*1dip, tpos*1dip,i,z)
        z=z+1
        colnum = colnum +1
        If colnum > intendedcols Then rownum = rownum+1
        If colnum > intendedcols Then colnum = 1
    Next
    
    Sub Addbutton(Left As Int, Top As Int, Tag As Object, listcount As Int) As SwiftButton
    Activity.LoadLayout("1")
    
    SwiftButton1.mBase.Left = Left
    SwiftButton1.mBase.Top = Top
    
    SwiftButton1.Tag = Tag
    SwiftButton1.xlbl.Text = Chr(65+listcount)
    'SwiftButton1.SetColors(xui.Color_Blue, xui.Color_Cyan)
    Return SwiftButton1
End Sub
Am i missing some factor or property that i need to consider, the ppi for samsung A50 is 403ppi and for A70 it is 393ppi.
Please can you help me out in this.
 

Attachments

  • display on A70.jpg
    display on A70.jpg
    36.1 KB · Views: 130
  • display on A50.png
    display on A50.png
    322.5 KB · Views: 121
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
mpos value for A50 is coming out to be 288 while for A70 it is 288, still for A70 the buttons seem moved higher than the middle point. For A50 it seems that the height of screen is 360 dips but for A70 i cannot get that value in dips.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Your approach is very complicated.
1. Don't use this scale, scale = r.GetField("density"), it will give you
If you really want to use a scale you should use GetDeviceLayoutValues.Scale and this will give you a value of '3' for both devices!

2. Again, calculate in pixels and use dip values.
Or post a small project showing the problem.

Why not using:
B4X:
    mpos = 70%y  'scaling for various devices, button top will be at the 70% of screen height
   
    For i = 1 To 26
       
        lpos = 15dip + (colnum-1) * 35dip +(colnum-1) * xoffset  '35 is swiftbutton width
        tpos = mpos + (rownum -1) * 60dip
        Addbutton(lpos, tpos, i, z)
        z=z+1
        colnum = colnum +1
        If colnum > intendedcols Then rownum = rownum+1
        If colnum > intendedcols Then colnum = 1
    Next
the value xoffset must be in pixels.
Not tested.
Your routines could be simplified.
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
Your approach is very complicated.
1. Don't use this scale, scale = r.GetField("density"), it will give you
If you really want to use a scale you should use GetDeviceLayoutValues.Scale and this will give you a value of '3' for both devices!

2. Again, calculate in pixels and use dip values.
Or post a small project showing the problem.

Why not using:
B4X:
    mpos = 70%y  'scaling for various devices, button top will be at the 70% of screen height
  
    For i = 1 To 26
      
        lpos = 15dip + (colnum-1) * 35dip +(colnum-1) * xoffset  '35 is swiftbutton width
        tpos = mpos + (rownum -1) * 60dip
        Addbutton(lpos, tpos, i, z)
        z=z+1
        colnum = colnum +1
        If colnum > intendedcols Then rownum = rownum+1
        If colnum > intendedcols Then colnum = 1
    Next
the value xoffset must be in pixels.
Not tested.
Your routines could be simplified.
Thanks Klaus,
let me try that. here you have written mpos = 70%y
will it work directly or i need to specify some thing else also
 
Upvote 0
Top