I\'m showing a bit of HTML in a TextView using Html.fromHtml, which works great.
Sometimes this html will have img-tags, and I\'d also like to display these.
I solved it using this:
ArrayList<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(content);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lines.add(line);
}
scanner.close();
for(int i = 0; i < lines.size(); i++) {
Document doc = Jsoup.parse(lines.get(i));
Elements imgs = doc.select("img");
if(imgs.size() == 0) {
TextView textView = new TextView(this);
textView.setText(Html.fromHtml(lines.get(i)));
maincontainer.addView(textView);
} else {
for(Element el : imgs) {
Element img = el.select("img").first();
String image = img.absUrl("src");
ImageView imageView = new ImageView(this);
imageView.setPadding(0, 10, 0, 10);
imageView.setAdjustViewBounds(true);
Picasso.with(getApplicationContext()).load(image).into(imageView);
maincontainer.addView(imageView);
}
}
}