Android Question Draw multiline text from a label to an imageview

mlc

Active Member
Licensed User
Longtime User
This code works as it is.
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private NativeMe As JavaObject
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim im As ImageView
    Dim lbl As Label
    Dim canvas1 As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    If FirstTime Then
        NativeMe.InitializeContext
    End If
    im.Initialize("")
    Activity.AddView(im, 30dip, 30dip, 200dip, 100dip)
    im.Color = Colors.White
    canvas1.Initialize(im)
           
    lbl.Initialize("")
    Activity.AddView(lbl, 30dip, 145dip, 200dip, 52dip)
    lbl.Color = Colors.LightGray
    lbl.Text = "I do not understand what is happening, please someone can help me with this problem before my PC goes flying. Thank you"
    lbl.TextColor = Colors.Black
    lbl.Padding = Array As Int (2dip, 0, 2dip, 2dip)
    'Due to the tests, I know this is the font size, but I need to know the font size at runtime,
    'since in the real project the text and width is changing between each calls.
    lbl.TextSize = 10

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_click
       
'    Dim ii, Sizes As Int
'    Dim su As StringUtils
'    For ii = 20 To 1 Step -1
'        lbl.TextSize = ii
'        Sizes = su.MeasureMultilineTextHeight(lbl, lbl.Text)
'        If Sizes < lbl.Height Then
'            lbl.TextSize = ii
'            Exit
'        End If
'    Next
    DrawTextLineByLine(lbl)
   
    'can not use CallSubDelayed2, because in the real project there are more code after the draw.
'    CallSubDelayed2(Me,"DrawTextLineByLine",lbl)
   
End Sub

Private Sub DrawTextLineByLine(ViewName As View)
    Log(DateTime.Now)
    Dim Source As Reflector
    Source.Target = ViewName
   
    Dim Text As String
    Text = Source.RunMethod("getText")
   
    Dim TLines As Int
    TLines = Source.RunMethod("getLineCount")
   
    'Check the height of the layout
    Dim HeightPerLine As Int
    HeightPerLine = NativeMe.RunMethod("lheight", Array(Source.RunMethod("getLayout")))
    HeightPerLine = HeightPerLine / TLines

    Dim Line As Int
    For Line = 0 To TLines - 1
        Dim jolnopuedo As String
        jolnopuedo = NativeMe.RunMethod("getLineOfText", Array(Text, Line, Source.RunMethod("getLayout")))
        Log(jolnopuedo)
        canvas1.DrawText(jolnopuedo, 2dip, HeightPerLine * (Line + 1), lbl.Typeface, lbl.TextSize, Colors.black, "LEFT")
    Next
    Activity.Invalidate
    Log(DateTime.Now)
End Sub


#If JAVA

import android.text.Layout;
import java.lang.String;

public int lheight(Layout layout) {
    int alto1 = layout.getHeight();
    return alto1;
}

public String getLineOfText(String fullText, int lineNumber, Layout layout) {
    int LineStart = layout.getLineStart(lineNumber);
    int LineEnd = layout.getLineEnd(lineNumber);    
    return fullText.substring(LineStart, LineEnd);
}
#End If



The problem is that I must change the width and text of the label at runtime, so in activity_create I comment the line (49) lbl.TextSize = 10 and uncomment on activity_click line (63) to (72), the code of su.MeasureMultilineTextHeight and I have this error:


Any idea how to fix it
Thank you
 

mlc

Active Member
Licensed User
Longtime User
Hello Erel,
In a For Next, I have to draw a text to a imageview that resizes in each loop.
Text, width and font size change for each case. And then I draw the bitmap into a PDF.
I have used your Sub DrawView (cvs As Canvas, v As View)
In the first loop all correct, but from the second to the end, the font size is correct but not the width, the wordwrap in not correct.
DrawView also does not work on activity_resume

CallSubDelayed has not worked either. There is more code after drawing.

I think that there are simpler ways to implement it.
How would you implement it?

Thanks for your help
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…