Android Question Text sizes on different screens

grafsoft

Well-Known Member
Licensed User
Longtime User
Could you show the differences between portrait and landscape !
How do you define the view dimensions ?
Are they different for portrait and landscape ?
I use functions like this:

B4X:
Sub textsyz (i As Int)
   Dim ry,rt As Float
   ry=GetDeviceLayoutValues.width/800
   rt=ry / GetDeviceLayoutValues.Scale
   Return i * rt
End Sub

Sub dobutton (b As Button, s As String)
   b.Color=Main.a400color
   b.TextColor=Main.btntxtcolor
   b.text=s.ToUpperCase
   b.Typeface=Main.tfm
   b.TextSize=textsyz(Main.bodysize1)
End Sub
And then I set main.bodysize1 to 24 or whatever.

I think I could

set ry differently,

if portrait: ry=GetDeviceLayoutValues.width/800
if landscape: ry=GetDeviceLayoutValues.height/800

Or, I could try ry=(GetDeviceLayoutValues.width + GetDeviceLayoutValues.height)/800

What I am wondering: For labels etc. I just use 40dip or 30%x, 10%y, or whatever, and it works perfectly on all devices, tablets or cells.

Therefore I think I think too complicated.

You wrote in this forum, tha one should not use dip for text sizes. Here, I read about SP http://stackoverflow.com/questions/7107920/why-should-we-use-sp-for-font-sizes-in-android

The text size is important for me, because I serve students (read everything) and older professors (read only on tablets, need bigger fonts).

This must be a problem many of us have. I think I just do not find the solution ....
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Can you post a small project showing the problem ?
It's difficult to give a concrete advice with code snippets.
In this line b.TextSize=textsyz(Main.bodysize1), what is Main.bodysize1 ?

You say this:
if portrait: ry=GetDeviceLayoutValues.width/800
if landscape: ry=GetDeviceLayoutValues.height/800
but I don't find this in your code !?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I would like to recommend Klaus' code which I use now for a while. Works good in all orientations. I put it in two subs: For Edittext's and Label's (Buttons work, too just adapt one of the subs). Change the "0.8" which means 80% to a value you like.

B4X:
Sub SetLabelTextSize(ex As Label, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    Dim h As Int

    ex.Text = txt
    ex.TextSize = 72
    dt = ex.TextSize
    h = stu.MeasureMultilineTextHeight(ex, txt)
    Do While dt > limit OR h > ex.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(ex, txt)
        If h > ex.Height Then
            ex.TextSize = ex.TextSize - dt
        Else
            ex.TextSize = ex.TextSize + dt
        End If
    Loop
    ex.TextSize=ex.textsize * 0.8
End Sub

Sub SetEditTextSize(ex As EditText, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    Dim h As Int

    ex.Text = txt
    ex.TextSize = 72
    dt = ex.TextSize
    h = stu.MeasureMultilineTextHeight(ex, txt)
    Do While dt > limit OR h > ex.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(ex, txt)
        If h > ex.Height Then
            ex.TextSize = ex.TextSize - dt
        Else
            ex.TextSize = ex.TextSize + dt
        End If
    Loop
    ex.TextSize=ex.textsize *0.8
End Sub
 
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
This is a very good solution - but my designer wants a fixed font size on all labels and buttons etc. This font size should vary according to the screen size.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem in your code is that you use one reference width 800 for both orientations.
When you change the orientation the width changes so you need to use a different reference width for each orientation.
But as you want to have the text size proportional to the screen size, you can use this code.
B4X:
Dim TextScale As Double
TextScale = GetDeviceLayoutValues.ApproximateScreenSize / 3.5
Button1.TextSize= 24 * TextScale
3.5 is the reference screen size, you should change it to your needs.
 
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
I found as similar solution:

Sub textsyz (i As Int)
Dim ry,rt As Float
ry=(GetDeviceLayoutValues.width + GetDeviceLayoutValues.height) / 1500
rt=ry / GetDeviceLayoutValues.Scale
Return i * rt
End Sub

if I feed this sfunction with a value of 30, it works OK on 2 devices. Further tests will follow.
 
Upvote 0
Top