Android Question Need help with a code

MetalOS

Member
Licensed User
Longtime User
Good morning,

I found this piece of code to be able to automatically cut the text contained in an EditText before creating my pdf, only it doesn't recognize the textPaint and layout variables and I don't know if it depends on a particular library. Thank you in advance for your help.

Personnal code:
Dim texte As String = ACEditText1.Text
        Dim largeur As Int = 550 ' largeur de la zone de dessin en pixels
        Dim textPaint As Paint
        textPaint.Initialize
        textPaint.TextSize = 14 / GetDeviceLayoutValues.Scale ' taille du texte en dp

        Dim layout As StaticLayout
        layout.Initialize(texte, textPaint, largeur, Layout.Alignment.ALIGN_NORMAL, 1.0, 0.0, False)

        Dim ligneCount As Int = layout.LineCount
        Dim yPos As Int = 220
        For i = 0 To ligneCount - 1
            Dim lineStart As Int = layout.GetLineStart(i)
            Dim lineEnd As Int = layout.GetLineEnd(i)
            Dim lineText As String = texte.SubString2(lineStart, lineEnd)
            pdf.Canvas.DrawText(lineText, 300, yPos, Typeface.DEFAULT, 14 / GetDeviceLayoutValues.Scale, Colors.Black, "CENTER")
            yPos = yPos + layout.GetLineBottom(i) - layout.GetLineTop(i)
        Next
 

MetalOS

Member
Licensed User
Longtime User
Yet it seems to me that the StaticLayout and Paint classes are available by default in B4A. Or am I wrong...
 
Upvote 0

MetalOS

Member
Licensed User
Longtime User
Here is my full sub


sub_buttonClick:
Private Sub ButtonEnvoyer_Click
    env.CheckAndRequest(env.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    
    If Result Then
        CustomListView1.sv.Height=3000dip
        Dim bmp As B4XBitmap=CustomListView1.sv.Snapshot
        
        pdf.Initialize
        pdf.StartPage(595, 3500) 'A4 size
        Dim Bitmap1 As Bitmap
        Bitmap1.Initialize(File.DirAssets,"logoR.png")
        Dim DestRect As Rect
        DestRect.Initialize(10, 10, 500, 139)
        pdf.Canvas.DrawBitmap(Bitmap1, Null, DestRect)
        
        pdf.Canvas.DrawText("Text 1", 270, 190, Typeface.DEFAULT_BOLD, 20 / GetDeviceLayoutValues.Scale , Colors.Black, "CENTER")
        
        ' Ajouter le texte de l'EditText au PDF
        'Dim texte As String = ACEditText1.Text
        'pdf.Canvas.DrawText(texte, 300, 220, Typeface.DEFAULT, 14 / GetDeviceLayoutValues.Scale, Colors.Black, "CENTER")
        
        Dim texte As String = ACEditText1.Text
        Dim largeur As Int = 550 ' largeur de la zone de dessin en pixels
        Dim textPaint As Paint
        textPaint.Initialize
        textPaint.TextSize = 14 / GetDeviceLayoutValues.Scale ' taille du texte en dp

        Dim layout As StaticLayout
        layout.Initialize(texte, textPaint, largeur, Layout.Alignment.ALIGN_NORMAL, 1.0, 0.0, False)

        Dim ligneCount As Int = layout.LineCount
        Dim yPos As Int = 220
        For i = 0 To ligneCount - 1
            Dim lineStart As Int = layout.GetLineStart(i)
            Dim lineEnd As Int = layout.GetLineEnd(i)
            Dim lineText As String = texte.SubString2(lineStart, lineEnd)
            pdf.Canvas.DrawText(lineText, 300, yPos, Typeface.DEFAULT, 14 / GetDeviceLayoutValues.Scale, Colors.Black, "CENTER")
            yPos = yPos + layout.GetLineBottom(i) - layout.GetLineTop(i)
        Next

        
        pdf.Canvas.DrawText("Texte 2", 270, 250, Typeface.DEFAULT_BOLD, 20 / GetDeviceLayoutValues.Scale , Colors.Black, "CENTER")
        DestRect.Initialize(110, 270, 0, 0)
        DestRect.Width = bmp.Width / bmp.Scale
        DestRect.Height = bmp.Height / bmp.Scale
        pdf.Canvas.DrawBitmap(bmp, Null, DestRect)
        
        
        
        pdf.FinishPage
        
        Dim OrigFormat As String=DateTime.DateFormat  'save orig date format
        DateTime.DateFormat="dd MMM yyyy"
        Dim MyDate As String=DateTime.Date(DateTime.Now)
        
        'CheckBox
        Dim Periode As String
        Periode=""
        If CheckBox1.Checked=True Then
            Periode="AM"
        Else If    CheckBox2.Checked=True Then
            Periode="PM"
        End If
        
        Dim DateSelect As String=btnDate.xLBL.Text
        
        Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "Texte 3" & DateSelect & " - " & Periode & ".pdf", False)
        pdf.WriteToStream(out)
        out.Close
        pdf.Close
        
        Dim FileName As String = "Texte 4"  & DateSelect & " - " & Periode & ".pdf"
        'copy the shared file to the shared folder
        DateTime.DateFormat=OrigFormat  'return to orig date format
        File.Copy(File.DirRootExternal, FileName, Starter.Provider.SharedFolder, FileName)
        Dim email As Email
        email.To.Add("[email protected]")
        email.Subject = "Texte Mail"
        email.Attachments.Add(Starter.Provider.GetFileUri(FileName))
        Dim in As Intent = email.GetIntent
        in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
        StartActivity(in)
    End If
End Sub
 
Upvote 0

MetalOS

Member
Licensed User
Longtime User
I don't know anymore unfortunately. But apparently the StaticLayout class is available in the Android library android.text. Paint class is available in android.graphics android library in KOTLIN or java. So by deduction I said to myself that it must be native on B4A.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
So by deduction I said to myself that it must be native on B4A.
Wrong deduction. They need to be wrapped in a library to be accessible. If that is genuine B4A code that someone has run then there must be a library that implements the classes.
 
Upvote 0

MetalOS

Member
Licensed User
Longtime User
Unfortunately I don't know I had this code for a while. Do you know if it is possible to make this code work differently?
 
Upvote 0
Top