What do I need to my code to make the dialog dismiss() after the webview is loaded?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new homeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.loadUrl("http://google.com");
ProgressDialog pd = ProgressDialog.show(Home.this, "",
"Loading. Please wait...", true);
}
I've tried
public void onPageFinshed(WebView view, String url){
pd.dismiss();
}
Didn't work.
:o
webview.setWebViewClient(new homeClient()); homeClient()????
try this
...
...
...
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
webview.loadUrl("http://www.google.com");
}
Update::
this is a good example.
Android WebView and the Indeterminant Progress Solution
This is OK.
public class WordActivity extends Activity {
private WebView webview;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Bundle objetbunble = this.getIntent().getExtras();
webview = (WebView) findViewById(R.id.webview);
final Activity activity = this;
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Chargement en cours");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
});
webview.loadUrl("http://www.example.com");
}
}
#Jorgesys this is not 100% accurate. If you have several iframes in a page you will have multiple onPageFinished (and onPageStarted). And if you have several redirects it may also fail. This approach i think solves all the problems:
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
} else{
redirect = false;
}
}
});
How are you accessing pd in onPageFinshed()? (And are you sure it's actually called when the page loads?)
In your onCreate(), try passing pd to your homeClient so that homeClient can take care of dismissing the dialog.
Your homeClient should look like this:
private class homeClient extends WebViewClient {
private ProgressDialog pd;
public homeClient(ProgressDialog pd) {
this.pd = pd;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished (WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}
}
}
In your onCreate():
ProgressDialog pd = ProgressDialog.show(Fbook.this, "",
"Loading. Please wait...", true);
webview.setWebViewClient(new homeClient(pd));
If you simply want to show Progress before webPage loading in WebView. You can simply request for Window Feature Progress like
getWindow().requestFeature(Window.FEATURE_PROGRESS);
before setContentView(R.layout.blahblah);
and show it progress in onProgressChanged like
final Activity context= this;
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView webView, int progress)
{
activity.setProgress(progress * 1000);
}
});
And if you want to add you own ProgressDialog then use WebviewClient
webView.setWebViewClient(new WebViewClient() {
ProgressDialog rogressDialog ;
#Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
progressDialog = ProgressDialog.show(context, "Loading...", "Please wait...");//where context = YourActivity.this;
super.onPageStarted(view, url, bitmap);
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog .dismiss();
super.onPageFinished(view, url);
}
});
webView.loadUrl(url);
wv1.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(Entertainment.this, description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
String webUrl = wv1.getUrl();
}
});
wv1.loadUrl(url);
The above code will show a progress dialogue for every incoming link you navigate to the in webview.
Related
I'm new to Java and was trying to create a web-view app. The loading widget was created using Progress Dialog but it wouldn't stop. I don't know what is wrong. Please help.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
mywebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
mywebView.setWebViewClient(new WvClient());
}
private class WvClient extends WebViewClient
{
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
You have declared setWebViewClient twice , ie after load url you again
declared the setWebViewClient with your custom client , correct answer is
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
Place a breakpoint in
onPageFinished
and see if the listener actually get called?
Progress Dialog Does Not Dismiss After Loading Webview Is Goes On loading After Webview Is Loaded.
public class MainActivity extends AppCompatActivity {
private WebView webView;
ProgressDialog pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd = ProgressDialog.show(MainActivity.this, "Progress Dialog", "Loading...");
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://kkgupta.mmiswebtech.com");
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("file:///android_asset/error.html");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}
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;
}
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
You'll need to add onPageFinished() to your MyWebClient and in it call dismiss() on your ProgressDialog:
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
use
pd.dismiss();
where u want ti dismiss Dialog.
Note: ProgressDialog is deprecated in API level 26. Better Use ProgressBar
Delete this code:
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("file:///android_asset/error.html");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}
change:
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;
}
}
To:
class myWebClient extends WebViewClient
{
#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); if (pd.isShowing()) { pd.dismiss(); } }
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
I have a webview that opens up a webpage that contains a some links. However, if I click on each link, it will show "404 Not Found" error. I have done the shouldOverrideUrlLoading inside WebViewClient, but it results the same.
Please help. Thanks in advance.
this is how I set my webview:
WebView web_view = (WebView)view.findViewById(R.id.web_view);
WebSettings webSettings = web_view.getSettings();
web_view.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
web_view.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void shouldOverrideUrlLoading(Webview view, String url){
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
return super.shouldOverrideUrlLoading(view,url);
}
})
webSettings.setJavaScriptEnabled(true);
web_view.loadUrl("http://someurl.com");
I have also done this:
#Override
public void shouldOverrideUrlLoading(Webview view, String url){
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
but both of them result in '404 not found'.
Any help please..
Prepare the layout to show when an error occurred instead of Web Page (a dirty 'page not found message') The layout has one button, "RELOAD" with some guide messages.
If an error occurred, Remember using boolean and show the layout we prepare.
If user click "RELOAD" button, set mbErrorOccured to false. And Set mbReloadPressed to true.
if mbErrorOccured is false and mbReloadPressed is true, it means webview loaded page successfully. 'Cause if error occurred again, mbErrorOccured will be set true on onReceivedError(...)
Here is my full source. Check this out.
public class MyWebViewActivity extends ActionBarActivity implements OnClickListener {
private final String TAG = MyWebViewActivity.class.getSimpleName();
private WebView mWebView = null;
private final String URL = "http://www.google.com";
private LinearLayout mlLayoutRequestError = null;
private Handler mhErrorLayoutHide = null;
private boolean mbErrorOccured = false;
private boolean mbReloadPressed = false;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
((Button) findViewById(R.id.btnRetry)).setOnClickListener(this);
mlLayoutRequestError = (LinearLayout) findViewById(R.id.lLayoutRequestError);
mhErrorLayoutHide = getErrorLayoutHideHandler();
mWebView = (WebView) findViewById(R.id.webviewMain);
mWebView.setWebViewClient(new MyWebViewClient());
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(getChromeClient());
mWebView.loadUrl(URL);
}
#Override
public boolean onSupportNavigateUp() {
return super.onSupportNavigateUp();
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btnRetry) {
if (!mbErrorOccured) {
return;
}
mbReloadPressed = true;
mWebView.reload();
mbErrorOccured = false;
}
}
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
else {
finish();
}
super.onBackPressed();
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(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);
}
#Override
public void onPageFinished(WebView view, String url) {
if (mbErrorOccured == false && mbReloadPressed) {
hideErrorLayout();
mbReloadPressed = false;
}
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
private WebChromeClient getChromeClient() {
final ProgressDialog progressDialog = new ProgressDialog(MyWebViewActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
return new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
};
}
private void showErrorLayout() {
mlLayoutRequestError.setVisibility(View.VISIBLE);
}
private void hideErrorLayout() {
mhErrorLayoutHide.sendEmptyMessageDelayed(10000, 200);
}
private Handler getErrorLayoutHideHandler() {
return new Handler() {
#Override
public void handleMessage(Message msg) {
mlLayoutRequestError.setVisibility(View.GONE);
super.handleMessage(msg);
}
};
}
}
try this... this is the code from AS default webview activity, generated by AS.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google);
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
}
sidenote :-->> iinitInstances() is referring to nagivation drawer on the left, could be ignored.
I am trying to implement a webview loader, but once the page has loaded, the loader keeps showing:
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webcontent);
webView = (WebView) findViewById(R.id.webView);
final ProgressDialog progDailog = ProgressDialog.show(WebActivity.this, "Loading ","Please wait... ", true);
progDailog.setCancelable(false);
webView.setWebChromeClient(new WebChromeClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
//webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.loadDataWithBaseURL("www.example.com", null, "text/html", "UTF-8", null);
webView.loadUrl("www.example.com");
}
I believe "onPageFinished" should remove the loader once the page has loaded?
Thanks
Replace this:
webView.setWebChromeClient(new WebChromeClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
by this:
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progDailog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progDailog.dismiss();
}
});
It worked for me.
I'm loading data in a webview using loadDataWithBaseURL from a string. I want to implement a listener which tells me when the data is loaded completely so that i may proceed with my code.
I've searched a lot but could not find any relevant topic
if (new String(rowData.getResource().getData()) != null) webView.loadDataWithBaseURL("", new String(rowData.getResource().getData()), "text/html", "UTF-8", "about:blank");
Use WebViewClient and implement onPageStarted,onPageFinished to know the status of the webview.
ProgressDialog progressDialog = new ProgressDialog.show(this);
progressDialog.setMessage("Loading WebPage...");
class CustomWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
webview.setWebViewClient(new CustomWebViewClient());
Try this Tested working demo.
public class URLActivity extends Activity {
private ProgressDialog progress;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webView = (WebView) findViewById(R.id.webView);
webView.setInitialScale(0);
webView.setWebViewClient(new WebViewClient());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
webView.loadDataWithBaseURL("", new String(rowData.getResource().getData()), "text/html", "UTF-8", "about:blank");
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress > 0) {
showProgressDialog("Please Wait");
}
if (newProgress >= 100) {
hideProgressDialog();
// data load successfully. you can go further.
}
}
});
}
public void showProgressDialog(final String msg) {
runOnUiThread(new Runnable() {
public void run() {
if (progress == null || !progress.isShowing()) {
progress = ProgressDialog.show(URLActivity.this, "", msg);
}
}
});
}
public void hideProgressDialog() {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
if (progress.isShowing())
progress.dismiss();
} catch (Throwable e) {
}
}
});
}
}