user3519582
user3519582

Reputation: 1

How to mapping emoticon with string in text view

How to show emoticons in text view become string. This kind of R.drawable.emoticon_smile be :D. In this case when I can show you a picture of emoticons, the image has a value of "obj", should :D

Upvotes: 0

Views: 2228

Answers (2)

Samuil Yanovski
Samuil Yanovski

Reputation: 2867

I'm using something like this:

public class ViewsUtils {

    private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();


    static {
        addPattern(emoticons, "\ud83d\udeb6", R.drawable.emot_d83ddeb6);
        ...
    }

    private static void addPattern(Map<Pattern, Integer> map, String smile,
            int resource) {
        map.put(Pattern.compile(Pattern.quote(smile)), resource);
    }

    public static boolean addSmiles(Context context, Spannable spannable) {
        boolean hasChanges = false;
        for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
            Matcher matcher = entry.getKey().matcher(spannable);
            while (matcher.find()) {
                boolean set = true;
                for (ImageSpan span : spannable.getSpans(matcher.start(),
                        matcher.end(), ImageSpan.class))
                    if (spannable.getSpanStart(span) >= matcher.start()
                            && spannable.getSpanEnd(span) <= matcher.end())
                        spannable.removeSpan(span);
                    else {
                        set = false;
                        break;
                    }
                if (set) {
                    hasChanges = true;
                    spannable.setSpan(new ImageSpan(context, entry.getValue()),
                            matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return hasChanges;
    }

    public static void setText(TextView view, String text) {
        if (null != view && null != text) {
            Spanned spanned = Html.fromHtml(text);
            SpannableString spannableString = SpannableString.valueOf(spanned);
            addSmiles(view.getContext(), spannableString);
            view.setText(spannableString);
        }
    }

    public static void setText(View parent, int viewId, int resId) {
        if (null != parent) {
            String text = parent.getContext().getString(resId);
            setText(parent, viewId, text);
        }
    }

    public static void setText(View parent, int viewId, String text) {
        if (null != parent) {
            TextView view = (TextView) parent.findViewById(viewId);
            if (null != view && null != text) {
                Spanned spanned = Html.fromHtml(text);
                SpannableString spannableString = SpannableString.valueOf(spanned);
                addSmiles(view.getContext(), spannableString);
                view.setText(spannableString);
            }
        }
    }

    public static void setText(View parent, int viewId, String text,
            int visibility) {
        if (null != parent) {
            TextView view = (TextView) parent.findViewById(viewId);
            if (null != view && null != text) {
                Spanned spanned = Html.fromHtml(text);
                SpannableString spannableString = SpannableString.valueOf(spanned);
                addSmiles(view.getContext(), spannableString);
                view.setText(spannableString);
                view.setVisibility(visibility);
            }
        }
    }

}

You just have to add map entries for the emote icons you want to handle in the static block. Using this class is pretty easy after that - just call some of the setText (or call addSmiles directly) methods. It would handle Html parsing too.

Upvotes: 2

MilapTank
MilapTank

Reputation: 10076

Do you like emojis in Whatsapp See this lib may helps you

https://github.com/rockerhieu/emojicon

Upvotes: 0

Related Questions