Xamarin Android Webview Linkify email addresses - android

I am trying to understand how I can get my WebView to "Linkify" the email addresses. I want the email address to generate a hyperlink with mailto tag. All the browsers seems to be able to do this automatically. I am trying the following but I still do no get the links to show in the WebView UI.
HtmlString = "<html><head></head><body><label>Email:</label><span>xxx.yyy#example.com</span></body></html>"
SpannableString sp = new SpannableString(HtmlString);
Linkify.AddLinks(sp, MatchOptions.EmailAddresses);
string linkifiedContent = sp.ToString();
WebView.LoadDataWithBaseURL(BaseURL, linkifiedContent, MimeType, null, null);

Linkify is mostly used with TextView. It is used to create clickable links inside TextView.
In your case, you are already making use of WebView.
Hence, you can just add the email address inside <a> tags:
var HtmlString = "<html><head></head><body><label>Email:</label><span><a href='mailto:xxx.yyy#example.com'>xxx.yyy#example.com<a/></span></body></html>";
webView.LoadData(HtmlString, "text/html", "UTF-8");
This should load your Html content perfectly.
Happy coding!

Related

Android- getSpans() always returns an array of length 0

This has been driving me crazy:
String message = "Here is some example test with URLs from mattheworiordan.com.
Any of the URLs which are prefixed with www. should become URLs such as www.mattheworiordan.com/path_name.html
And an explicit URL such as http://www.mattheworiordan.com/ should become a URL.
Emails such as matt#google.com should become links to.
Or email mailto links such as mailto:matt#google.com should become links to.
And of course lets not forget querstryings such as http://mattheworiordan.com/?test-param=true_or_false"
SpannableString spannable = new SpannableString(message);
URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
Linkify.addLinks(spannable, Linkify.ALL);
Log.v("data_", "length:" + Integer.toString(spans.length));
The length of the URLSpan[] array is always 0. What am I doing wrong here and how can I parse all urls in the String?
Edit: Html.fromHtml(message) in place of the message parameter returns the same result.
It is because you don't have any spans. In the case of Html.fromHtml to get a URLSpan, you have to use the tag <a href. E.g.
String message = "Here is some example test with URLs from mattheworiordan.com.
Any of the URLs which are prefixed with www. should become URLs such as www.mattheworiordan.com/path_name.html
And an explicit URL such as <a href='http://www.mattheworiordan.com/'>http://www.mattheworiordan.com/</a> should become a URL.
Emails such as matt#google.com should become links to.
Or email mailto links such as mailto:matt#google.com should become links to.
And of course lets not forget querstryings such as http://mattheworiordan.com/?test-param=true_or_false"
will yield 1. If you don't want to use Html, then you will have to set the URLSpans programatically
Simple error. Try calling Linkify.addLinks before calling getSpans.

How to get editable web view content to string in Android.?

I want to implement a Rich text editor by using a webview in android. Content can be loaded by using a HTML file (which resides in assets) without any problem. But if user has edited the content of the webview (with all the formatting), I need to get the modified content as a HTML string and save it in the database. How can I do this?
I tried in many ways but it seems that we need to pass a URL to get the content of the webview. But after editing the webview content, how can we get the edited URL? or current updated webview content to HTML formatted string?
Using below code I made editable web view.
String msgBody = "<html>\n"+
"<body>\n"+
"<div id=\"content\" contenteditable=\"true\" style=\"font-family:Helvetica;font-size:14px\">" + a +" </div>\n"+
"</body>"+
"</html>";
// wbview = (WebView)this.findViewById(R.id.wbview);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.loadDataWithBaseURL("", msgBody, "text/html", "UTF-8", "");
wbView.setHorizontalScrollBarEnabled(true);
wbView.setVerticalScrollBarEnabled(true);
In iOS we can get it easily by using below code line.
NSString* html=[_tbEmail.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].innerHTML"];

Inserting HTML formatted (not a resource) string in android TextView

I need to plug an HTML formatted string into a TextView. I'm retrieving the text from a database and when I insert it using this code
WebView webview = (WebView) view.findViewById(R.id.webView);
text = "<html><body>" + {text retrieved form database} + "</body></html>"
webview.loadData(text, "text/html", "utf-8");
It works but this code
TextView output = (TextView) view.findViewById(R.id.simple_text);
output.setText(Html.fromHtml(text));
strips out all HTML formatting.
What is the right way to do this?
Start by getting rid of the <html><body> and </body></html> parts, as Html.fromHtml() does not use them.
Then, limit your HTML to use tags that are supported by Html.fromHtml().

Load a div into a webview on Android

I have an Android app, which contains a WebView, and I would like to display in it not a webpage, but only a div from that webpage. I should mention that I do not have access to that page.
I would recommend Jsoup. It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use. For example if you want to pull a div with id example you would do the following:
Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#example");
Then to load the HTML you've extracted into your web view you would do:
String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
webView.loadData(html, mime, encoding);
You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string. You may find an HTML parser library (e.g. tagsoup) useful for this.

Is there any way to have WebView auto-link URLs and phone numbers in Android?

If a page has a URL or a phone number on it that isn't a link is there any way to have WebView recognize it and automatically turn it into a link like you can with TextViews?
With a TextView you would simply set the android:autoLink to the desired settings:
<TextView
android:autoLink="web|phone"
... />
but I can't find any equivalent for WebView.
If you are loading your own (web) content from a String, then you can do something like this:
final String content = "My email is: firstname#email.com ...";
Spannable sp = new SpannableString(content);
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
myWebView.loadData(html, "text/html", "utf-8");
I don't know about any way which would make this work just by changing a setting, but a workaround would be to wait until the web page finishes loading and then do:
yourWebView.loadUrl("javascript:(function(){ /* code that creates links */ })()");
This will inject javaScript into the already loaded web page.
There's a slightly longer example available here: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.
You can find the JavaScript source for creating links if you take a look at the source of Linkify script for Greasemonkey (it's a plugin for Firefox in case you're not familiar with it). I believe it comes with the default install.

Categories

Resources