Apr 29, 2013

Display HTML String with img on TextView, with Html.ImageGetter

Example to display HTML String with img on TextView. To display image in TextView, we have to implement interface of Html.ImageGetter.

package com.example.androidhtmltextview;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='http://android-coding.blogspot.com'>Android Coding</a></b>";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  TextView htmlTextView = new TextView(this);
  setContentView(htmlTextView);
  
  htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter(){

   @Override
   public Drawable getDrawable(String source) {
    Drawable drawable;
    int dourceId = 
      getApplicationContext()
      .getResources()
      .getIdentifier(source, "drawable", getPackageName());
    
    drawable = 
      getApplicationContext()
      .getResources()
      .getDrawable(dourceId);
    
    drawable.setBounds(
      0, 
      0, 
      drawable.getIntrinsicWidth(),
      drawable.getIntrinsicHeight());
    
    return drawable;
   }
   
  }, null));
  
  htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());
  
 }

}


Display HTML String with img on TextView
Display HTML String with img on TextView


Next: Display HTML String with multi images on TextView, with Html.ImageGetter

4 comments:

  1. I need help from sir before that thank you very much for sharing your code with us.

    How to show multiple images in text view.
    These would help me much

    Thank you

    ReplyDelete
  2. Hi, by this method can I show local assets folder image...?

    ReplyDelete

Infolinks In Text Ads