This code works as it is.
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
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:
Error occurred on line: 93 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.example.main._drawtextlinebyline(main.java:423)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:6207)
at android.view.View$PerformClick.run(View.java:23639)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getHeight()' on a null object reference
at b4a.example.main.lheight(main.java:531)
... 21 more
Any idea how to fix it
Thank you