简体   繁体   中英

How to add emoji support in app for posting comments in Android?

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 .

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM