I created a basic webview app...
It has a Progress Bar which hides after the page has loaded completely,
On top of it I created a FAB without library i.e. by using ImageButton...
I want to Show the FAB only after page load & not everytime (e.g Like when Offline)
how to achieve that?
here's the Code pic...
Implement WebViewClient and extend onPageFinished() as follows,
mWebView.setWebViewClient(new WebViewClient() {public void onPageFinished(WebView view, String url) {
// do your stuff here
fab.setVisibility(View.VISIBLE);
}});
here is my solution to hide the progress dialog when page loaded.
webView.setWebViewClient(new WebViewClient() {
private int webViewPreviousState;
private final int PAGE_STARTED = 0x1;
private final int PAGE_REDIRECTED = 0x2;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewPreviousState = PAGE_STARTED;
if (dialog == null || !dialog.isShowing())
dialog = ProgressDialog.show(MainActivity.this, "", getString(R.string.dialog_loading), true, true,
new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// relax
}
});
}
#Override
public void onPageFinished(WebView view, String url) {
if (webViewPreviousState == PAGE_STARTED) {
dialog.dismiss();
dialog = null;
}
}
});
}
Related
I want to hide the progress bar on webview as and when webview started rendering the content. I tried webview.getProgress() to get the current progress of webview. It returns progress in the range between 0-100. But I couldn't conclude that at which range actually webview starts render the content.
I would like to know that, Is there any method to identify the start of rendering the content in webview android.
Your help is much appreciated.
Thank you
You can try to add WebViewClient() and in callback methods hide progress.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
});
Found the solution after a lot of searching! There is a "onPageCommitVisible" method in webViewClient which does exactly this, displays the progressbar until the page renders and the disappears.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
mProgressBar.setVisibility(View.INVISIBLE);
}
});
Hope it helps :D
final ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait....");
pDialog.setCancelable(false);
pDialog.show();
String url = "url";
final Activity activity = (Activity) context;
pdfView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setProgress(progress * 1000);
pDialog.incrementProgressBy(progress);
if(progress == 100 && pDialog.isShowing())
pDialog.dismiss();
}
});
Dialog will dismiss when webview start rendering the content.
class MyWebViewClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
class MYWebViewChromeClient extends WebChromeClient{
#Override
public void onProgressChanged(WebView view, int newProgress) {
//When the schedule is equal to 100 of the time, that is, the load is completed, you can hide the progress bar
super.onProgressChanged(view, newProgress);
}
}
I want to make a PDF viewer using a WebView and Google Docs url.
However, for some reason it isn't showed properly when it reaches 2nd page and successive, except on Android 5.
Here is an image from Android 4.1.1:
http://oi57.tinypic.com/6rrbpy.jpg
And here other from Android 5.0.0:
http://oi62.tinypic.com/2vdlefm.jpg
Any idea why this happens?
Here is my code
public class PDFViewer extends Activity {
public void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
this.setContentView(R.layout.web_viewer);
WebView myWebView = (WebView) this.findViewById(R.id.webViewSimple);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
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);
if (progressDialog == null) {
progressDialog = new ProgressDialog(PDFViewer.this);
progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
});
String link=getIntent().getExtras().getString("link");
myWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+link);
}
I want to have a busy indicator of some sort when pages are loading. My indicator will be full-screen like a splash screen. This is because the android webview blanks the page to white before it starts loading, unlike a normal browser.
A simple solution might be suggested. for example, set loading true in onPageStarted() and set false in onPageFinished().
#Override
public void onPageFinished(WebView view, String url) {
setLoading(false);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setLoading(true);
}
the problem with this is that the website i'm loading results in multiple start / finished cycles for a single click. maybe this is redirects, or iframes. i'm not sure ... but the result is that the "busy" indicator flashes on and off disturbingly.
Considering that, another ideas is to keep a count of pages loading- incrementing in onPageStarted() and decrementing in onPageFinished(). something like this,
private int loadingCount = 0;
#Override
public void onPageFinished(WebView view, String url) {
loadingCount--;
if (loadingCount == 0) {
setLoading(false);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (loadingCount == 0) {
setLoading(true);
}
loadingCount++;
}
I'm running into this problem: page started / page finished are not matched. I'm not sure why exactly, but I think it might have to do w/ redirects.
Any ideas?
Try this:
private boolean loadingFinished = true, redirect = false;
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.setVisibility(View.VISIBLE);
view.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setTitle(url);
loadingFinished = false;
progressDialog = new ProgressDialog(FacebookWebView.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(" Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
if (progressDialog.isShowing()){
progressDialog.dismiss();
}
//finish();
} else {
redirect = false;
}
}
I'm not exactly sure, but this should solve your problem with redirects. In the case of several frames on the same page, probably counting in onPageFinished till the number of frames, and then dismiss "busy dialog", should be a solution.
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();
}
}
}
}
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