I have used progress bar to indicate page is loading in android webview. But the Progress Bar is showing for ever even after the page has been loaded completely. I used logcat for debugging which shows onPageFinished function is called.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
_dialog =ProgressDialog.show(UmlScreen.this, "", "Please wait...");
super.onPageStarted(view, url, favicon);
_dialog.setCanceledOnTouchOutside(true);
Log.d("LOGCAT", "started");
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if (_dialog != null || _dialog.isShowing()) {
_dialog.dismiss();
Log.d("LOGCAT", "finished");
}
}
Try initialization of _dialog outside WebViewClient such as in OnCreateView method
Because the page is not finished. May be the page have a lot of images so it is taking time to download them.
you can check it using break point. debug you code and put break point at super.onPageFinished(view, url);
I can give you some dummy codes.
webview.load(url);
dialog.show();
public void onPageFinished(WebView view, String url) {
//....
dialog.dismiss();
}
#Override
public void onPageFinished(WebView view, String url) {
try{
if (prDialog.isShowing()|| prDialog!= null) {
prDialog.dismiss();
prDialog= null; /*** Add ***/
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
Or visit...
https://stackoverflow.com/a/31981161/3857542
Related
I´ve searched in all topics but i've not found anything that works for me.
I have an android application with a webview. I set up the page with a small javascript code in OnPageFinished and i see how it hides and shows elements, but i don´t want to see that. I would want that webview shows the current page while it is loading and when it's loaded fully, webview shows the new page.
Is there any way to get it?? Sorry for my english. Thanks.
Show progress bar or progress dialog ,initialize a progress bar on your xml page and use it to load untill the your webpage has fullyloaded
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setDomStorageEnabled(true);
webView.post(new Runnable() {
#Override
public void run() {
webView.loadUrl("file:///android_asset/Guage.html");
}
});
webView.setWebViewClient(new myWebClient());
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
I am using webview to show a webpage in my app. The webpage takes about 2-3 seconds to load.
Till the webpage loads, I want to show an activity circle progress bar like the one shown here. This is what an activity circle looks like :
How can I accomplish it?
I searched the web but couldn't find a satisfactory and proper explanation of how it is implemented. Thanks!
WebViewPage.java
public class Webpage extends Activity {
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_page);
Intent startwebpage = getIntent();
web = (WebView) findViewById(R.id.webView);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
Display progressBar in onPageStarted method and dismiss it in onPageFinished.
private ProgressDialog progressBar;
Show progressBar in onPageStarted:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.show();
}
2.Dismiss it in onPageFinished:
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.dismiss();
}
EDIT : Use ProgressBar instead of ProgressDialog.
Use Following code in your layout.
Use drawable of your choice for "#drawable/progress"
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:indeterminateDrawable="#drawable/progress" >
</ProgressBar>
Inflate it in your activity. Make it visible in onPageStarted and gone in onPageFinished.
private ProgressBar prgrsBar;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
prgrsBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
prgrsBar.setVisibility(View.GONE);
}
This works for me:
ProgressDialog pb = null;
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
// prepare for a progress bar dialog
if (pb == null) {
pb = new ProgressDialog(Quiz.this);
pb.setCancelable(true);
pb.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pb.show();
}
}
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
pb.dismiss();
}
In my app I'm loading a local HTML file to a webview. If some button was clicked, I want to load a local HTML file and scroll it with specific value in y axis.
The problem is that the command:
webview.scrollTo(0, scrollY);
is executed too early, before the HTML file finish loaded, so the scrolling is not happen.
I tried to use a lot of answers from Stackoverflow (like this, and this) but the problem is always the same. I can see in debug mode that the scrollTo command executed before the file is visible.
Anyone have any idea how can I achieved this scrolling?
After trying all the answers I found that only the following really help:
private final Handler mHandler = new Handler();
// in onCreate
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (mWebView.getContentHeight() > 0) {
mWebView.scrollTo(0, mLastPosition);
Log.d("scrolling", "true");
mHandler.removeCallbacks(this);
} else {
mHandler.postDelayed(this, 100);
}
}
}, 100);
This solution was taken from hereenter link description here
WebViewClient will help you. Extend onPageFinished() as follows:
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webview.scrollTo(0, scrollY);
}
});
fisrt set custom webview client like this
webview.setWebViewClient(new CustomWebViewClient());
then add following piece of code in class
private class CustomWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
webview.scrollTo(0, scrollY);
}
}
I am new to android.I am using progress dialog in my app but it could not stop.In my app i am using webview.In first activity I have connect button.In second activity connect to facebook code is available.In second activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
wvFacebook.setWebViewClient(new MyWebViewClient());
wvFacebook.loadUrl(strFacebook);
}
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = ProgressDialog.show(getParent(), "In progress", "Loading, please wait...");
}
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
prDialog.dismiss();
Log.e("status","calling next......");
}
}
when connect button clicked the second activity will be executed and getting facebook page.But progress dialog could not stop.
Try like this
First create dialog in your onCreate()
ProgressDialog progressDialog =new ProgressDialog(getApplicationContext());
progressDialog.setMessage("Please Wait");
Then show the dialog in onPageStarted() method.
progressDialog .show();
Then dismiss it in your onPageFinished() method
progressDialog.cancel();
You should not create Dialog in onPageStart, when loading a web page, onPageStart() could be called multi times.
In you onPageStart, you create a new Dialog without closing the old one, so the old dialog will always show.
To solve this you have many choices:
As lmran said, create the dialog only once in onCreate(). Show it in onPageStarted and dismiss in onPageFinished.
Before create a new Dialog, dismiss the old one.
just this code used and try it out,
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
if (prDialog != null || prDialog.isShowing())
prDialog.dismiss();
Log.e("status","calling next......");
}
#Override
public void onPageFinished(WebView view, String url) {
try{
if (prDialog.isShowing()|| prDialog!= null) {
prDialog.dismiss();
prDialog= null; /*** Add ***/
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
You may also visit the following link to get a complete detail...
http://androiddubd.blogspot.com/2014/07/how-to-show-progress-dialog-or-loader.html
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