bh.amir
bh.amir

Reputation: 109

remove html tags from a string but restore \n

In my news application, I get the data from JSON which i need to display in the TextView. But the data content that comes are mixed up with html tags. I tried removing the html tags from the string but got some problems.

This is the String i have:

String testString = "<a href=\"#"><img src=\"xyz.jpg" width=\"196\" height=\"300\" /></a>Residents of Chisapani bazaar in the south of Khotang district have been living in darkness for the past three years.\r\n\r\nThe power house of Dobhane river Hydropower Project was damaged in a landslide three years ago.\r\n\r\n Source:ABCD";

I need to show this String in the TextView excluding the html tags. Thus is what I have done.

textbox = (TextView) findViewById(R.id.textview1);
textbox.setText(Html.fromHtml(testString).toString());

On doing so, even the \n (newline) part was omitted which turned the whole text into a single block removing the paragraphs. But this is not preffered. I need the text which is a news content to be well-paragraphed.

Can there be any way which can remove just the tags like <a> or <img> but restore \n.

Upvotes: 2

Views: 1202

Answers (1)

Mushahid Khatri
Mushahid Khatri

Reputation: 728

Try this

           tv.setText(Html.fromHtml(testString.replace("\n", "<br/>")));

This will create new line at each "\n". But It can cause problem when your data itself contains \n any how...

Upvotes: 6

Related Questions