1

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.

Scorpio
  • 1,124
  • 1
  • 11
  • 28
  • You can't show a image in a `TextView`. – Spring Breaker Mar 13 '14 at 13:33
  • According to [this](http://stackoverflow.com/questions/8184566/show-image-in-textview) and [this](http://stackoverflow.com/questions/16396462/android-textview-settext-in-html-fromhtml-to-display-image-and-text) , you can. I have used this in other applications and it worked perfectly! – Scorpio Mar 13 '14 at 13:38
  • The mere existence of `ImageGetter` suggests that it is possible indeed. – njzk2 Mar 13 '14 at 13:44

5 Answers5

4

I have found the problem with my code. Everything I wrote in the question is correct and you can use it as a reference for incorporating images in TextViews.

The problem was that I was using the attribute :

android:textAllCaps="true"

In the xml file and that somehow prevented the image from being shown.

Scorpio
  • 1,124
  • 1
  • 11
  • 28
1
  String name = modelDispatchReportList.get(position).getName();   //get name from model class typed List
    String text = "<font color='#000000'>" + name + "</font>";  //change color of name

    /*  check version of API to call method of Html class because fromHtml method is deprecated
    * */
    if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.N) {
        Log.d(TAG, "onBindViewHolder: if");
        holder.textViewName.setText("12345" + " ");    //set text to textView
        holder.textViewName.append(Html.fromHtml(text));    //append text to textView
    } else {
        Log.d(TAG, "onBindViewHolder: else");
        holder.textViewName.setText("12345" + " ");    //set text to textView
        holder.textViewName.append(Html.fromHtml(text, 0)); ////append text to textView
    }
Khyati Vara
  • 1,042
  • 13
  • 22
0

well, I tried your code, it' s working...

btw, I used the deafult ic_lancher drawable...

i just simply set the the textview to setContentView.

i guess is the textview' s layout or drawable' s problem?

could you tell me more?

longkai
  • 3,598
  • 3
  • 22
  • 24
  • I don't really know what else to say, of course I have a bigger layout file, but I cannot share it...it worked for me in other cases, but I don't see what could influence it. If you can think of anything specific to ask, I will be happy to respond. – Scorpio Mar 13 '14 at 13:53
  • well, make sure the ``getDrawable()`` is called and the drawable is non-null, I cann' t see why... – longkai Mar 13 '14 at 14:08
  • as I said in the question, I did make sure. It enters the function and the drawable is not null. I saw that in debug – Scorpio Mar 13 '14 at 14:45
  • Does it make a difference if the view is inflated and added to a listview? I am running out of ideas and thinking of anything that may work... – Scorpio Mar 14 '14 at 14:30
  • 1
    maybe, I' m not clear but I suggest you removing some textview layout attributes to test. – longkai Mar 15 '14 at 09:19
0

Just change your Textviewinto WebView and set the content to your WebView

Ameer
  • 2,709
  • 1
  • 28
  • 44
-1

You cant set image on textview by html.

user8938
  • 559
  • 1
  • 4
  • 12