Android Question Multicolored Edittext control

Fusseldieb

Active Member
Licensed User
Longtime User
Hi all,
it's possible to multicolor a text in a Edittext control?
Like this: "Hello whole World!"
If it's not possible, are they any libraries to make this possible?

Thanks in advance ;)
 

Fusseldieb

Active Member
Licensed User
Longtime User
Exactly this what I wanted! :D
I have one more qustion: I want to replace the strings into color while the user is writing. But how can I make this?
When I do "rc.Initialize" on event "TextChanged", then the app crashes...
Thanks in advance ^^
 
Upvote 0

Fusseldieb

Active Member
Licensed User
Longtime User
I have already found the solution.
It's a little bit buggy, but works.
It's highlights the B4A code.
B4X:
Sub Process_Globals
    Dim rs As RichString
    Dim rsf As RichStringFormatter
End Sub

Sub Globals
    Private EditText1 As EditText
    Private Button1 As Button
    Dim WholeCode As String
    Dim contador As Int
    Private conta As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainActivity")
    EditText1.Color=Colors.Transparent
    EditText1.TextColor=Colors.Black
    rsf.Initialize("{example}")
    rsf.AddToken("{Blue}", rsf.FORMAT_COLOR, Colors.Blue)
    rsf.AddToken("{Red}", rsf.FORMAT_COLOR, Colors.rgb(128,0,0))
    rsf.AddToken("{Green}", rsf.FORMAT_COLOR, Colors.rgb(0,128,0))
    rsf.AddToken("{Sea}", rsf.FORMAT_COLOR, Colors.rgb(0,139,139))
    rsf.AddToken("{Orange}", rsf.FORMAT_COLOR, Colors.rgb(255,97,3))
    rsf.AddToken("{Gray}", rsf.FORMAT_COLOR, Colors.rgb(85,85,85))
    ReformatText
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ReformatText
    conta = EditText1.SelectionStart
     WholeCode = EditText1.Text
     WholeCode=WholeCode.Replace("{Blue}", "")
     WholeCode=WholeCode.Replace("End","{Blue}End{Blue}")
     WholeCode=WholeCode.Replace("Sub","{Blue}Sub{Blue}")
     WholeCode=WholeCode.Replace("If","{Blue}If{Blue}")
     WholeCode=WholeCode.Replace("Then","{Blue}Then{Blue}")
     WholeCode=WholeCode.Replace("Try","{Blue}Try{Blue}")
     WholeCode=WholeCode.Replace("As","{Blue}As{Blue}")
     WholeCode=WholeCode.Replace("Catch","{Blue}Catch{Blue}")
  
     WholeCode=WholeCode.Replace("Boolean","{Sea}Boolean{Sea}")
     WholeCode=WholeCode.Replace("String","{Sea}String{Sea}")
     WholeCode=WholeCode.Replace("Int","{Sea}Int{Sea}")
     WholeCode=WholeCode.Replace("Double","{Sea}Double{Sea}")
  
     WholeCode = RegexReplace("^([^']*)('+)(.*)(\r?\n|$)", WholeCode, "$1{Green}$2$3{Green}$4")
     WholeCode = RegexReplace("^([^#]*)(#+)(.*)(\r?\n|$)", WholeCode, "$1{Orange}$2$3{Orange}$4")
  
     WholeCode = RegexReplace("^([^:]*)(:+)(.*)(\r?\n|$)", WholeCode, "$1{Red}$2$3{Red}$4")
  
     'WholeCode=WholeCode.Replace("True","{Red}True{Red}")
     'WholeCode=WholeCode.Replace("False","{Orange}{Red}False{Red}")
  
     EditText1.Text = WholeCode
     rs.Initialize(EditText1.text)
     rs = rsf.Format(rs)
     EditText1.Text = rs
    setSelection(EditText1,conta,conta )
End Sub

Sub RegexReplace(Pattern As String, Text As String, Replacement As String) As String
  Dim m As Matcher
  m = Regex.Matcher2(Pattern, Regex.MULTILINE, Text)
  Dim jo As JavaObject = m
  Return jo.RunMethod("replaceAll", Array(Replacement))
End Sub

Sub Button1_Click
    ReformatText
End Sub

Sub EditText1_TextChanged (Old As String, New As String)
      contador = contador + 1
    If contador = 20 Then
    ReformatText
    contador = 0
    End If
End Sub


Sub setSelection(edt As EditText, StartIndex As Int, EndIndex As Int)
    Dim jo = edt As JavaObject
    jo.RunMethod("setSelection", Array As Object(StartIndex, EndIndex))
End Sub
But, has anyone a improvement? This here It's very slow on higlihting colors.
I have put to color every 20 characters, but then every 20 seconds the app has a little delay.

Please don't bump your threads.
I did not know. Sorry about that.
 
Upvote 0
Top