I am loading an Url from that page, we do some action and redirect to a New page inside WebView. now I want to get Callback when redirected page finishes loading.
I have tried
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
L.d(url);
if (url.contains("Thank-you")) {
Utility.showToast(TabsActivity.this, "Subscription Done");
}
}
and also override
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
L.d(url);
view.loadUrl(url);
return true;
}
but I am still not able to get the callback from URL.
a bit of guessing is needed here, but I still believe, that you are try to find the string "Thank-you" in the url, but the url is written in lower case. Also you could try using WebChromeClient and then the onProgressChanged method.
Regards Tarik.
Register WebViewClient on your webview to return page finish listerner like this:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
Related
I developed a simple webview app. I added a button which should act as the back button, so users can navigate back one page.
The button is initially hidden and should only show if a back history exists.
How can I realise this?
The code is simple:
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
but where do I have to call this? How is the "some site loaded" event called?
Update:
I tried to apply #Murats answer, but nothing happens:
private WebView blizzView; // my webview
private Button backButton;
public void onPageFinished(WebView view, String url)
{
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
You can use WebView#canGoBack to find out if there is a backstack. You can check for that after the page is loaded e.g. in WebViewClient#onPageFinished
It works like this:
blizzView.setWebViewClient(new WebViewClient() { //
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
});
I have an App made using Android Webview, while using Wifi connection the initial page loaded displays ok, after switching to mobile data mode, the application shows the ERR_NAME_NOT_RESOLVED message at times.
Is there a workaround to hide this message until the page is correctly displayed? or to make it work when the connection is not good in the initial load?
Thanks for the insight.
You can ask the user to retry at that point of time either displaying there or by showing a popup with a message. If you get the network is strong enough load the same page again.
You can check the url that is getting loaded in your webview by doing this.
myWebView.setWebViewClient(new WebViewClient() {
#SuppressLint("NewApi")
#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);
}
});
I am having a webview, the webview might contain a webpage with two different types of links. What I want is that, if the user clicks on a link which contains http://webpagename.com it should open the link in the phone's browser. If the user clicks on the second type of link, which does not have http:// he should be redirected to a new activity. Right now what is happening is that, the links with http:// are opening up both in webviews and browsers. But, the links not having http:// is showing Web Page not available.
The code to handle onclick link in webview:
// to know which link is clicked
holder.webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
holder.webView.setWebViewClient(new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
if (url != null && url.startsWith("http://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return null;
}
else {
Log.e("URL: ", url);
System.out.println(url.replace(url.substring(0,url.lastIndexOf("/")+1), ""));
return null;
}
}
/**
* Return WebResourceResponse with CSS markup from a String.
*/
});
return super.shouldOverrideUrlLoading(view, url);
}
});
What should I do to make sure the http:// links always opens in the browser and the other links open an activity but nothing opens in the webview?
From the docs for WebViewClient, the shouldOverrideUrlLoading() method:
Returns
True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
Since you're handling the WebView's content and the redirects to Activities, this method should explicitly return true, rather than the super method's return value.
From your description and code, it doesn't appear that you need to override the shouldInterceptRequest() method.
holder.webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url != null && url.startsWith("http://"))
{
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
else
{
Log.e("URL: ", url);
System.out.println(url.replace(url.substring(0, url.lastIndexOf("/") + 1), ""));
// Start your app's Activity here
}
return true;
}
});
I have a simple webview that loads a website. Once the website is loaded, I have the following to focus/scroll to the login box:
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now.
Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. :(
I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically.
Does anyone have any suggestions? I would greatly appreciate it! :)
EDIT:
Here's the entirety (minus the import headers) of my MainActivity.java class.
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://example.com");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
I think I might be missing a curly bracket, but ignore that for now, haha. So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.
Does that make more sense? Sorry, it's confusing me too, trying to explain it.
Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever.
I had a similar issue here:
Closing a Webview in Android
So basically you just want to execute some JavaScript once you've logged in? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js.
Something like this maybe?
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (url.contains(getString(R.string.loginURL))){
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}else if(url.contains(getString(R.string.loggedInURL))) {
wv.loadUrl(js goes here);
}
}
}
This will execute every time a page is finished loading in your WebView
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
String uid = extras.getString("uid");
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://xxx.xxx.xxx.xxx/android/login.php?uid="+uid);
}
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) {
view.loadUrl(url);
return true;
}
}
This is the code of my webview activity.
I'd like to implement a page loading function which is It will count in background when URL called.If URL doesnt load completely in 5000ms, the code below must run automatically.
I cant figure out how to figure It.
Intent i = new Intent(MainActivity.this, InternetError.class);
startActivity(i);
I've tried lot of methods but never get correct result.
Thank you.
First, please change you implementation of shouldOverrideUrlLoading so that it does not call loadUrl on the WebView, it's wrong to do that. The URL load is in progress when you get that callback, you must not start it again.
Second, to solve your issue can you post a delayed message to a Handler that will execute your error code after the desired timeout, and remove that message if WebViewClient.onPageFinished comes first?