Android Question Change fontScale on elongated screens

Ilya G.

Active Member
Licensed User
Longtime User
The variable scale is equal to 1.78, but for some reason the condition does not work, how to make a comparison correctly?

B4X:
Sub Process_Globals
    Public scale As Double = Round2(GetDeviceLayoutValues.Height / GetDeviceLayoutValues.Width, 2) 'ignore
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        #if Java
            import android.content.*;
            import android.content.res.*;
            @Override
            protected void attachBaseContext(Context newBase) {
                super.attachBaseContext(newBase);
                final Configuration override = new Configuration(newBase.getResources().getConfiguration());
                
                if (override.fontScale > 1.0f) {
                    override.fontScale = 1.0f;
                }
                
                if (_scale != 1.78) {
                    override.fontScale = 0.8f;
                }
                
                applyOverrideConfiguration(override);
            }
        #End If
    End If
End Sub
 

Ilya G.

Active Member
Licensed User
Longtime User
I tried different things, but the condition does not work ?

B4X:
if (_scale.equals("1.78")) {
    override.fontScale = 0.5f;
}

if (_scale.toString().equals("1.78")) {
    override.fontScale = 0.5f;
}

if (_scale.toString().equals("1.78".toString())) {
    override.fontScale = 0.5f;
}

if (_scale.toString().compareTo("1.78") == 0) {
    override.fontScale = 0.5f;
}

if (_scale.toString().compareTo("1.78".toString()) == 0) {
    override.fontScale = 0.5f;
}
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
The resolution without the scale is meaningless.

You need to think in device screen size (inches / centimeters).

I thought this was a simple way to influence the size of the entire application at once. How to make a comparison work?
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
Why the first one doesn’t work, but the second one does?

B4X:
Sub Process_Globals
    Public scale As String = "1.78"
End Sub



if (_scale.equals("1.78")) {
    does not work
}

if ("1.78".equals("1.78")) {
    work
}
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
What you are calling "scale" is the aspect ratio of the device screen, and you seem to be wanting to check if this is above a certain value. Why are you not using something like the following code . . .
B4X:
    Dim aspectRatio As Float = GetDeviceLayoutValues.Height / GetDeviceLayoutValues.Width
    If (aspectRatio > 1.78) Then
        
    End If

Your approach seems to be horrendously complicated and full of risk - checking a truncated floating point number for precise equality is not good practice.
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User

I need to change font scale like in this topic, this is only possible with the InlineJava
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
I does not understand how to make this? I need to set fontScale = 0.8 if aspectRatio > 1.78, and else fontScale = 1.0
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't use Java, but why would this not work . . .
B4X:
    Dim aspectRatio As Float = GetDeviceLayoutValues.Height / GetDeviceLayoutValues.Width
    If (aspectRatio > 1.78) Then
        #if java
           ' Put Java code to change the font scale here . . .
        #End If
    End If

If the aspect ratio is not greater than 1.78 then the font scale will be unchanged - 1.0 presumably.
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
If the aspect ratio is not greater than 1.78 then the font scale will be unchanged - 1.0 presumably.

No, if the user has set the font size to large in the system settings


I already tried this, but I get an error:

B4X:
If GetDeviceLayoutValues.Height / GetDeviceLayoutValues.Width = 1.78 Then
    #if Java
        import android.content.*;
        import android.content.res.*;

        @Override
        protected void attachBaseContext(Context newBase) {
            super.attachBaseContext(newBase);
            final Configuration override = new Configuration(newBase.getResources().getConfiguration());

            if (override.fontScale > 1.0f) {
                override.fontScale = 1.0f;
                applyOverrideConfiguration(override);
            }
        }
    #End If
Else
    #if Java
        import android.content.*;
        import android.content.res.*;

        @Override
        protected void attachBaseContext(Context newBase) {
            super.attachBaseContext(newBase);
            final Configuration override = new Configuration(newBase.getResources().getConfiguration());

            override.fontScale = 0.8f;
            applyOverrideConfiguration(override);
        }
    #End If
End If

B4X:
B4A Version: 12.80
Parsing code.    (0.38s)
    Java Version: 11
Building folders structure.    (0.16s)
Running custom action.    (0.29s)
Compiling code.    (1.01s)
Compiling layouts code.    (0.01s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Compiling resources    (0.19s)
Linking resources    (0.94s)
Compiling generated Java code.    Error
src\phbk\sales\main.java:503: error: method attachBaseContext(Context) is already defined in class main
                protected void attachBaseContext(Context newBase) {
                               ^
Note: src\phbk\sales\main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

javac 11.0.1
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have been thinking a lot about your problem. If I understand correctly you intend to change the font settings in Accessibility options if your app runs on "elongated" devices. Isn't this very bad practice? If I had a vision impairment and had set the accessibility font size to a particular value I would not like to have to reset it every time after running your app. If I had not changed the font setting I would not like to find that the font size was changed in all my apps after running your app. Surely the correct approach would be to adjust the font sizes in the appropriate views within your app. Yes, very inconvenient for you but that is better than inconveniencing your customer.

If I have misunderstood what you are doing then I appologise - sorry to have wasted your time.
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
I checked, this does not affect the system settings, only my application.
In any case, thanks for the help, I didn’t think that comparing variables in Java is such a complex operation that I can’t handle
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Your error in post #13 is caused by you having the same inline code in 2 places.
When you use #If Java ... #End If, it will include the enclosed java into the source file (normally at the end). This is irrespective of any B4X code you surround it with, as in your code you check a value, and want to include one block for = 1.78 or another block for <> 1.78.
In reality both inline blocks are compiled into the code.

Set a variable in B4X and use that in the inline code.
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
Can anyone help compare these variable in Java?

B4X:
Sub Process_Globals
    Public scale As Double = Round2(GetDeviceLayoutValues.Height / GetDeviceLayoutValues.Width, 2) 'ignore
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        #if Java
            import android.content.*;
            import android.content.res.*;
            @Override
            protected void attachBaseContext(Context newBase) {
                super.attachBaseContext(newBase);
                final Configuration override = new Configuration(newBase.getResources().getConfiguration());
               
                if (override.fontScale > 1.0f) {
                    override.fontScale = 1.0f;
                }
               
                if (_scale != 1.78) {                        <<<<<<<<<<<<<<< THIS
                    override.fontScale = 0.8f;
                }
               
                applyOverrideConfiguration(override);
            }
        #End If
    End If
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…