Android Question [B4X] Scroll background color by words in a string.

Juan Perz

Member
gif_mio_02-gif.98007


Hi.
I am trying to do this that I have done in B4J ( https://www.b4x.com/android/forum/threads/scroll-background-color-by-words-in-a-string.120845) in B4A, but not run and it gives me the next error:


*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 112 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
at b4a.ScrollBackgroundColorWords.main._addbackgroundpanels(main.java:583)
at b4a.ScrollBackgroundColorWords.main._activity_create(main.java:442)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.ScrollBackgroundColorWords.main.afterFirstLayout(main.java:105)
at b4a.ScrollBackgroundColorWords.main.access$000(main.java:17)
at b4a.ScrollBackgroundColorWords.main$WaitForLayout.run(main.java:83)
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:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

** Activity (main) Resume **


In the Designer I have exactly the same Views.
What am I doing wrong?
My B4A code is the following:


B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Background_Color_Words
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: true
#End Region

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

Sub Process_Globals
    Private xui As XUI
End Sub

Sub Globals
    Private WordsToHighlight As B4XSet
    Dim BBCodeView1 As BBCodeView
    Private TextEngine As BCTextEngine
    Dim myString As String = "Put here the string you want to use."
    Dim listIndex As List
    Dim listWordsSpaces As List
    Private Button1 As B4XView
    Dim count As Int = 0
    Dim sb1 As StringBuilder
    Dim sb2 As StringBuilder
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    listIndex.Initialize
    listIndex = getIndexListWordsSpaces(myString)
    listWordsSpaces.Initialize
    listWordsSpaces = getListWordsSpaces(myString, listIndex)
    sb1.Initialize
    sb2.Initialize
    sb1.Append("[TextSize=20]") ' Put the text size here
    sb1.Append(myString)
    sb1.Append("[/TextSize]")
    TextEngine.Initialize(Activity)
    WordsToHighlight = B4XCollections.CreateSet2(listWordsSpaces)
    BBCodeView1.Text = sb1.ToString
    AddBackgroundPanels
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    WordsToHighlight = B4XCollections.CreateSet2(Array(listWordsSpaces.Get(count),""))
    count = count + 1
    sb2.Initialize
    sb2.Append(sb1.ToString)
    If count = listIndex.Size Then
        count = 0
    Else
        sb2.Insert(listIndex.Get(count-1)+13, "[plain]")
        sb2.Insert(listIndex.Get(count)+7+13, "[/plain]")
        BBCodeView1.Text = sb2.ToString
    End If
    AddBackgroundPanels
End Sub

Sub getIndexListWordsSpaces(str As String) As List
    Dim indexF As List
    Dim index As Int = 0
    Dim index0 As Int
    indexF.Initialize                                
    indexF.Add(index)                              
    index = str.IndexOf2(" ",index+1)            
    indexF.Add(index)                              
    Do While index <> -1                              
        index0 = index                              
        index = str.IndexOf2(" ",index+1)        
        If index > index0 + 1 Then                  
            indexF.Add(index0+1)                    
            indexF.Add(index)                      
        Else                                          
            If index = -1 Then                        
                indexF.Add(index0+1)                
            Else                                      
                indexF.Add(index)                  
            End If
        End If
    Loop
    If str.SubString(indexF.Get(indexF.Size-1)) = "" Then
        indexF.RemoveAt(indexF.Size-1)
    End If
    Return indexF
End Sub

Sub getListWordsSpaces(str As String, listIndexM As List) As List
    Dim listWordsSpaces As List
    listWordsSpaces.Initialize
    For i=0 To listIndexM.Size-2
        listWordsSpaces.Add(str.SubString2(listIndexM.Get(i),listIndexM.Get(i+1)))
    Next
    listWordsSpaces.Add(str.SubString(listIndexM.Get(i)))
    Return listWordsSpaces
End Sub

Private Sub AddBackgroundPanels
    For Each x As B4XView In BBCodeView1.sv.ScrollViewInnerPanel.GetAllViewsRecursive
        If x.Tag = "background" Then
            x.RemoveViewFromParent
        End If
    Next
    Dim scale As Float = TextEngine.mScale
    For Each line As BCTextLine In BBCodeView1.Paragraph.TextLines
        For Each un As BCUnbreakableText In line.Unbreakables
            For Each s As BCSingleStyleSection In un.SingleStyleSections
                If WordsToHighlight.Contains(s.Run.Text) Then
                    Dim pnl As B4XView = xui.CreatePanel("")
                    pnl.Color = 0xFF00D2FF
                    pnl.Tag = "background"
                    BBCodeView1.sv.ScrollViewInnerPanel.AddView(pnl, BBCodeView1.Padding.Left + s.AbsoluteStartX / scale - 2dip, _
                     BBCodeView1.Padding.Top + (line.BaselineY - s.MaxHeightAboveBaseLine) / scale - 2dip, _
                        s.Width / scale + 4dip, (s.MaxHeightBelowBaseLine + s.MaxHeightAboveBaseLine) / scale + 4dip)
                    pnl.SendToBack
                End If
            Next
        Next
    Next
End Sub
 
Last edited:

Juan Perz

Member
1. Done
2. Done
3. Done - Added file zip too. Yes, simpler, and very very good, but I keep doing something wrong because in B4A I keep making the same error. Any solution?
 

Attachments

  • B4J-ScrollBackgroundColorWords.zip
    4.1 KB · Views: 75
  • B4A-ScrollBackgroundColorWords.zip
    11.1 KB · Views: 70
  • B4X-B4XPages-ScrollBackgroundColorWords.zip
    10.5 KB · Views: 78
Last edited:
Upvote 0
Top