I am loading string value in my web view and i need when i click on the link shown in web view should open in other web browser of phone.
I want like this - String s = "hello read more.... www.google.com"
String html = "<html><body>+s+</body></html>"
webview.loaddata(html);
Try this:
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
You can do something like this:
String s = "<a href='http://www.google.com' target='_blank'>Read more over here</a>";
webview.loaddata(s);
You will need to add other html content by yourself.
So basically, you can use target='_blank' which will open it in a new browser, not in webview.
Hope it helps.
Related
I have a webView and it gets the URL dynamically so I can't edit the HTML document. My problem is that I want the app to detect phone numbers and provide intent but the HTML document does not contain an anchor tag for telephone numbers so I cannot detect the URL. Is there any way to make the WebView identify phone numbers by itself?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith(MAIL_TO)){
final String RE_MAIL = "([\\w\\-]([\\.\\w])+[\\w]+#([\\w\\-]+\\.)+[A-Za-z]{2,4})";
Pattern p = Pattern.compile(RE_MAIL);
Matcher m = p.matcher(url);
while(m.find()) {
sendEmail( m.group(1) );
}
return true;
}else if(url.startsWith(TEL_PREFIX)){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
String appHint = "Choose phone application";
context. startActivity(Intent.createChooser(intent, appHint ));
return true;
}
else{
view.loadUrl(url);
}
return false;
}
I'm loading local html using loadDataWithBaseURL method.
I'm calling the meod setHTML with the source, and it's going well. the link is VALID, as i've seen in previous questions on stackoverflow, so everything is set.
When the user clicks on a link, it works and its opening the ArticleActivity, but when he clicks on an image (from the html), the method isn't called, even tho the image has a link.
What would be the problem here?
public void setHTML(String source) {
Document doc = Jsoup.parse(source);
String html = WebHelper.docToBetterHTML(doc, this);
mHTMLWebView.loadDataWithBaseURL(mLink, html, "text/html", "UTF-8", "");
mHTMLWebView.setVisibility(View.VISIBLE);
}
mHTMLWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Animation fade = AnimationUtils.loadAnimation(mLoadingLayout.getContext(), android.R.anim.fade_out);
mLoadingLayout.startAnimation(fade);
mLoadingLayout.setVisibility(View.GONE);
}
},500);
}
public boolean shouldOverrideUrlLoading(WebView view, String url){
//super.shouldOverrideUrlLoading(view,url);
if(url != null
&& (url.endsWith(".png") || url
.endsWith(".jpg") || url
.endsWith(".jpeg"))){
// Launch imageviewer
ArrayList<String> list = new ArrayList<>();
list.add(url);
try{
new ImageViewer.Builder(view.getContext(), list)
.setStartPosition(0)
.show();
} catch (Exception e){
e.printStackTrace();
}
} else if (url != null
&& url.contains("dkhlak.com")){
Intent intent = new Intent(view.getContext(),ArticleActivity.class);
intent.putExtra("link",url);
view.getContext().startActivity(intent);
} else {
Intent ViewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
try{
view.getContext().startActivity(ViewIntent);
} catch (Exception e){
e.printStackTrace();
}
}
return true;
}
});
Example:
<p>\u0625\u0646 \u0639\u0627\u0644\u0645\u0646\u0627 \u0645\u0643\u0627\u0646\u064c \u063a\u0631\u064a\u0628\u064c \u062c\u062f\u0627\u064b\u060c \u0641\u0641\u064a \u0628\u0639\u0636 \u0627\u0644\u0623\u062d\u064a\u0627\u0646 \u064a\u0648\u062c\u062f \u0623\u0634\u064a\u0627\u0621\u064c \u0643\u062b\u064a\u0631\u0629 \u0646\u0638\u0646\u0647\u0627 \u0639\u0627\u062f\u064a\u0629\u060c \u0644\u0643\u0646\u0647\u0627 \u0642\u062f \u062a\u0643\u0648\u0646 \u0641\u064a \u0646\u0638\u0631 \u063a\u064a\u0631\u0646\u0627 \u0645\u0646 \u0627\u0644\u0646\u0627\u0633 \u063a\u064a\u0631 \u0639\u0627\u062f\u064a\u0629\u060c \u0628\u0644 \u0623\u0646\u0647\u0645 \u0642\u062f \u064a\u0631\u0648\u0646\u0647\u0627 \u0642\u0628\u064a\u062d\u0629. \u0644\u0643\u0646 \u0641\u064a \u0627\u0644\u0646\u0647\u0627\u064a\u0629 \u064a\u0648\u062c\u062f \u0623\u0634\u064a\u0627\u0621 \u0646\u062c\u0645\u0639 \u0643\u0644\u0646\u0627 \u0639\u0644\u0649 \u0642\u0628\u062d\u0647\u0627 \u0648\u0628\u063a\u0636\u0647\u0627\u060c \u0648\u0630\u0644\u0643 \u0644\u0623\u0646\u0647\u0627 \u063a\u064a\u0631 \u0625\u0646\u0633\u0627\u0646\u064a\u0629 \u0648\u0638\u0627\u0644\u0645\u0629 \u0644\u062f\u0631\u062c\u0629 \u063a\u064a\u0631 \u0642\u0627\u0628\u0644\u0629 \u0644\u0644\u062a\u0635\u0648\u0631.<\/p>\n<p>\u0648\u0645\u0647\u0645\u0627 \u0643\u0627\u0646 \u0642\u0628\u062d \u062a\u0644\u0643 \u0627\u0644\u0623\u0634\u064a\u0627\u0621 \u0643\u0628\u064a\u0631\u0627\u064b\u061b \u064a\u0628\u0642\u0649 \u0644\u062f\u064a\u0646\u0627 \u0641\u0636\u0648\u0644 \u0643\u0628\u064a\u0631 \u0644\u0645\u0639\u0631\u0641\u0629 \u0645\u0627\u0647\u064a\u062a\u0647\u0627\u060c \u0641\u0641\u064a \u0627\u0644\u0645\u0642\u0627\u0644 \u0627\u0644\u062a\u0627\u0644\u064a \u0633\u0646\u0639\u0631\u0636 \u0644\u0643\u0645 \u0633\u062a \u0639\u0634\u0631\u0629 \u062d\u0642\u064a\u0642\u0629 \u0635\u0627\u0631\u062e\u0629 \u062c\u0627\u0648\u0632\u062a \u0641\u064a \u0642\u0628\u062d\u0647\u0627 \u0623\u0628\u0639\u062f \u0627\u0644\u062d\u062f\u0648\u062f:<\/p>\n<p><strong>1. \u0641\u064a \u0645\u0637\u0644\u0639 \u0627\u0644\u0642\u0631\u0646 \u0627\u0644\u0639\u0634\u0631\u064a\u0646\u060c \u0639\u064f\u0631\u0636 \u0627\u0644\u0631\u062c\u0644 \u0627\u0644\u0643\u0648\u0646\u063a\u0648\u0644\u064a Ota Benga \u0641\u064a \u0623\u062d\u062f \u0623\u0642\u0641\u0627\u0635 \u0627\u0644\u0642\u0631\u062f\u0629 \u0641\u064a \u062d\u062f\u064a\u0642\u0629 Bronx \u0643\u0646\u0645\u0648\u0630\u062c \u062d\u064a \u0644\u0645\u0631\u0627\u062d\u0644 \u0627\u0644\u062a\u0637\u0648\u0631 \u0627\u0644\u0628\u0634\u0631\u064a\u0629 \u0627\u0644\u0645\u0628\u0643\u0631\u0629. \u0645\u0635\u064a\u0631 Benga \u0625\u0646\u062a\u0647\u0649 \u0628\u0625\u0642\u062f\u0627\u0645\u0647 \u0639\u0644\u0649 \u0627\u0644\u0625\u0646\u062a\u062d\u0627\u0631 \u0646\u062a\u064a\u062c\u0629 \u0644\u0633\u0648\u0621 \u0627\u0644\u0645\u0639\u0627\u0645\u0644\u0629 \u0627\u0644\u062a\u064a \u062a\u0639\u0631\u0636 \u0625\u0644\u064a\u0647\u0627\u060c \u0648\u0627\u0644\u0625\u0643\u062a\u0626\u0627\u0628 \u0627\u0644\u0630\u064a \u0623\u0635\u0627\u0628\u0647 \u0644\u0627\u062d\u0642\u0627\u064b.<\/strong><em>\u00a0\u2014<a href=\"https:\/\/en.wikipedia.org\/wiki\/Ota_Benga\" rel=\"nofollow\">\u0627\u0644\u0645\u0635\u062f\u0631<\/a><\/em><\/p>\n<div id=\"attachment_15079\" style=\"width: 1034px\" class=\"wp-caption aligncenter\"><img class=\"size-full wp-image-15079\" src=\"https:\/\/dkhlak.com\/wp-content\/uploads\/2017\/06\/15071-1.jpg\" alt=\"\u0627\u0644\u0631\u062c\u0644 \u0627\u0644\u0643\u0648\u0646\u063a\u0648\u0644\u064a Ota Benga\" width=\"1024\" height=\"580\" srcset=\"https:\/\/dkhlak.com\/wp-content\/uploads\/2017\/06\/15071-1.jpg 1024w, https:\/\/dkhlak.com\/wp-content\/uploads\/2017\/06\/15071-1-400x227.jpg 400w, https:\/\/dkhlak.com\/wp-content\/uploads\/2017\/06\/15071-1-768x435.jpg 768w, https:\/\/dkhlak.com\/wp-content\/uploads\/2017\/06\/15071-1-810x459.jpg 810w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/>
In my activity class:
final String input = slateURL + OtherHalfUrl;
//here I am combining my Strings to make it a complete Url
//for example slateURL = "http://example.com/id=" & OtherHalfUrl = 69
//So String input becomes like this "http://example.com/id=69"
Log.e("Complete URL", input);//in log I am checking and it is correct Url
webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl(input);//no output
// webView.loadUrl("http://example.com/id=69");//getting output
what is the reason for this or am I Doing something wrong.
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
OtherHalfurl I am getting from my other activity fragment like this:
Intent intent = new Intent(getContext(), GoToActivity.class);
intent.putExtra("id",idno);
And getting it like this :
String OtherHalfUrl = getIntent().getStringExtra("id");
Try this
webView.loadUrl(YOUR URL);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
I am trying to open pdf links in my webview using Google Docs. The Webview initially accepts the url but in log cat i notice that at some point the url changes to "about:blank" and in the end it load only a blank page.
I have also overriden onPageStarted and onPageFinished methods and noticed that when shouldOvverideUrlLoading is called the second time by the WebView it changes the url to "about:blank"
Any thoughts on why this happens?
#Override
public boolean shouldOverrideUrlLoading(String url) {
getView().loadWebview(url);
}
#Override
public void loadWebview(String url) {
String pdfSuffix = ".pdf";
String googleDocsUrl = "http://docs.google.com/viewer?url=" + url;
if (url.endsWith(pdfSuffix)) {
Logger.i(LOG_TAG, "pdf loadwebview: url= " + googleDocsUrl);
mView.loadUrl(googleDocsUrl);
} else {
Logger.i(LOG_TAG, "loadwebview: url= " + url);
mView.loadUrl(url);
}
}
I'm developing an Android application that reads ebooks (in epub format) and as for now I'm using Paul Siegeman's epublib library that is really a very good epub reader but it has some limitations, for example and the one I need, you can't move through pages horizontally (as you do reading a real book) so I need my own implementation of it, but I'm stuck.
The method that actually reads the epub and then puts it inside a webview is the next:
private void openEpub(String bookFilename){
WebView webView = (WebView) findViewById(R.id.webView);
nl.siegmann.epublib.domain.Book book=null;
try {
book = (new EpubReader()).readEpub(new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/" + bookFilename));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String baseUrl = Environment.getExternalStorageDirectory().getPath() + "/";
String data=null;
try {
data = new String(book.getContents().get(1).getData());
} catch (IOException e) {
e.printStackTrace();
}
webView.loadDataWithBaseURL(baseUrl, data, "text/html", "UTF-8", null);
}
So as you see I display the ebook in a webview so as far as I know the only scrolling possibility webview gives is up/down.
I was thinking on splitting the html string that getData() returns and webview loads into pages and displaying them one by one with a viewpager, but how to split the html correctly according to screen size?
Do you think with this idea I'm on the right way? Any other solutions to display epub from left to right / right to left (paginate) or any other "free or cheap" library to do so? (I tried PageTurner, it's really good, but the commercial version is too expensive for me)
I have done pagination effect in android like this..
-> create a custom webview class.
-> set below clients and load url then you will get horizontal scrolling with page count.
-> Lock the webview default scroll.
-> For smooth pagination effect instead of moving scroll of webview ,move the entire webview so for one page there would be one webview.
-> Use your own viewflippers to buffer previous and next pages.
I have done all these implementations and I made a product for an organisation.Just I am sharing my idea how to approach towards the best solution.Instead of using third parities and stucking in the middle due to limitation of that sdk ,make every thing your own.
private class MyWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
final MyWebView myWebView = (MyWebView) view;
String varMySheet = "var mySheet = document.styleSheets[0];";
String addCSSRule = "function addCSSRule(selector, newRule) {"
+ "ruleIndex = mySheet.cssRules.length;"
+ "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"
+ "}";
String insertRule1 = "addCSSRule('html', 'padding: 0px; height: "
+ (myWebView.getMeasuredHeight()/getContext().getResources().getDisplayMetrics().density )
+ "px; -webkit-column-gap: 0px; -webkit-column-width: "
+ myWebView.getMeasuredWidth() + "px;')";
myWebView.loadUrl("javascript:" + varMySheet);
myWebView.loadUrl("javascript:" + addCSSRule);
myWebView.loadUrl("javascript:" + insertRule1);
}
}
private class MyWebChromeClient extends WebChromeClient
{
#Override
public void onProgressChanged(WebView view, int newProgress)
{
super.onProgressChanged(view, newProgress);
if(newProgress == 100)
{
postDelayed(new Runnable()
{
#Override
public void run()
{
calculateNoOfPages();
}
},200);
}
}
}
private void calculateNoOfPages()
{
if(GlobalSettings.EPUB_LAYOUT_TYPE == GlobalConstants.FIXED)
{
}
else
{
if(getMeasuredWidth() != 0)
{
int newPageCount = computeHorizontalScrollRange()/getMeasuredWidth();
getData().getChapterVO().setPageCount(newPageCount);
}
}
}
#Override
public int computeHorizontalScrollRange() {
// TODO Auto-generated method stub
return super.computeHorizontalScrollRange();
}
one you load url to
Follow this github account.
FbReader provide some great libraries for epub and pdf reader. Try this....
or
You can Make your own custom WebView by extending WebView. Here you can place and modify all the functionalities you want from your WebView.
My colleague made a reader using this FbReader and It was fabulous.
Hey I think this will help you. The answer by Nacho L worked for me. Here HTML book-like pagination?