Java tutorial
//package com.java2s; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ImageSpan; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static List<String> emotionTexts; private static Pattern pattern; public static CharSequence replace(Context ctx, String text) { if (text == null) { return null; } SpannableString spannableString = new SpannableString(text); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String factText = matcher.group(); String key = factText.substring(1); if (emotionTexts.contains(factText)) { Bitmap bitmap = getDrawableByName(ctx, key); ImageSpan image = new ImageSpan(ctx, bitmap); int start = matcher.start(); int end = matcher.end(); spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return spannableString; } 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; } }