Last active
February 6, 2016 03:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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