2012-10-11

Add image into TextView on Android


You can use ImageSpan to add image into TextView
Here is the example, ImageSpan will replace "abc" as a image icon:
TextView text  = (TextView) findViewById(R.id.text);
SpannableString ss = new SpannableString("abcdef");
Drawable img = getResources().getDrawable(R.drawable.icon);
img.setBounds(0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight());
ss.setSpan(new ImageSpan(img, ImageSpan.ALIGN_BASELINE), 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
text.setText(ss); 

No comments: