Sep 9, 2013

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

Previous example load single image in HTML String with Html.ImageGetter. This example load two different images in HTML String. Because both images in drawable, so we can use a common Html.ImageGetter.

Display HTML String with multi images on TextView, with Html.ImageGetter
Display HTML String with multi images on TextView, with 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;
import android.widget.Toast;

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>" +
   "<br/>" +
   "<img src='page'><b><a href='https://plus.google.com/b/113141750089533146251/'>My G+ Page</a></b><br/>";

 @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) {
    
    Toast.makeText(getApplicationContext(), 
      source, 
      Toast.LENGTH_LONG).show();
    
    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());
  
 }

}


Next: Html.ImageGetter load image from internet

2 comments:

  1. this code show the ..package not open ...inavalide value like that..could you tell me any solution.

    ReplyDelete
  2. i want to disply images from url

    ReplyDelete

Infolinks In Text Ads