Is there a way to convert Html to String without highlighting hyperlinks? - android

I know that I can use the default Html.fromHtml(string) but it highlights links by default. Is there a way to prevent that behavior?
P.S.
I'm trying to feed it straight to TextView using .setText() without saving it to String. I want to keep all the formatting except links.

If you only need text you can fetch it with:
String string = Html.fromHtml(string).toString();
Edit:
Since you want to remove only the links, you can use String.replaceAll before parsing the html:
// Remove <a href*>
html = html.replaceAll("<a href.*?>", "");
// Remove </a>
html = html.replaceAll("</a>", "");
textView.setText(Html.fromHtml(html));

Related

How to add large amount of text in android?

For my app i want to add a FAQ page which contains large amount of text. Some of it has to be bold, underlined. I have an faq_xml with a scrollview. How do i add this large amount of text in a well displayed format? I read some online forums saying about using the html format. I couldn't understand. Some help would be appreciatd!
android:maxLines = "any integer"
android:scrollbars = "vertical"
Then use:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
in your main activity.
You can create a string containing HTML code. This String will then be used in a webView that you will make in you faq_xml.
Check this thread..
You can assign your html code to a string and then apply that css file you learned to create in the above mentioned thread.
Then call the webView like
wv.loadDataWithBaseURL("", YourStringHere, mimeType, encoding, "");
For your ease check out this thread..

how can i use regex to match this string in android?

I want to grab img tag from text returned from json data like that
‫#رصد| #انقلاب_3يوليو| اليوم ... مبني المركبات العسكري في صلاح سالم<br /> <br /> تصوير المواطن الصحفي : عبدالرحمن النحاس‬<br/><br/><img class="img" src="https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-frc3/1239478_598075296936250_1910331324_s.jpg" alt="" />
i want to grab this
<img class="img" src="https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-frc3/1239478_598075296936250_1910331324_s.jpg" alt="" />
what the reqular expression i must use in android to match it
I used this code but it is not working
String content = e.getString("content");
String img = "";
Pattern p = Pattern
.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
Matcher m = p.matcher(content);
if (m.matches()) {
Log.d("true", m.group(0).toString());
img = m.group(0).toString();
}
Log.d("image", "image : " + content);
Using regular expressions to parse HTML is a very bad idea.
Better to use a true HTML parser and walk the DOM tree to get what you want.
You also need to be careful about proper encoding, since you want Arabic text.
Well... you know you can get the JSON object and parse that without regex? that is probably the best approach. Then you can just strip out the content without worrying about parsing anything from a string because it automatically puts it into variables for you.
How to parse JSON
It can become very messy to mess around with regex for the reasons #duffymo posted above me.
edit:
I see what you are trying to do.... parse the image out of the content section correct? There needs to be two things involved here yes.. regular expressions and also json parsing. You need to grab all the content fields from the json parser then use regex on those to extract the images. That's what you are trying to do correct?

How to skip image tag in html data in android?

My data in db is stored in html format along with image tags in it. So when I am getting the data from database I am removing the html tags and setting it to textview. My problem is even after removing the html tags, there is a small square box displaying in the emulator indicating that there is some image. How can I remove those square box in emulator which is an indication of image in that html data? Help me regarding this...
Thanks in advance
My Code:
textView.setText(Html.fromHtml(htmlString));
You could do a regex replace <img.+?> on htmlString.
textView.setText(Html.fromHtml(htmlString.replaceAll("<img.+?>", "")));
Untested
since images can look like:
<img ...></img>
and
<img... />
This solution will match both cases:
String htmlBody = htmlString.replaceAll("<img.+/(img)*>", "");
textView.setText(Html.fromHtml(htmlBody));
None of the answers are able to remove all possible img tags.
The most appropriate answer would be
String htmlBody = htmlString.replaceAll("(<(/)img>)|(<img.+?>)", "");
It will remove and tags both. And not remove any content between two images.
hi have a look on this example
String temp="<img>helloo</img><b> this is test</b>";
temp= temp.replace("<img>", "");
temp= temp.replace("</img>", "");
textView.setText(Html.fromHtml(temp));

Json parsing converts html tags to escape sequence

I am fetching few html content from my server for which I am using JSON parsing. But this converts my html content to unicode values.
For Eg: <p>Spend minimum $10 (in a single same-day receipt) at any outlet<\/p> is getting converted to,
;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>
Now if I try to set this to my WebView it displays with HTML tags itself. If I try to encode the data using TextUtils.encode it displays the text with unicode values.
Can anyone help me with this.
How should I fetch a HTML content and display it in WebView?
I am not getting your question exactly but, If you want to load HTML in web view in you can use
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
and if you want to convert &lt and &gt like notation you can use Jsoup Library
Guys thanks for your help. But I have solved this issue myself. I have elaborated my way of solving the issue.
What I did is,
1)convert the unicode value to Spanned like this,
Spanned ss=Html.fromHtml(;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>");
2)Now convert this Spanned to String like this,
String tempString=ss.toString();
3)And now set this to WebView which solved the problem,
webView.loadData(tempString, "text/html","UTF-8");
Actually this isn't JSON encoder converts data to HTML entities but some other layer, before it passed to JSON encoder.
JSON have nothing to do with HTML tags, usually only quotes encoded by parser (Unicode is supported by most parsers).
You probably need to change the way data is returned by server, to omit encoding of HTML tags braces to HTML entities or decoding entities backin your app.
Update:
To decode HTML entities used in HTML tags (and others too) you may use StringEscapeUtils.unescapeHTML()
To show the HTML page inside the Webview why you require the JSON. create web view inside the XML and write below code Inside the Activity you can see the HTML page.
webView = (WebView)findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.webView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
webView.loadUrl("http://www.google.co.in/");

Android RegEx doesn't find matches

I am trying to use Regular Expressions to decode some HTML I retrieve from a webpage.
I want to transform some <iframe> tags into links.
The code I'm using should be working fine according to me and some testprograms, however when I run it on my android device it does not find any matches (where as in the test programs it does).
The regular expression I am using is as follows (keep in mind I'm coding in Java, so I need to escape the escape character as well):
String regularExpression = "<iframe.+?src=\\\\?(\\S+).+?(><\\\\?/iframe>|\\\\?/>)";
String replacement = "<a href=$1>Youtube</a>";
input.replaceAll(regularExpression, replacement);
From what I can gather from this it should replace all <iframe> tags that have a src attribute to hyperlinks with that source. However when I feed the following input to it, it does nothing with it:
<iframe src=\"http:\/\/www.youtube.com\/embed\/s6b33PTbGxk\" frameborder=\"0\" width=\"500\" height=\"284\"><\/iframe>
The response is simply the exact same text, only with the escape-characters removed:
<iframe src="http://www.youtube.com/embed/s6b33PTbGxk" frameborder="0" width="500" height="284"></iframe>
Can someone help me and explain what I'm doing wrong? I only started learning Regular Expressions yesterday, but I just can't for the life of me figure out why this doesn't work.
The method String.replaceAll doesn't modify the string. It can't because strings are immutable. Instead it returns a new string with the result. You need to assign this result to something:
String result = input.replaceAll(regularExpression, replacement);
Also, don't use regular expressions to parse HTML.
String resultString = subjectString.replaceAll("(?=<(iframe)\\s+src\\s*=\\s*(['\"])(.*?)\\2[^>]*>).*?</\\1>", "<a href=$3>Youtube</a>");
This should work. In addition to #Mark Byers note your regex does not seem to match to your input, even with removed (double) backslashes.

Categories

Resources