1

I am developing a chat application. For smileys I have used image buttons. But the problem is that I don't know how to insert the smileys to the text control after the chat messages, similar to this: enter image description here

How to do it through code? Please guide me.

Simon
  • 1,814
  • 20
  • 37
Tejaswini
  • 357
  • 4
  • 15

2 Answers2

5

You can achieve this using ImageSpan objects. The TextViews and EditTexts use Spanned/Spannable objects to store the entered text content, not just mere java Strings. On these Spanned/Spannable objects you can define spans for sections on the text that modifies the way that those sections are shown. So you can display a text in bold, italics, etc. and you can also display images in place of certain sections.

This way you can search for a ":-)" pattern in the entered text, and slap an ImageSpan on it displaying a smiley. Check the docs http://developer.android.com/reference/android/text/style/ImageSpan.html.

Sunny
  • 14,522
  • 15
  • 84
  • 129
0

We cannot add image directly to editText and textView. for that we have to make a spannable text, and my code creates a spanned text and now you can set it to textView/editText.

  ImageGetter imageGetter = new ImageGetter() 
                {
                    public Drawable getDrawable(String source) {
                        Drawable d = getResources().getDrawable(R.drawable.e001);
                        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                        return d;
                    }
                };

                Spanned cs = Html.fromHtml("<img src='" + getResources().getDrawable(R.drawable.e001) + "'/>", imageGetter, null);


            yourTextView.setText(cs);
Xar-e-ahmer Khan
  • 1,314
  • 15
  • 23
  • we cannot directly add image to textView or editText but we can do it with the help of ImageSpan, I have converted my image to spanned Text and now you can set it to textView or editText, and also i am creating a emoji keyboard it is working properly – Xar-e-ahmer Khan Dec 22 '14 at 14:43
  • 1
    Code-only answers are not very useful. – Phantômaxx Dec 22 '14 at 14:59