I have a WebView that I'm trying to handle behavior for when #close is added to the end of a url.
The code below triggers for every load url that doesn't have #close appended to it. How do I handle for this?
WebViewClient wvc = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("https:<coreofurl>/?#close") ){
// handle event - does not enter
}
}
};
webView.setWebViewClient(wvc);
Edit: I've also tried the following with no luck.
WebChromeClient wcc = new WebChromeClient() {
#Override
public void onCloseWindow(WebView window) {
// handle event - does not enter
}
};
webView.setWebChromeClient(wcc);
This seems like a "how do I tell if my url ends with x" string question, so... try this
if (url.endsWith("#close")) {
// close stuff here
}
(this would be implemented in your onLoadResource() method)
Related
I’m trying to override url loading when clicking on a link on an app WebView.
The page loads but the WebView will keep it’s last scroll position and content size.
Is there some parameter I forgot to set on the WebView to reset the content size and the scroll position on next load?
Here’s how I’m using it:
#SuppressLint("SetJavaScriptEnabled")
#AfterViews
protected void afterViews() {
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebClient());
webView.setWebChromeClient(new ChromeClient());
webView.loadUrl(url);
}
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean isValidEmail = url.startsWith("mailto:") && Patterns.EMAIL_ADDRESS.matcher(url.substring("mailto:".length())).matches();
boolean isValidPhone = url.startsWith("tel:") && Patterns.PHONE.matcher(url.substring("tel:".length())).matches();
if (url.startsWith("about:")) {
return super.shouldOverrideUrlLoading(view, url);
}
if (isValidEmail) {
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
startActivity(Intent.createChooser(sendIntent, ""));
} else {
if (isValidPhone) {
Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(Intent.createChooser(dialIntent, ""));
} else {
WebViewActivity.this.setPageTitle(url);
webView.loadUrl(url);
}
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//..
}
}
public class ChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int progress) {
//...
}
}
Thanks.
Did you try to set scroll position in onPageFinished?
#Override
public void onPageFinished(WebView view, String url) {
// other code...
view.scrollTo(0,0);
}
That should set WebView content back to top, and not on old scroll position
Edit 1:
Sometimes if the page loads for a long time this won't work properly, so we must wait for page to get fully loaded:
#Override
public void onPageFinished(WebView view, String url) {
view.postDelayed(new Runnable() {
#Override
public void run() {
webView.scrollTo(0, 0);
}
// Delay the scrollTo to make it work
}, 300);
}
Please note that webView this time is not the WebView from the method onPageFinished, but the WebView fetched from the layout (same one as in afterViews() method).
#Bojan Kopanja, my sincere apologies for misleading.
It turns out I had the webview inside a pull to refresh listener with a scrollview and removing that got rid of the problem.
Thanks for your help nevertheless.
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);
How would I stop a webview from opening after I click on a link? I also need to extract the URL address so I can put it into a string.
Set WebViewClient :
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO do something with url
return true;
}
});
I'm trying to fetch and display a webpage using WebView, there are chances of page redirection when the webpage is loaded, so is there any option of reading URL's coming as response from first request? I have to perform certain condition checks based on response URL.
Look at shouldOverrideUrlLoading():
WebViewClient wvc = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url) {
if (!url.startsWith("http://m.youtube.com/")) {
// do something
return true;
} else if (url.startsWith("http://m.youtube.com/watch?")) {
// do something different
return true;
}
return false;
}
};
The following steps worked for me:
webview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
if(url.equals(" abc.com")){
//do some thing
}
else
//do some other thing
super.onPageFinished(view, url);
}
});
I am experimenting with the loopj package. I am trying to make a HTTP request to a website and display the website in the webview.
I am successfully getting a result back, however the web view does not display the page as desired, instead chrome opens up and displays the page.
Am I missing something or is there a way I can override this unwanted behaviour?
Below is my oncreate method where I am making the request:
public class MainActivity extends Activity {
Button connectBtn;
TextView status;
WebView display;
String url = "http://www.google.com";
AsyncHttpClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.statusbox);
connectBtn = (Button)findViewById(R.id.connectBtn);
display = (WebView)findViewById(R.id.webView1);
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(String response) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
display.loadUrl(url);
}
});
}
});
}
setWebViewClient to your WebView and override shouldOverrideUrlLoading() now write view.loadUrl(url); in that method.
Just add this code,
display.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
You need to set a WebViewClient and override the shouldOverrideUrlLoading method. Something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
view.loadUrl(url);
}
});
That makes sure that clicks on links in the WebView are handled by the WebView itself.
Edit: Actually, I misread the question. You aren't dealing with a click in the WebView itself, so this isn't relevant. Sorry!
just use this code under you webviewclient
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
final EditText editText = (EditText) findViewById(R.id.urlfield);
editText.setText(url);
}
}