Android Question Font width

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi All
I would like either to use, in my app, a fixed width font, like Courier, or to know a way to calculate the width of a text in the screen. Thanks
 

LucaMs

Expert
Licensed User
Longtime User
For Courier: http://www.b4x.com/android/forum/threads/add-new-fornts.8497/#post-47471

B4X:
Private default_font, default_font_bold, default_font_italic As Typeface
Label1.Typeface = default_font

Private Sub SetFont(FontFileName As String)
    default_font = Typeface.LoadFromAssets(FontFileName)
    default_font_bold  = Typeface.CreateNew(default_font, default_font.STYLE_BOLD)
    default_font_italic = Typeface.CreateNew(default_font, default_font.STYLE_ITALIC)
End Sub
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
TypeFace.MONOSPACE is a fixed width font, or you can use the canvas object to measure the length of the string.

B4X:
    Dim Bmp As Bitmap
    Bmp.InitializeMutable(10Dip,10Dip) ' Size is irrelevant just to initialize the canvas
    Dim Cnv As Canvas
    Cnv.Initialize2(Bmp)
    Log(Cnv.MeasureStringWidth("Test String",Typeface.DEFAULT,12))
 
Upvote 0
Top