Search code examples
androidtextviewemojiandroid-keypad

Handling smileys in android


Recently completed an application and have been informed that when users add smileys to their comments, they show up the first time and the disappear.

I have displayed the comments using comment.setText(Html.fromHtml(text));

I had never really considered smileys and this is my first time of trying to handle them with an app. What is the default behaviour with with displaying smileys in a textview. I have checked on my default keypad and there are no smileys so users might be using a custom keypad. What is the best way to stop handle input from custom keypads or handle smileys generally within an app.


Solution

  • Android Span allow you to format code as you want. You can use an ImageSpan to render the input text ":-)" by any bitmap in a TextView :

    String text = "here is a smiley :-)";
    String smileyStr = ":-)";
    SpannableString ss = new SpannableString("Here's a smiley :-)");
    int smileyStart = text.indexOf(smileyStr);
    Bitmap smiley = BitmapFactory.decodeResource(getResources(), R.drawable.emoticon);
    ss.setSpan(new ImageSpan(smiley), text.indexOf(smileyStr), smileyStr.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    textView.setText(ss);
    

    You can find out more here : http://blog.stylingandroid.com/introduction-to-spans/