0

I am trying to set an image stored in assets folder in strings.xml and set that string to a TextView. Is this possible?

Strings.xml has the following string:

<string name="nofriends">No Friends.. <![CDATA[<br><br><br><br> <img src=\"file:///android_asset/sad.png\"/>]]></string>

And in my java file I am setting something like this"

Spanned text = Html.fromHtml(res.getString(R.string.nofriends));
noFriends.setText(text);

I am getting the text with a small green box but not the actual image?

Can anybody help me fix this?

TheDevMan
  • 5,914
  • 12
  • 74
  • 144

2 Answers2

3

You can use ImageGetter to get image from drawable resource.

ImageGetter imageGetter = new ImageGetter() {

    @Override
    public Drawable getDrawable(String source) {
        Drawable d = getResources().getDrawable(R.drawable.yourimage);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }
};

Spanned spanned = Html.fromHtml(
    "<img src='" + getResources().getDrawable(R.drawable.yourimage) + "'/>", imageGetter, null);

now set it to TextView

textView.setText(spanned);

Edit

<string name="nofriends">No Friends.. ####</string> //#### to replace with image

String s=res.getString(R.string.nofriends);
s=s.replace("####",spanned);
textView.setText(s);
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
-1

TextView's are not designed to be used like this. What you should be doing instead is setting the drawable attribute on a TextView.

See a great answer to a similar problem here.

Community
  • 1
  • 1
Andrew Breen
  • 685
  • 3
  • 11