Avoid webview redirect - android

I have a WebView in my project and its loaded with some initial content. Now when the user clicks on the hyperlink on the WebView it should not reload the WebView, instead it should display the initial content. I tried the below code but it is not working.
detailsAndTerms.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
Please help to solve this problem. Thanks in advance.

detailsAndTerms.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted (WebView view, String url, Bitmap favicon)
{
if (url != termsurl) {
// stop loading page if its not the originalurl.
detailsAndTerms.stopLoading();
}
}
});
Update
Try returning false from the shouldOverrideUrlLoading method.
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return true;
}

Related

Android WebView Links Does not Open

How can I click on a link that is provided in the WebView data that I load?
The WebView data that is load is in HTML format as:
<P CLASS="western" ALIGN=JUSTIFY STYLE="margin-bottom: 0.14in">Further
here</P>
The WebView is placed inside a scroll view and its webClient is as:
webViewClient = new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url)
{
}
};
webView = (WebView) findViewById(R.id.tos); // Defining the WebView for the terms and conditions and loading the required data
webView.setWebViewClient(webViewClient);
webView.loadData(getResources().getString(R.string.terms_of_service), "text/html", "UTF-8");
The problem is that when I click on the Link no action takes place. How can this be resolved?
use this code to open
WebView webView;//make sure to initialize
webView.setWebViewClient(webViewClient);
WebViewClient webViewClient= new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("http://yoururl.com") ){
// do something
}
}
}

web view url goes to browser .how to do avoid and open in same web view?

here my code...am already implemented WebViewClient concept.it works well in web view..In my App have content saring via fb,twitter,g+,,,fb also login and open in webview...but when am click share to fb icon in my app after that all process will work on browser..but i want all process in webview only...
oncreate
webView = (WebView) findViewById(R.id.webView1);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
if (Build.VERSION.SDK_INT < 8) {
...
} else {
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
webView.loadUrl("www.example.com.");
WebViewClient
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onLoadResource (WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/myerrorpage.html");
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
You must override your shouldOverrideUrlLoading() method in WebViewClient class and following code
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Go through the documentation for more information.
Use a if clause in your shouldOverrideUrlLoading method that returns false;
Example from Building web apps in web views
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}

WebView still loads url even if shouldOverrideUrlLoading returns false

#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url); // Also tried with return false;
}
onPageStarted and onPageFinished are empty.
Still WebView loads the Url.
I think If you don't want to load url then you have to return true in shouldOverrideUrlLoading method.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return true;
}
Please check this once, it may work.

Android WebView custom action on html button of a web page

I have loaded a web page in WebView via WebView.loadData().
Now I want to ad custom action to a button of that webpage from the WebView (Like, when I click the button, it will behave differently from the original webpage).
Is it possible from WebView? If possible, how can I do that?
Yes, it is possible. You can try something like this
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equalsIgnoreCase("your url"))
{
// your code
return true;
}
return false;
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
}
}
Don't forget to add myWebClient to your webView
No, it's no possible. You can only webView.setWebViewClient(new CustomWebClient());
private class CustomWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url...) // your logic here
return true;
}

Don't navigate to other pages in WebView,disable links and references

I have a webView in Android, and I open a html webpage in it. But it's full of links and images, and when I click one of them, it loads in my webview. I want to disable this behaviour, so if I click on a link, don't load it. I've tried this solution and edited a bit for myselft, but not worked.
My webviewclient code:
private boolean loaded = false;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(loaded == false){
view.loadUrl(url);
loaded = true;
return true;
}else{
return false;
}
}
My webview implementation and settings.
WebView wv = (WebView) findViewById(R.id.recipeWv);
ourWebViewClient webViewClient = new ourWebViewClient();
webViewClient.shouldOverrideUrlLoading(wv, URLsave);
wv.setWebViewClient(webViewClient);
wv.setFocusableInTouchMode(false);
wv.setFocusable(false);
wv.setClickable(false);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);
Example: If the user open in my webview the StackOverflow homepage and clicks on one of the links(like "Questions") then the webview should stay on the StackOverflow homepage.
You should save in a class variable your current url and check if it's the same with the loaded one. When the user clicks on a link the shouldOverrideUrlLoading is called and check the website.
Something like this:
private String currentUrl;
public ourWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
view.loadUrl(url);
}
return true;
}
Important: don't forget set the WebViewClient to your WebView.
ourWebViewClient webViewClient = new ourWebViewClient(urlToLoad);
wv.setWebViewClient(webViewClient);
Implement a WebViewClient and Just return true from this method of WebView Client
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
If you want to open inner link of a web page in different window then
Don't use
WebView webView;//Your WebView Object
webView.setWebViewClient(new HelpClient());// Comment this line
B'coz setWebViewClient() method is taking care of opening a new page in the same webview. So simple comment this line.
private class HelpClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Hope this will work.
Even I had been facing the same issue. I solved this issue like below
webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
With lambda expressions
webView.setOnTouchListener((v, event) -> true);
Note that the method signature changed in API 24.
For APIs earlier than 24 the second parameter is String and that signature is now deprecated.
For APIs 24 and later, the second parameter is WebResourceRequest.
If your app supports both pre and post API 24 and you want to disable all links you can use this:
webView.setWebViewClient(new WebViewClient(){
#Override //for APIs 24 and later
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
return true;
}
#Override //for APIs earlier than 24
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
});
You need to add an extra line of code, like so-
webview.loadUrl(url);
webview.setWebViewClient(new MyWebViewClient());
And add an additional class MyWebViewClient like this-
/* Class for webview client */
class MyWebViewClient extends WebViewClient {
// show the web page in webview but not in web browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
Using inject javascript On #Override onPageFinished
view.loadUrl("javascript: (function () {document.addEventListener('click', function (e) {e.stopPropagation();}, true);})()");
Very similar to Amit Gupta's answer, but I found a shorter way to do it.
private String currentUrl;
public CustomWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return ! url.equals(currentUrl);
}
After this CustomWebViewClient is defined, set the client to the webview.
CustomWebViewClient webViewClient = new CustomWebViewClient(urlToLoad);
wv.setWebViewClient(webViewClient);

Categories

Resources