Android WebView TimeOut - android

Is there a way to set the timeout value in WebView?
I want the WebView to be timeouted if the url is too slow to response.

You can do it by setting up a Timer which checks for progress of current page by calling getProgress() and if it is less than some threshold after some specified time then you can dismiss the loading of the current page.

We can use onLoadResource method of WebViewClient instead of Timer. Like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WEBCLIENT", "onPageFinished");
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
Log.d("WEBCLIENT","onLoadResource");
if(webView.getProgress() == 100) {
progressDialog.dismiss();
}
}
}

I use
#Override
public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.alert_dialog);
Button btTryAgain = dialog.findViewById(R.id.bt_try_again);
btTryAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
recreate();
}
});
dialog.show();
//Toast with error conection
Toast.makeText(getApplicationContext(), "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
}
Where -alert_dialog- is a layout with a button to retry

Related

Get URL, perform action on URL change and get URL to ParentActivity

I'm getting the URL and finishing the activity when the URL is stackoverflow.com. How do I pass this URL to Parent Activity?
wvPayment.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "shouldOverrideUrlLoading: "+String.valueOf(request.getUrl()));
if(String.valueOf(request.getUrl()).equals("http://stackoverflow.com/")){
finish();
Toast.makeText(PaymentActivity.this ,"Successful",Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
wvPayment.setVisibility(View.GONE);
ProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wvPayment.setVisibility(View.VISIBLE);
ProgressBar.setVisibility(View.GONE);
}
});

xWalkWebView while loading page?

I am working with xWalkWebView
and I would like to know the equivalent to this code
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// progressBar.setVisibility(View.GONE);
}
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
}
});
Thanks using Crosswalk, you can also ask question by sending email to
crosswalk-help#lists.crosswalk-project.org
For this question:
mXWalkView.setUIClient(new XWalkUIClient(mXWalkView) {
#Override
public void onPageLoadStarted(XWalkView view, String url) {
System.out.println("onPageLoadStarted " +url);
}
#Override
public void onPageLoadStopped(XWalkView view, String url,
LoadStatus status) {
System.out.println("onPageLoadStopped " + status);
}
});
mXWalkView.setResourceClient(new XWalkResourceClient(mXWalkView) {
#Override
public void onReceivedHttpAuthRequest(XWalkView view,
XWalkHttpAuthHandler handler, String host, String realm) {
System.out.println("onReceivedHttpAuthRequest");
}
});

Clicklistner in webview

The question can be a little vague, I have created a webview and showing the webpage of the URL which I get from some server. Now When i navigate to different pages , On one page there is only a button which will send me a different URL , I need that when user is on that page and click on that button the webview should hide and user continue other operation. I have read that addJavascriptInterface is needed to do this , can anyone tell me if this is possible and how to achieve this , any good tutorial will work.
I also tried this but of no use
wv.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
HitTestResult result = wv.getHitTestResult();
if (result.getType() == HitTestResult.UNKNOWN_TYPE) {
Message msg = mHandler.obtainMessage();
wv.requestFocusNodeHref(msg);
}
return false;
}
});
topupWV.setWebViewClient(new WebViewClient());
topupWV.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("XXXXX")) {
try {
URL urls = new URL(url);
query = urls.getQuery();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}
you can do something like this for detecting click within a webview
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isNetworkConnected()) {
//DO YOU OPERATION HERE
}
return true;
}
});

Progress Dialog In Webview

Simply put how can I make a progress dialog show up every time a new link is clicked in my webview. I have tried many tutorials and methods but every one of them only shows the dialog once, when the app is initially loaded.
Here is my code
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(myActivity.this, "",
"Loading. Please wait...", true);
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
check out this:
wvCouponsAndOffers.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(CouponsWebViewUI.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
progressDialog.setCancelable(false);
progressDialog.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
else
return false;
}});
progressDialog.show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
progressDialog.setCancelable(false);
progressDialog.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
else
return false;
}});
progressDialog.show();
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
Try this,
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("http://slashdot.org/");
And if you want to make your own progressDialog then
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// Start PROGRESS DIALOG
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//HIDE PROGRESS DIALOG LOADING IT HAS FINISHED
}
});
And let me know what happen..
Try this easiest one,it work for me
public class HomeWebViewActivity extends AppCompatActivity{
private String weburl;
private WebView webview;
private Toolbar toolbar;
private ProgressDialog prDialog;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_webview_activity);
toolbar = (Toolbar) findViewById(R.id.toolbar_homewebview);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
weburl="ADD YOUR URL HERE";
webview = (WebView) findViewById(R.id.webviewinfo);
webview.setWebViewClient(new WebVwClientcls());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.loadUrl(weburl);
}
private class WebVwClientcls extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = new ProgressDialog(HomeWebViewActivity.this);
prDialog.setMessage("Please wait ...");
prDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(prDialog!=null){
prDialog.dismiss();
}
}
}
}

Android - tracking browser activity

I'm looking to develop an application to track how long a site takes to load on the default android browser. Does the browser have any method of tracking when the site is fully loaded?
on page started and on page finished are the methods to
get the the start time and end time of loading of the webview
WebView search_webview;
search_webview = (WebView) findViewById(R.id.show_webview);
search_webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
_dialog.dismiss();
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try{
_dialog.dismiss();
Toast.makeText(ShowWebView.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
}catch (Exception e) {
// TODO: handle exception
}
}
});

Categories

Resources