Remove Spanable image from EditText
I am wo开发者_如何学Gorking on an messenger type application where I need to add Smilies in Edit Text during chat.For adding the Smilies I need to add real smiley image not smiley string.To Achieve this I am trying to add Spanable image dynamically on a EditText This is my code
SpannableString ss = new SpannableString("abc");
Drawable d = img.getDrawable();
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
et.setText(ss);
I am following above code from link as How can i adding image on edittext dynamically?,
I am adding image in Edit Text and set the focus at end of Edit text where user's text end.Smilies adding fine as I want. Now when I want to removing smilies / adding more text to Edit Text its given unexpected output. Sometimes it remove text instead of smilies.Sometimes when I try to add more text/smiley it also again show old smilies that I already deleted on adding more text/smiley. Waiting for comments . . .
Try this.
String strMessage = edtMsg.getText().toString();
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(
R.drawable.happy);
d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
return d;
}
};
Spanned cs = Html.fromHtml(strMessage+
"<img src='"
+ getResources()
.getDrawable(R.drawable.happy)
+ "'/>", imageGetter, null);
System.out.println("cs is:- " + cs);
edtMsg.setText(cs);
精彩评论