I am trying to add some text and an image in a TextView using Html.fromHtml(), but it's not working.
Getting the string from resources and calling Html.fromHtml():
String insertedText = "<img src=\"small_image\"/>";
String strText = getResources().getString(R.string.big_string, insertedText);
myTextView.setText(Html.fromHtml(strText, context, null));
Where context is an activity that also implements ImageGetter. This is the getDrawable function:
public Drawable getDrawable(String source) {
int id = 0;
Drawable drawable = null;
if(source.equals("small_image")){
id = R.drawable.small_image;
}
if (id > 0) {
drawable = getResources().getDrawable(id);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
return drawable;
}
In the strings.xml file in values, I have:
<string name="big_string">"Big String %1$s"</string>
I have small_image.png in the drawable folder and while debugging, I can see that id gets a correct value and then the bitmap is loaded in the drawable variable, but it still renders the rectangle with 'obj' written in it.
Any idea why it does that?
I also tried using SpannableStringBuilder and add an image span that would have replaced a 'space' from my text, but that didn't work either.