I have a TextView in which I want to insert a smiley. I am using the following code to insert the smiley into the Spannable
private static final Map<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put(":)", R.drawable.smileyon);
emoticons.put(":-)", R.drawable.smileyon);
}
public Spannable getSmiledText(String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
if (emoticons.size() > 0) {
int index;
for (index = 0; index < builder.length(); index++) {
if (Character.toString(builder.charAt(index)).equals(":")) {
for (Map.Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(getContext(), entry.getValue()), index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.v("CheckChat", "Found new smily ");
index += length - 1;
break;
}
}
}
}
}
Log.v("CheckChat", "Returning text " + builder);
return builder;
}
chatLine.setText(getSmiledText(chatmessage.getMessage()) + " "
+ DateUtil.timestampToTime(Integer.parseInt(chatmessage.getMessageTime())));
When I add some text, like "hello :-)" the log states that it found the smiley and that the text is correctly returned.....But.....there's no smiley inserted ? The drawable is there, its a png file.
EDIT OK so I see the text exactly as I entered it "hello :-)" so it seems the mapping is not performed at all. So the :-) was not replaced with an image.
Looked at several tutorials but could not find a solution, as it appears I am doing everything according to the book.
Thanks for your help.