Display emoji/emotion icon in Android TextView | Newbedev
文章推薦指數: 80 %
Why do you want to embed the protected Apple emoji images in your application at all? The Unicode standard includes 722 emoji that can be displayed by ...
MenuNEWBEDEVPythonJavascriptLinuxCheatsheetContactNEWBEDEVDisplayemoji/emotioniconinAndroidTextViewWhydoyouwanttoembedtheprotectedAppleemojiimagesinyourapplicationatall?
TheUnicodestandardincludes722emojithatcanbedisplayedbyAndroid'sdefaultfontjustbyenteringtheUnicodecharsintoanEditTextfieldorTextView.
Youcan,inaddition,usethefollowinglibrary(infolder"Java")toautomaticallyconvertpopularemoticonslike:-)tothecorrespondingUnicodeemoji:
https://github.com/delight-im/Emoji
Here,Pleasegothroughbelowsolution:
Problem:InsideTextViewinsteadofEmoji,String\ue415\ue056\ue057isshowing.
Rootcause:Injavaorandroid,programmaticallystringrepresentationofEmoji'syouwillgetas\\ue415\\ue056\\ue057.ButwhenyoutrytoprintsameStringinconsoleorLogCatthenescapecharacterisremovedandyouwillgetstringas\ue415\ue056\ue057becauseofwhichrootcauseofthisissueisnotdetectable.
Solution:Tosolvethisissue,weneedtohandleescapecharacter.Ihavecreatedbelowmethodwhichsolvethisproblem.
publicstaticStringgetEmojiFromString(StringemojiString){
if(!emojiString.contains("\\u")){
returnemojiString;
}
StringemojiEncodedString="";
intposition=emojiString.indexOf("\\u");
while(position!=-1){
if(position!=0){
emojiEncodedString+=emojiString.substring(0,position);
}
Stringtoken=emojiString.substring(position+2,position+6);
emojiString=emojiString.substring(position+6);
emojiEncodedString+=(char)Integer.parseInt(token,16);
position=emojiString.indexOf("\\u");
}
emojiEncodedString+=emojiString;
returnemojiEncodedString;
}
Itworksfineifyouconvertthestringtoachararrayandcheckeachchar,suchas:
StringBuildersb=newStringBuilder();
for(charcurr:str.toCharArray()){
sb.append((SUPPORTED_EMOJI_SET.contains(curr))?convertCharToImgTag(curr):curr);
}
whereSUPPORTED_EMOJI_SETisjustasetofchars,forexample:
newHashSet
延伸文章資訊
- 1How to set emoji by unicode in a textview? - Stack Overflow
How to set emoji by unicode in a textview? android unicode textview encode emoji. Hi I'd like to ...
- 2Display emoji/emotion icon in Android TextView | Newbedev
Why do you want to embed the protected Apple emoji images in your application at all? The Unicode...
- 3关于android:如何在TextView中通过Unicode设置表情符号?
How to set emoji by unicode in a textview?嗨,我想做以下事情:[cc]??? unicode = U+1F60AString emoji = getEm...
- 4FAQ - Emoji & Pictographs - Unicode
- 5android - 如何在TextView 中通过unicode 设置表情符号?
嗨,我想执行以下操作: ??? unicode = U+1F60A String emoji = getEmojiByUnicode(unicode) String text = "So hap...