remove html tag in android
Asked Answered
W

9

5

I have follows below XML feed:

<Description>
  <p>Touch, tap, flip, slide! You don&#39;t just read Books, you experience it.</p>
</Description>

Here I have to display the description like

Touch,tap,flip,slide! You don 39.just read the Books, you experience it.

Here I have handled the parser like:

   public static String removeHTML(String htmlString)
  {
  // Remove HTML tag from java String    
String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");

// Remove Carriage return from java String
noHTMLString = noHTMLString.replaceAll("\r", "<br/>");
noHTMLString = noHTMLString.replaceAll("<([bip])>.*?</\1>", "");
// Remove New line from java string and replace html break
noHTMLString = noHTMLString.replaceAll("\n", " ");
noHTMLString = noHTMLString.replaceAll("\"", "&quot;");
noHTMLString = noHTMLString.replaceAll("<(.*?)\\>"," ");//Removes all items in brackets
noHTMLString = noHTMLString.replaceAll("<(.*?)\\\n"," ");//Must be undeneath
noHTMLString = noHTMLString.replaceFirst("(.*?)\\>", " ");
noHTMLString = noHTMLString.replaceAll("&nbsp;"," ");
noHTMLString = noHTMLString.replaceAll("&amp;"," ");
return noHTMLString;

    }

In endElement :

   public void endElement(String uri, String localName, String qName)throws SAXException {
  currentElement = false;
   if (localName.equalsIgnoreCase("Description")){
   sitesList.setDescription(currentValue);
   String Sub_arry=n+currentValue;
   Appscontent.Sub_arraylistdes.add(Sub_arry);
   String stringWithoutHTML=removeHTML(currentValue);
   System.out.println("description value----->"+n+att_ID+"------>>"+stringWithoutHTML);}

Now i have to run the app means the html tag is displayed with my description...Here how can I remove the HTML tag? please provide me solution for these ???

i wish to display the description without Html tags...please provide e solution for these.

EDIT:

    if (localName.equalsIgnoreCase("Description")){
    sitesList.setDescription(currentValue);
    String Sub_arry=n+currentValue;
    StringBuffer sb = new StringBuffer();
    sb.append(Sub_arry);
     String newString = sb.toString();
      Appscontent.Sub_arraylistdes.add(newString);
       System.out.println("description value----->"+n+att_ID+"------>>"+newString);}

EDIT:

  public static String html2text(String html) {
  return Jsoup.parse(html).text();
    }

In endElement:

    if (localName.equalsIgnoreCase("Description")){
    sitesList.setDescription(currentValue);
    String Sub_arry=n+currentValue;
    Appscontent.Sub_arraylistdes.add(Sub_arry);
      String stringWithoutHTML=html2text(currentValue);
       System.out.println("description value----->"+n+att_ID+"------>>"+stringWithoutHTML);}

But i didn't get the o/p..pls provide me solution for these ??? how can i remove the html tags in these description...

Wreathe answered 4/3, 2013 at 7:16 Comment(0)
M
17

You can easily remove any HTML tag in Android using the built-in HTML class in Android. Import android.text.Html;. Now, considering "data" is your String variable which has HTML tags, you use Html.fromHtml(data).toString() to get back the string without any HTML tags.

Mismatch answered 3/11, 2013 at 2:36 Comment(0)
S
8

Simple method to remove html. This will return non-html formatted text

 private String removeHtml(String html) {
    html = html.replaceAll("<(.*?)\\>"," ");
    html = html.replaceAll("<(.*?)\\\n"," ");
    html = html.replaceFirst("(.*?)\\>", " ");
    html = html.replaceAll("&nbsp;"," ");
    html = html.replaceAll("&amp;"," ");
    return html;
}

To formatted according html tag and remove tag.

Html.fromHtml(data).toString();
Spear answered 16/6, 2016 at 6:20 Comment(0)
A
3

One option is to add the JSoup library, import it and use it as follows:

public static String html2text(String html) {
return Jsoup.parse(html).text();
}
Advisable answered 4/3, 2013 at 7:20 Comment(0)
L
3
String plain = Html.fromHtml("your_html_string").toString();
Lowell answered 17/3, 2014 at 4:4 Comment(0)
W
2

In Kotlin you can simply do it with this extension function -

fun String.removeHtml() = replace("\\<.*?\\>".toRegex(), "")

and you can call this function as

val cleanString = yourHtmlString.removeHtml()
Wadewadell answered 9/12, 2023 at 6:14 Comment(0)
J
1
    private int iMobileVersion = Build.VERSION.SDK_INT;
    String strResonseTemplate = data.getStringExtra("template"); //getting HTML data in string

    if (iMobileVersion >= 24) {
                                mEtReply.setText(Html.fromHtml(strResonseTemplate, Html.FROM_HTML_MODE_COMPACT));// this code only works on and above API 24, and removes all HTML tag, but gives same view as HTML in Edittext.
                            } else {
                                mEtReply.setText(Html.fromHtml(strResonseTemplate).toString()); // bellow API level 24 we are removing only HTML tags, it will show as normal text.

                            }

Hope this will be helpfull :)

Jungly answered 3/11, 2018 at 8:31 Comment(0)
W
0

According to my knowledge you can get data by spannable interface.

Try using this:

Spanned spannable = Html.fromHtml(arrayList.get(position).getBusinessDescription());
System.out.println("description value----->"+n+att_ID+"------>>"+spannable);

check the below link for more details:

http://developer.android.com/reference/android/text/Spanned.html http://developer.android.com/reference/android/text/Spannable.html

Willett answered 4/3, 2013 at 7:58 Comment(0)
H
0

Just add these few lines of code and you are done .

String html=(jsonObject1.getString("originaltext"));
            html = html.replaceAll("<(.*?)\\>"," ");
            html = html.replaceAll("<(.*?)\\\n"," ");
            html = html.replaceFirst("(.*?)\\>", " ");
            html = html.replaceAll("&nbsp;"," ");
            html = html.replaceAll("&amp;"," ");
            newsModel.setNews(html);
            Log.d("originaltext: ",html);
Hopson answered 20/3, 2018 at 9:38 Comment(0)
W
0
//Patter to detect elements contained into "<>"
private static final Pattern REMOVE_TAGS = Pattern.compile("<.+?>");

//Method to remove the html tags contained in a String variable
public static String removeTags(String string) 
{
  //validate that at least one value contains the string
  if (string == null || string.length() == 0) 
  {
     return string;
  }
  //Function to find the matches within the chain and the pattern       
  Matcher m = REMOVE_TAGS.matcher(string);
  //replace <> element with ""
  return m.replaceAll("");
}

//Implementation of the method to eliminate html tags and place inside a Text control
this.itemView.setText(
 Html.fromHtml(
  new String(removeTags("<h1>My Title here</h1>").getBytes("ISO-8859-1"),"UTF-8")
 )
);
Wageworker answered 27/6, 2019 at 15:49 Comment(1)
Hi Jose! This could be a great answer, can you elaborate and explain some of the steps to the question asker? That would help tremendously.Crosscrosslet

© 2022 - 2024 — McMap. All rights reserved.