Bluetooth Chat - Text in txtLog window colored with RichString

raphael75

Active Member
Licensed User
Longtime User
Hello,

I try to adapt the Serial/Bluetooth chat example with colored text in the txtLog window (EditText type).

Here is an extract of the code in the ChatActivity module (LogMessage Sub is modified):
B4X:
[...]
Sub AStream_NewData (Buffer() As Byte)
   LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub
[...]

Sub btnSend_Click
   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   txtInput.SelectAll
   txtInput.RequestFocus
   LogMessage("Me", txtInput.Text)
End Sub

Sub LogMessage(From As String, Msg As String)
   'ORIGINAL txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
   
   ' TEST Time Stamp
   DateTime.TimeFormat = "HH:mm:ss.SSS"
   Dim timeNow As String
   timeNow = DateTime.Time(DateTime.Now)
   
   ' TEST TextColor
   Dim rs As RichString
   rs.Initialize(txtLog.Text & "{TxtColor}" & "(" & timeNow & ") " & From & ": " & Msg & "{TxtColor}" & CRLF) 
   'TEST rs.Initialize("{TxtColor}" & "(" & timeNow & ") " & From & ": " & Msg & "{TxtColor}") 
   If From = "Me" Then rs.Color2(Colors.Blue, "{TxtColor}")
   If From = "You" Then rs.Color2(Colors.Red, "{TxtColor}")
   txtLog.Text = rs
   'TEST txtLog.Text = txtLog.Text & rs & CRLF
   
   'ORIGINAL
   txtLog.SelectionStart = txtLog.Text.Length
End Sub

The result of this code is that the last line in the txtLog window has the correct color, but the previous lines become black. It seems like the text format of the previous lines disappears in the command rs.Initialize(txtLog.Text & ......).

The time stamp itself works correctly.

Do you know how I can keep the text color of all the lines in the txtLog window?

Thank you for your help.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use RichStringBuilder instead:
B4X:
Sub Globals
   Dim lbl As Label
   Dim rsb As RichStringBuilder
End Sub

Sub Activity_Create(FirstTime As Boolean)
   lbl.Initialize("")
   Activity.AddView(lbl, 0,0, 500dip, 500dip)
   lbl.Gravity = Bit.Or(Gravity.LEFT, Gravity.TOP)
   rsb.Initialize
End Sub
Sub Activity_Click
   Dim rs As RichString
   rs.Initialize("{TxtColor1}Me{TxtColor1}" & CRLF & "{TxtColor2}You{TxtColor2}" & CRLF)
   rs.Color2(Colors.Red, "{TxtColor1}")
   rs.Color2(Colors.Blue, "{TxtColor2}")
   rsb.Append(rs)
   lbl.Text = rsb
End Sub
 
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
OK, I added your code in the ChatActivity module (extra lines in Globals Sub and Activity_Create Sub, and new Activity_Click Sub) and I came back to the original code in the LogMessage Sub, but now I get only black text in the txtLog window. Maybe I misunderstood something :sign0013:
 
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
OK, thank you for the clarification.

I have now the following code and it seems to work fine :)

B4X:
Sub Globals
   Dim txtInput As EditText  
   Dim txtLog As EditText  
   Dim btnSend As Button  
   
   ' 2012-03-13 
   ' http://www.b4x.com/forum/basic4android-updates-questions/15866-bluetooth-chat-text-txtlog-window-colored-richstring.html
   Dim rsb As RichStringBuilder
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("2") 
   If AStream.IsInitialized = False Then
      AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
   End If
   txtLog.Width = 100%x
   
   ' 2012-03-13 
   rsb.Initialize
End Sub

Sub Activity_Click
   
End Sub

Sub LogMessage(From As String, Msg As String)
   'ORIGINAL txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF

   ' Time Stamp 
   DateTime.TimeFormat = "HH:mm:ss.SSS"
   Dim timeNow As String
   timeNow = DateTime.Time(DateTime.Now)

   ' 2012-03-13 
   Dim rs As RichString
   rs.Initialize("{TxtColor}" & "(" & timeNow & ") " & From & ": " & Msg & "{TxtColor}" & CRLF) 
   If From = "Me" Then rs.Color2(Colors.Blue, "{TxtColor}") 
   If From = "You" Then rs.Color2(Colors.Red, "{TxtColor}") 
   rsb.Append(rs)
   txtLog.Text = rsb

   'ORIGINAL
   txtLog.SelectionStart = txtLog.Text.Length
End Sub

I hope this will help other people.
 
Upvote 0

clusker2

Member
Licensed User
Longtime User
Error Connecting

I loaded this on to my HTC Incredible2 and it works well, except with this specific scenario... If I exit the app, then manually turn off my BlueTooth radio, then re-enter the app, I get the following error:

"Error connecting: java.io.IOException: Software caused connection abort"

I am using it with all of the exact code that was given through these threads. I made no personal adjustments to any of it. Any ideas?
 
Upvote 0
Top