Android Question is there a good solution for Display EMOJI in

Ju Yang

Active Member
Licensed User
ASP.NET(Web),B4A,B4i and B4J?


We are making a CRM with chat module using B4X, and have ASP.NET(Web),B4A,B4i and B4J platform,

How to make a good solution for Display EMOJI in all those platforms?

help! thank all guys very much! ~_~
 

Erel

Administrator
Staff member
Licensed User
This forum is not for ASP.Net questions.

Emojis are Unicode characters. You can use UTF32 to create such characters:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   EditText1.Text = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602)
End Sub


Sub UTS (codepoint As Int) As String
  Dim bc As ByteConverter
  Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
  Return BytesToString(b, 0, 4, "UTF32")
End Sub
You need to add a reference to ByteConverter library in B4A and B4J, and iRandomAccessFile in B4i.

It will work in B4A, B4J and B4i:





B4J

The desktop font doesn't support emojis. So you need to include a custom font, such as: https://github.com/MorbZ/OpenSansEmoji
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.SetFormStyle("UNIFIED")
   MainForm.Show
   MainForm.RootPane.LoadLayout("1")
   fx.LoadFont(File.DirAssets, "OpenSansEmoji.ttf", 32)
   Label1.Style = Label1.Style & "-fx-font-family: OpenSansEmoji;"
   Label1.Text = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602)
   TextField1.Style = TextField1.Style & "-fx-font-family: OpenSansEmoji;"
   TextField1.Text = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602)
End Sub

Sub UTS (codepoint As Int) As String
  Dim bc As ByteConverter
  Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
  Return BytesToString(b, 0, 4, "UTF32")
End Sub
 

Ju Yang

Active Member
Licensed User
But we need all platforms' EMOJI image is same, like iOS EMOJI. how to implement?
 

Kwame Twum

Active Member
Licensed User
I tried naming my emojis like below.
emoj.png

then I tried parsing the filename to an Int (without the extension) using the sub below:
B4X:
Sub emo_Click(Thumbnail As Panel)
    Dim coda As String = Thumbnail.Tag
    coda = c.GetFileName(coda).Replace(".png","")
    coda = "0x" & coda : Log(coda)            'This prints 0x1F601 to the logs for the first emoji
    Dim code As Int = Bit.ParseInt(coda,16)
    txt.Text = txt.Text & UTS(code)            'I get java.lang.NumberFormatException: Invalid int: "0x1F601" on this line
End Sub
What could I be doing wrong?
 
Last edited:

LucaMs

Expert
Licensed User
Hi, Erel,

the code you posted in #2 (function UTS) works as expected on my tablet (Android 4.4.2) but does not work on my smartphone (Android 4.0.4) - it shows a "space": is it due to the different Android versions?
 

LucaMs

Expert
Licensed User
I tried naming my emojis like below.
View attachment 42177
then I tried parsing the filename to an Int (without the extension) using the sub below:
B4X:
Sub emo_Click(Thumbnail As Panel)
    Dim coda As String = Thumbnail.Tag
    coda = c.GetFileName(coda).Replace(".png","")
    coda = "0x" & coda : Log(coda)            'This prints 0x1F601 to the logs for the first emoji
    Dim code As Int = Bit.ParseInt(coda,16)
    txt.Text = txt.Text & UTS(code)            'I get java.lang.NumberFormatException: Invalid int: "0x1F601" on this line
End Sub
What could I be doing wrong?
Can I ask how do you replace the default characters with those images?
 

Leni Berry

Active Member
Licensed User
How to convert this output
This forum is not for ASP.Net questions.

Emojis are Unicode characters. You can use UTF32 to create such characters:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   EditText1.Text = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602)
End Sub


Sub UTS (codepoint As Int) As String
  Dim bc As ByteConverter
  Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
  Return BytesToString(b, 0, 4, "UTF32")
End Sub
You need to add a reference to ByteConverter library in B4A and B4J, and iRandomAccessFile in B4i.

It will work in B4A, B4J and B4i:





B4J

The desktop font doesn't support emojis. So you need to include a custom font, such as: https://github.com/MorbZ/OpenSansEmoji
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.SetFormStyle("UNIFIED")
   MainForm.Show
   MainForm.RootPane.LoadLayout("1")
   fx.LoadFont(File.DirAssets, "OpenSansEmoji.ttf", 32)
   Label1.Style = Label1.Style & "-fx-font-family: OpenSansEmoji;"
   Label1.Text = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602)
   TextField1.Style = TextField1.Style & "-fx-font-family: OpenSansEmoji;"
   TextField1.Text = UTS(0x1F600) &  UTS(0x1F601) &  UTS(0x1F602)
End Sub

Sub UTS (codepoint As Int) As String
  Dim bc As ByteConverter
  Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
  Return BytesToString(b, 0, 4, "UTF32")
End Sub
Yes, i can display emoji icon from this UTS function into textboxt. but how to translate this emoji into string that can be save to database?
 

Leni Berry

Active Member
Licensed User
How to convert this output


Yes, i can display emoji icon from this UTS function into textboxt. but how to translate this emoji into string that can be save to database?
my mistake... database show me '????' because i set this charset for the connection database.

mysqli_set_charset($connection,"utf8");

just remove this charset. everything back to normal...
 
Top