Android Question Character Height in DIP

vbmundo

Well-Known Member
Licensed User
Hi,

Every user have a different SmartPhone or Tablet.

Every user can set a different font size (normal, large)

I'm using a Flexgrid Table, and I can set the Row Height but... How can I know what font size in DIP have the user ?

Regards
 

vbmundo

Well-Known Member
Licensed User
Accessibility.GetUserFontScale returns the user scale settings. Every font size is multiplied by this scale.

You can use Canvas.MeasureStringWidth / Height or StringUtils.MeasureMultilineTextHeight to calculate the actual dimensions.

Accessibility is not recognized, what lib I need to attach ?
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
I'm trying to test the Canvas.MeasureStringWidth / Height

I wrote the example that brings the command

obviously, first defining objects

B4X:
    t="TEXTO a MOSTRAR"
    Width=Canv.MeasureStringWidth(t,Typeface.DEFAULT,14)
    height=Canv.MeasureStringHeight(t,Typeface.DEFAULT,14)
    Rect1.Initialize(100dip,100dip,100dip+Width,100dip+height)
    Canv.DrawRect(Rect1,Colors.White,True,0)
    Canv.DrawText(t,Rect1.Left,Rect1.bottom,Typeface.DEFAULT,14,Colors.Blue,"Left")

But I receive this error in Runtime

B4X:
Error occurred on line: 45 (Articulos)
java.lang.NullPointerException
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper.MeasureStringWidth(CanvasWrapper.java:343)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at electro.merlo.articulos.afterFirstLayout(articulos.java:102)
    at electro.merlo.articulos.access$000(articulos.java:17)
    at electro.merlo.articulos$WaitForLayout.run(articulos.java:80)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4947)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
    at dalvik.system.NativeStart.main(Native Method)

This is the Error Line

B4X:
Width=Canv.MeasureStringWidth(t,Typeface.DEFAULT,14)

What's wrong ?

Thanks
 
Upvote 0
Top