3

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.

Coen Damen
  • 2,009
  • 5
  • 29
  • 51
  • 1
    May be that image drawable is not set to a valid bounds!!! – Gopal Gopi Apr 09 '14 at 07:49
  • hi Gopal can you elaborate? (I am new to this stuff) – Coen Damen Apr 09 '14 at 07:50
  • 2
    see [this](http://stackoverflow.com/questions/3341702/displaying-emoticons-in-android/4302199#4302199) and [this](http://stackoverflow.com/questions/7189903/how-to-add-smiley-emojis-in-edittext/7200218#7200218)... – Gopal Gopi Apr 09 '14 at 07:53
  • 2
    OK great, it works ! I used Drawable myIcon = context.getResources().getDrawable(entry.getValue()); myIcon.setBounds(0, 0, 60, 60); spannable.setSpan(new ImageSpan(myIcon, ImageSpan.ALIGN_BOTTOM), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); – Coen Damen Apr 09 '14 at 08:32
  • @CoenDamen You should put the answer as correct so more people can see it – FabioR May 16 '17 at 18:59

1 Answers1

1

But in my case I was using fragments so I used

text.setSpan(new ImageSpan(container.getContext(),R.drawable.image),0,0,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

rather than directly passing the Drawable

text.setSpan(new ImageSpan(getResources().getDrawable(R.drawable.image)),0,1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Just to add what solved the OP problem and as noted by Fabio the answer was

Drawable myIcon = context.getResources().getDrawable(entry.getValue());
myIcon.setBounds(0, 0, 60, 60);
spannable.setSpan(new ImageSpan(myIcon, ImageSpan.ALIGN_BOTTOM), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);