Skip to content

Instantly share code, notes, and snippets.

@pwittchen
Last active February 6, 2016 03:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwittchen/5200843 to your computer and use it in GitHub Desktop.
Save pwittchen/5200843 to your computer and use it in GitHub Desktop.
Exemplary class which shows, how to display image gathered from the internet inside the TextView in Android application having only html code.
public class FromHtmlImageActivity extends Activity {
private TextView sampleTextView;
private Spanned spannedValue;
private String stringWithHtml;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stringWithHtml = "Sample string with an <a href=\"http://www.exemplary-link.com\">exemplary link</a>.";
spannedValue = Html.fromHtml(stringWithHtml,getImageHTML(),null);
sampleTextView = (TextView)findViewById(R.id.sample_textview);
sampleTextView.setText(spannedValue);
}
public ImageGetter getImageHTML() {
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
try {
Drawable drawable = Drawable.createFromStream(new URL(source).openStream(), "src name");
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
} catch(IOException exception) {
Log.v("IOException",exception.getMessage());
return null;
}
}
};
return imageGetter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment