remove html tag in android

断了今生、忘了曾经 提交于 2019-11-30 22:26:51

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.

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();

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();
}
String plain = Html.fromHtml("your_html_string").toString();

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

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);
    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 :)

//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")
 )
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!