I use WebChromeClient for opening a link to news.html on my server, it opens safari and shows the content, ok.
But...
When I implent WebViewClient shouldOverrideUrlLoading to intercept the call when it's link to a .pdf file (use another class for it) the WebChromeClient link to news.html stays in webview and does not open safari anymore...
I am doing something wrong, but what?
Code snippet: (links are long so I shortened it)
myWebView = (WebView)findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
//only to catch url override
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
else {
return false ;
}
}
});
myWebView.setWebChromeClient(new WebChromeClient());
//loading
myWebView.loadUrl("http://www.domain.nl/news.html");
return false; means that the url wasn't handled and it should be opened in the WebView. If you want to open the url in an external browser you need to explicitly do it.
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return true;
}
});
To open the PDF file url :
add the "http://docs.google.com/viewerembedded=true&url=" string at starting of PDF file url string...
try this it work
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView;
webView = new WebView(this);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginsEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
if (webUrl.equals(XmlFileUrl.MemberDownloadAppForm)) {
webUrl = "http://docs.google.com/viewerembedded=true&url=" + webUrl;
}
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
setContentView(webView);
}
An example that helped me to do something similar Here
For geolocation add:
myWebView.getSettings().setGeolocationEnabled(true);
and :
private class MyWebChromeClient extends WebChromeClient {
.........
#Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback)
{
// callback.invoke(String origin, boolean allow, boolean remember);
callback.invoke(origin, true, false);
}
}
Related
I recently implemented the shouldInterceptRequest method to detect when the link "http://sitemercado.com.br/valida" clicked to open it in the android browser instead of opening internally in the webview until it worked the link is opened in the browser but when I come back for the webview application, it is also loaded, I would like it to load only in the browser.
My code is the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (findViewById(R.id.pb));
mWebView = findViewById(R.id.webview);
mWebView.setListener(this, this);
mWebView.loadUrl("https://www.sitemercado.com.br/frade");
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Intent i = new Intent(MainActivity.this, off.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.sitemercado.com.br/valida")) {
return true;
}
String valida = "https://www.sitemercado.com.br/valida";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(valida));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return false;
}
});
Where am I going wrong?
Unless I'm mistaken, the following is just wrong:
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.sitemercado.com.br/valida")) {
return true;
}
String valida = "https://www.sitemercado.com.br/valida";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(valida));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return false;
}
});
You're getting the host of the URL, then comparing it to a full URL. That's never going to be equal.
Then you launch the link only if that statement is false? That's inverted, since you say in your question that you want it to launch in the browser only if the URL is /valida. The way you have it, no matter what URL you attempt to load, Android will launch a browser pointing to /valida.
Try this instead:
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getPath().contains("valida")) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..
Here's my code:
public class MainActivity extends Activity {
#SuppressLint("SetJavaScriptEnabled") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://www.shufflemylife.com/shuffle");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("/shuffle")){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
return true;
}
}
}
Basically, I want any url containing '/shuffle' to load inside WebView, and anything else to be opened in the external browser. Is it doable? How close am I do accomplishing this?
Thanks for any help!
To clarify, this is how you should write your shouldOverrideUrlLoading method:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("/shuffle")) {
mWebView.loadUrl(url);
return false;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
}
Is it doable?
Yes.
How close am I do accomplishing this?
Close, but backwards. The default behavior of a WebView is to display links in the external browser. Hence, if url.contains("/shuffle"), you want to call loadUrl() on your WebView to keep the link internal, and return true in that case. If this is a URL you want handled normally, return false.
I'm trying to use a facebook login button that uses JS Facebook SDK on Android Webview. When I click it open a new page and redirects to https://www.facebook.com/dialog/oauth... which is a blank page with a javascript code. And the webview stays here.
I'm using:
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
Thank you!
You need to add logic to redirect back to the original url of your website.
In order to do this, you need to first create a new java class that extends the WebViewClient class and overrides the onPageFinished method like this:
public class CustomWebViewClient extends WebViewClient
{
#Override
public void onPageFinished(WebView view, String url) {
//https://www.facebook.com/dialog/permissions.request
//actually works for me, but I put the URL you say is coming up
//blank in there instead, whatever works for you:
if(url.startsWith("https://www.facebook.com/dialog/oauth")){
String redirectUrl = "http://www.mydomain.com/MyApp/";
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
Second, just add it to your WebView:
webview.setWebViewClient(new CustomWebViewClient());
Once that page is finished loading, it will redirect back to your original page
I dont know what exactly i did at that time, but the problem was solved. I'll give you the code. Try finding out :)
WebView browser,mWebviewPop;
private void open(){
browser = (WebView)findViewById(R.id.webView1);
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setAppCacheEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.getSettings().setSupportMultipleWindows(true);
browser.setWebViewClient(new MyBrowser());
browser.setWebChromeClient(new MyCustomChromeClient());
mContext=this.getApplicationContext();
browser.loadUrl(target_url);
MainActivity.this.progressBar.setProgress(0);
browser.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
private class MyBrowser extends WebViewClient {
String redirectUrl = "MY URL";
private void noInternet() {
WebView webview = (WebView) findViewById(R.id.webView1);
RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
tryAgainLayout.setVisibility(View.VISIBLE);
webview.setVisibility(View.GONE);
webview.destroy();
progressLayout.setVisibility(View.INVISIBLE);
}
public void visible(){
WebView webview = (WebView) findViewById(R.id.webView1);
RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
tryAgainLayout.setVisibility(View.INVISIBLE);
webview.setVisibility(View.INVISIBLE);
progressLayout.setVisibility(View.VISIBLE);
}
public void unvisible(){
WebView webview = (WebView) findViewById(R.id.webView1);
RelativeLayout tryAgainLayout = (RelativeLayout)findViewById(R.id.tryAgainLayout);
RelativeLayout progressLayout = (RelativeLayout)findViewById(R.id.progressLayout);
tryAgainLayout.setVisibility(View.INVISIBLE);
webview.setVisibility(View.VISIBLE);
progressLayout.setVisibility(View.INVISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
if (host.equals(target_url_prefix))
{
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
baseLayout.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com"))
{
return false;
}
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
noInternet();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
visible();
}
#Override
public void onPageFinished(WebView view, String url) {
unvisible();
System.out.println("\n" +view.getUrl());
if(url.startsWith("https://m.facebook.com/v2.1/dialog/oauth")){
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
baseLayout.removeView(mWebviewPop);
mWebviewPop=null;
}
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
private class MyCustomChromeClient extends WebChromeClient
{
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new MyBrowser());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
baseLayout.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
#Override
public void onCloseWindow(WebView window) {
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
OK so from what I can tell, what's happening here is that there are two "ways" for oauth to work, either it will call back "to aparent page" (like popup "log me in" that disappears), or it can redirect you to a login page, then to some other url (non popup style), after success or failure (kind of a chain forward). Unfortunately it appears WebView is not well suited for the "callback to the parent page" style, so you have to do some shenanigans like have a second WebView (jincy's answer here, or see also this).
I have a WebView on my application and some links which are URL adresses.
Some of them don't have the baseURL, only something like "/abc/abc"
I would like to set the baseURL for all the links starting with "/".
I have tryed this:
WebView wv = (WebView) findViewById(R.id.EditorialWebView);
wv.setBackgroundColor(0x00000000);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL("", editorialBoard, "text/html", "utf-8", "");
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if (url.startsWith("/"))
{
wv.loadUrl("http://www.mdpi.com"+url);
}
return true;
}
});
But nothing happens.
Try something like this
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("/")) {
Uri u = Uri.fromParts("http://www.domain.com", url, "");
view.loadUrl(u);
}
return true;
}
});
Please let me know if it works for you as I am unable to test it at the moment.