Reputation: 357
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:
How to do it through code? Please guide me.
Upvotes: 1
Views: 2749
Reputation: 1334
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);
Upvotes: 0
Reputation: 14808
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.
Upvotes: 5