Smiley Apps for Android Chat

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

How can I do this with code? Please guide me.

+1


source to share


2 answers


You can achieve this using ImageSpan objects. TextViews and EditTexts use Spanned / Spannable objects to store typed text content, not just java strings. On these Spanned / Spannable objects, you can define spacing for sections of text that change how those sections are displayed. This way you can display text in bold, italic, etc., and also you can display images instead of specific sections.



So you can find the ":-)" pattern in the entered text and tickle the ImageSpan by displaying an emoticon. Check docs http://developer.android.com/reference/android/text/style/ImageSpan.html .

+5


source


We cannot add an image directly to editText and textView. for this we have to make the text spannable and my code creates spannable text and now you can set it in 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);

      

0


source







All Articles