Android Question Emoji text length

leongcc

Member
Licensed User
Longtime User
When I type just 1 emoji character into an edittext, the length of the text is 2 as reported by editext1.text.length. So if I type '123###' (where # represent an emoji I type at the keyboard), the length of the text is 9.
I understand emoji need more space but I would like to know there are only 6 characters in the edttext and not 9.

Any suggestion?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use ToCodePoints to get an array of ints with the code points:
B4X:
Sub Process_Globals
   Private bc As ByteConverter
End Sub

Sub Globals
   Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   EditText1.Text = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602)
   Dim cp() As Int = ToCodePoints(EditText1.Text)
   Log($"Text length: ${cp.Length}"$)
   For Each codepoint As Int In cp
     Log(codepoint)
   Next
End Sub

Sub ToCodePoints(str As String) As Int()
   Dim b() As Byte = str.GetBytes("UTF-32BE")
   Return bc.IntsFromBytes(b)
End Sub


Sub UTS (codepoint As Int) As String
  Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
  Return BytesToString(b, 0, 4, "UTF-32BE")
End Sub
 
Upvote 0
Top