Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.Spannable; import android.text.SpannableString; import android.text.TextUtils; import android.text.style.ImageSpan; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static Pattern pattern; private static String[] emojiCodes = new String[] { ":smile:", ":laughing:", ":blush:", ":smiley:", ":relaxed:", ":smirk:", ":heart_eyes:", ":kissing_heart:", ":kissing_closed_eyes:", ":flushed:", ":relieved:", ":satisfied:", ":grin:", ":wink:", ":stuck_out_tongue_winking_eye:", ":stuck_out_tongue_closed_eyes:", ":grinning:", ":kissing:", ":kissing_smiling_eyes:", ":stuck_out_tongue:", ":sleeping:", ":worried:", ":frowning:", ":anguished:", ":open_mouth:", ":grimacing:", ":confused:", ":hushed:", ":expressionless:", ":unamused:", ":sweat_smile:", ":sweat:", ":disappointed_relieved:", ":weary:", ":pensive:", ":disappointed:", ":confounded:", ":fearful:", ":cold_sweat:", ":persevere:", ":cry:", ":sob:", ":joy:", ":astonished:", ":scream:", ":tired_face:", ":angry:", ":rage:", ":triumph:", ":sleepy:", ":yum:", ":mask:", ":sunglasses:", ":dizzy_face:", ":neutral_face:", ":no_mouth:", ":innocent:", ":thumbsup:", ":thumbsdown:", ":clap:", ":point_right:", ":point_left:" }; public static CharSequence replace(Context context, String text) { if (TextUtils.isEmpty(text)) { return text; } SpannableString spannableString = new SpannableString(text); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String factText = matcher.group(); String key = factText.substring(1, factText.length() - 1); if (contain(emojiCodes, factText)) { Bitmap bitmap = getEmojiDrawable(context, key); ImageSpan image = new ImageSpan(context, bitmap); int start = matcher.start(); int end = matcher.end(); spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return spannableString; } public static boolean contain(String[] strings, String string) { for (String s : strings) { if (s.equals(string)) { return true; } } return false; } public static boolean contain(String text) { boolean isContain = false; if (TextUtils.isEmpty(text)) { return false; } Matcher matcher = pattern.matcher(text); while (matcher.find()) { String factText = matcher.group(); if (contain(emojiCodes, factText)) { isContain = true; break; } } return isContain; } public static Bitmap getEmojiDrawable(Context context, String name) { return getDrawableByName(context, "emoji_" + name); } public static Bitmap getDrawableByName(Context ctx, String name) { BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), ctx.getResources().getIdentifier(name, "drawable", ctx.getPackageName()), options); return bitmap; } }