0

I have an app where the user can post a comment for a product/item and it will upload to my server. The app then loads a listview of the comments that have been posted from a MySql database. Users can include emoji's in their comment post by default because of their devices' keyboard but when the app refreshes the comment list, the emoji's show up as ????. How can I add support for emoji's?

UPDATE

I am using a standard TextView to show the comments. Posting the comments is a standard EditTextView. Per the answer below, I included the text from the edittext view in my logcat and any emoji I choose shows as ��.

Steve C.
  • 1,333
  • 3
  • 19
  • 50
  • You need to use utf8_mb4 in MySQL. – SLaks Jun 29 '16 at 03:45
  • @SLaks utf8_mb4 isn't available. The closest thing available is utf8mb4_"various languages here" or utf8mb4_bin or utf8mb4_unicode_ci. Could I use any of those? – Steve C. Jun 29 '16 at 06:16

2 Answers2

1

Ok. I figured out how to get it to work. Per user6526330's link. I first had to add the library commons-lang-2.5.jar and use the following for the EditText and TextView.

//This converted the emoji code to utf8md4 for the server
String cmttxt = newCmnt.getText().toString().trim();
String toServerUnicodeEncoded = StringEscapeUtils.escapeJava(cmttxt);

//This converted the server response to properly show the emoji
String serverResponse = "Some string from server with emoji code embedded";
String fromServerUnicodeDecoded = StringEscapeUtils.unescapeJava(serverResponse);

That wasn't enough to get things to work though because the standard EditText and TextView doesn't know how to translate the emoji code. So I found the library Emojicon here.

Using the TextView and EditText from that library allowed me to post and receive the text with embedded emoji properly.

I also had to change my database charset to utf8md4_unicode_ci for my database to see it properly.

Because I was using PDO to post the comment, I had to change this line:

$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);

to

$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8mb4", $username, $password, $options);

So for anyone else that may see this and want to have emoji support in their app, here is the checklist you would need to follow:

  1. Make sure your database collation/charset is utf8mb4 by following this link.
  2. Add the commons-lang-2.5 library to your project from here.
  3. Add the Emojicon library to your project from here.
  4. Add the TextView and EditText view from the library to their respective layouts.
  5. Follow the code posted above to convert your emoji text into utf8mb4 encoding.
  6. Enjoy!

Hope this helps someone else. Thank you everyone for your help.

Steve C.
  • 1,333
  • 3
  • 19
  • 50
0

I don't have enough reputation so I have to make an answer instead of a comment.

Could you share some additional info on how you are trying to display the emoji? Is it being shown in a TextView? Also, what is the value of the string you are trying to display, you can find this by logging the value of the string before you set it in the code and then viewing it in logcat.

While you are at it take a look at this post, perhaps it can help you out? If so, let us know!

Community
  • 1
  • 1
Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16
  • 1
    This may help as well: http://stackoverflow.com/questions/9740565/display-emoji-emotion-icon-in-android-textview – Dr. Nitpick Jun 29 '16 at 03:42
  • After checking in my logcat, any emoji I choose shows as "��" – Steve C. Jun 29 '16 at 06:46
  • reputation is not a good reason for you to post like this. Gain more by helping others. Not like doing this – Rashid Jun 29 '16 at 08:24
  • @SteveC. thank you for sharing the additional info on what you were doing. I'm glad that you managed to find the solution and that the link was helpful (though not sufficient on its own). Would you mind marking your solution as the answer for anyone else who comes around? – Dr. Nitpick Jun 29 '16 at 13:17