android - How to mapping emoticon with string in text view -


how show emoticons in text view become string. kind of r.drawable.emoticon_smile :d. in case when can show picture of emoticons, image has value of "obj", should :d

i'm using 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;         (entry<pattern, integer> entry : emoticons.entryset()) {             matcher matcher = entry.getkey().matcher(spannable);             while (matcher.find()) {                 boolean set = true;                 (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 have add map entries emote icons want handle in static block. using class pretty easy after - call of settext (or call addsmiles directly) methods. handle html parsing too.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -