trying to load a simple web page in WebView as below
alway leads to the fact that the browser is being launched with it.
The following code I wrote in my main activity besinde defining
"uses-permission android:name="android.permission.INTERNET" "
in the manifest. There is no Exception thrown which I verified with the catch block.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
WebView myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
} catch (Exception e) {
Log.e("MyApp", e.getMessage());
}
}
I cannot imagine what coult be simpler than my scenario. I guess as I do not do things like clicking a link
it is not necessary to do things like
// By default, redirects cause jump from WebView to default
// system browser. Overriding url loading allows the WebView
// to load the redirect into this screen.
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Do you have any Idea what could go wrong here?
In my very first example I do not use setWebViewClient(... at all but also in that case the browser opens instead of loading the side into the web view.
Thanks and Regards
Dieter
By returning fase in shouldOverrideUrlLoading(), you're instructing the WebView to ignore loading the URL and open the device's default browser instead.
You'll want to replace the contents of that function with:
// By default, redirects cause jump from WebView to default
// system browser. Overriding url loading allows the WebView
// to load the redirect into this screen.
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
Related
I have to make a webbrowser for android, so I want to try to block a site.
How can I do that?
Lets say your WebView id is myWebView then what you will do is this :
WebView wb = (WebView) findViewById(R.id.myWebView);
wb.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("http://yourBlockedUrl.com")){
//notify the user that this url is blocked
return true;
}
return false;
}
});
by doing this you are overriding the url loading of your webview you can thus block a url from loading.
say you get the url sent as a string "www.google.com", from the edittext just do a check if this is a blocked url.
for example
if( "www.google.com".equalsIgnoreCase(blocked_string))
{
webview.setVisibility(View.GONE);
warning_view.setVisibilty(View.VISIBLE);
}
or you could try overriding the shouldOverrideUrlLoading() in the WebViewClient class
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(my_url.equals("www.google.com")){
//do something.
}
return true;
}
Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).
public void gotoUrl(View view) {
EditText theEditText = (EditText)findViewById(R.id.urlTxt);
theUrl = theEditText.getText().toString();
//
// String blok= "http://www.teknojurnal.com";
webBrowserKu.loadUrl(theUrl);
}
my web browser when klik go, will process this, so what is the problem? why I cant do the steps from anything for blocking one site?
I have a "NewsActivity" with a webview. When I start it from my main activity, links are opened in the webview correctly but, for some reason, some url's cause the webview to open but then to immediately launch the external browser. Checking the debug console I have not found any exception or other message thrown by webview as not being able to handle the url.
Please note that I am not talking about a link clicked after the webview has loaded the url/page.
I have also tried to activate javascript in the webview but to no avail.
Also, this happens for just some urls from the same domain (specifically a news website; also, I have no block url or override in place).
Here is one of the urls that fail to open in the webview: url_not_loaded
Here is the code that calls the "NewsActivity"
Intent intent = new Intent(this, NewsActivity.class);
intent.putExtra(MainActivity.EXTRA_MESSAGE, url);
startActivity(intent);
And here is the code in "NewsActivity"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
mWebview = (WebView) findViewById(R.id.newsv);
Log.d(MainActivity.LOG_TAG, "URL: " + url);
mWebview.loadUrl(url);
}
If someone has a clue as to what may be happening or can suggest any idea, I'll be grateful.
Thanks!
Your URL's might have redirects. I encountered a similar problem.
Add this to your activity with the webview.
webView.setWebViewClient(new Callback());
Add this outside of onCreate.
private class Callback extends WebViewClient{ //Helps to open in webview instead of browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
It's not a bug.
You need to use WebViewClient.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
System.out.println("hello");
return false;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
mWebView.loadUrl(url);
You can set a newWebViewClient like this:
webView.setWebViewClient(new WebViewClient());
But to be more precisely you need to override the shouldOverrideUrlLoading method like this:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}});
Is there any way to restore a default WebViewClient in WebView?
There is a WebView.setWebViewClient(WebViewClient client) method, but no get companion.
I would like to to load URL in WebView and avoid being sent to native browser in case of redirections but then go back to default behavior - links clicked opening in native browser.
To achieve that I would like set my own WebViewClient temporarily and then restore the default one.
WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://example.com");
// TODO: restore default WebViewClient
to go back to the default behavior try:
webview.setWebViewClient(null);
however you can't do that immediately after you call loadUrl, you need to at least wait for WebViewClient.onPageFinished.
Finally the code:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.setWebViewClient(null);
}
});
webView.loadUrl(url);
There is a WebView which loads mobile-optimized URL (webpage). But when I click on a link, it does not load inside of the WebView (inside of the app), but mobile browser opens.
How to prevent this?
I tried overloading URLs via shouldOverrideUrlLoading(), but it did not help.
This is a code.
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setPluginsEnabled(true);
if (Build.VERSION.SDK_INT > 7) {
webSettings.setPluginState(WebSettings.PluginState.ON);
}
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(url)) {
view.loadUrl(url);
return true;
}
return false;
}
#Override
public void onLoadResource(WebView view, String url) {
}
});
webView.loadUrl("http://some-url.com");
EDIT
Does GET or POST posting methods have anything with links' clicks open mobile web browser???
Return true instead of false in shouldOverrideUrlLoading.
From the documentation:
shouldOverrideUrlLoading returns True if the host application wants to
leave the current WebView and handle the url itself, otherwise return
false.
I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView. But it's opening the default browser and loading the page there?
I have enabled JavaScript. But still it's not working. Have I forgotten something?
If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:
#Override
public void onLoadResource(WebView view, String url)
{
if (url.equals("http://redirectexample.com"))
{
//do your own thing here
}
else
{
super.onLoadResource(view, url);
}
}
Official documentation says, click on a link in a WebView will launch application that handles URLs. You need to override this default behavior
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
or if there is no conditional logic in the method simply do this
myWebView.setWebViewClient(new WebViewClient());
Add this 2 lines in your code -
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
The method boolean shouldOverrideUrlLoading(WebView view, String url) was deprecated in API 24. If you are supporting new devices you should use boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).
You can use both by doing something like this:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
} else {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
Arulx Z's answer was exactly what I was looking for.
I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
exactly under your WebView statement.
Here's a example of my implemented WebView code:
public class WebView1 extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
wv.setWebViewClient(new WebViewClient()); //the lines of code added
wv.setWebChromeClient(new WebChromeClient()); //same as above
wv.loadUrl("http://www.google.com");
}}
this way, every link clicked in the website will load inside your WebView.
(Using Android Studio 1.2.2 with all SDK's updated)