I am creating application that use WebView to access a online website. I am stuck where I have to add code to check availability of page.
public class SpartanWeb extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
// Get Web view
mWebView = (WebView) findViewById(R.id.webView1);
WebSettings websettings = mWebView.getSettings();
websettings.setJavaScriptEnabled(true);
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("http://google.com");
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
// onProgressChanged
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// bar disappear after URL is loaded, and changes string to
// Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); // Make the bar
// disappear after URL
// is loaded
// Return the app name after finish loading
if (progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}// EOM oc
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I am trying to add onReceivedError but for some reason custom page is not loading.
/** Called when the activity is first created. */
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
mWebView.loadUrl("file:///android_asset/error.html");
}
Please advise what to do.
You can call loadErrorPage(view) function in the onReceivedError function.
The following code will load the error content you need to show.Here i am load the html file with loadDataWithBaseURL.
public void loadErrorPage(WebView webview){
if(webview!=null){
String htmlData ="<html><body><div align=\"center\" >"This is the description for the load fail : "+description+"\nThe failed url is : "+failingUrl+"\n"</div></body>";
webview.loadUrl("about:blank");
webview.loadDataWithBaseURL(null,htmlData, "text/html", "UTF-8",null);
webview.invalidate();
}
}
I added onReceivedError to mWebView.setWebViewClient(new WebViewClient so now it's working. Thanks for tips.
mWebView.setWebViewClient(new WebViewClient() {
#Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
} });
You can use the following code ..
public class TestResultWebclient extends WebViewClient {
ProgressDialog progressDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(TermsAndCondsMrupeeActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog != null)
try {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
} catch (Exception exception) {
exception.printStackTrace();
}
super.onPageFinished(view, url);
}
}
Related
I am using android in that I am calling a link via webview. Now, the problem is that whenever I click any button or call any page of that link at that time after some second progress bar coming i.e. progress bar performance is delay while load anything.
From this problem user can't know that button/link has been clicked or not, without getting any progress bar sign, and hence from that user clicked many times that button/link.
Here is my android code of MainActivity.java,
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.progressBar2);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://florro-001-site2.etempurl.com/");
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I searched many things but related topics I didn't get.
use bar.setVisibility(View.VISIBLE); in the shouldOverrideUrlLoading not in onPageStarted try to use like that
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
bar.setVisibility(View.VISIBLE);
return true;
}
}
In my application, i'm using a webview to open a url. The url is very simple, it just takes secs to load. But the progress bar doesn't hide when the url loads completely. I'm not able to figure out whats wrong. please help me. Below i'm posting the code.
WebView Class
public class noti_cse extends BaseActivity {
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noti_cse);
// overridePendingTransition(0, 0);
// webView = (WebView) findViewById(R.id.webcse);
// webView.setWebViewClient(new MyWebViewClient());
if (!DetectConnection.checkInternetConnection(this)) {
Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
} else {
webView = (WebView) findViewById(R.id.webcse);
webView.setWebViewClient(new CustomWebViewClient());
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
startWebView("http://notify.php");
}
// webView.getSettings().setJavaScriptEnabled(true);
// startWebView("http://notify.php");
// webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
// webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource(WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(noti_cse.this);
progressDialog.setMessage("On the Way!!!");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
private class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!DetectConnection.checkInternetConnection(noti_cse.this)) {
Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
} else {
view.loadUrl(url);
}
return true;
}
}
#Override
protected boolean useDrawerToggle() {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_placements)
return true;
if (item.getItemId() == android.R.id.home)
onBackPressed();
return super.onOptionsItemSelected(item);
}
You are setting 2 WebViewClient.
First:
webView.setWebViewClient(new CustomWebViewClient());
And after that, you are calling
startWebView()
which sets a new WebviewClient.
You can change your method like below:
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!DetectConnection.checkInternetConnection(noti_cse.this)) {
Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
} else {
view.loadUrl(url);
}
return true;
}
//Show loader on url load
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(noti_cse.this);
progressDialog.setMessage("On the Way!!!");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
try {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
And remove this line form onCreate() method:
webView.setWebViewClient(new CustomWebViewClient());
To create a webView recently I was using webViewClient. mWebView.setWebViewClient(new WebViewClient());
But I need to implement a progress bar. When user clicks a link, this progress bar will be visible. After page complete, progressbar will be hidden and webView will be visible. Regarding to this and this, So I added a WebChromeClient. But it loads first URL, but when I click a button inside my web page, a dialog opens and asks to open URL with which application.
I read that I should override shouldOverrideUrlLoading() method. but I get an error that "shouldOverrideUrlLoading" can't be overriden for WebChromeClient.
I would be happy if you can give an example that has progressbar and webView, also opens new URLs inside the same webView.
public class WebActivity extends Activity {
WebView mWebView;
ProgressBar mProgress;
Context mContext;
ProgressBar mProgressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
this.mContext = getApplicationContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebChromeClient(new myWebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUserAgentString("myApp");
mProgressBar = (ProgressBar) findViewById(R.id.webProgressBar);
mProgressBar.setMax(100);
}
public class myWebChromeClient extends WebChromeClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
//WebActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
}
try with this code
webView = (WebView) view.findViewById(R.id.transcationwebview);
progressdialog = ProgressDialog.show(mContext, "",
mContext.getString(R.string.please_wait));
progressdialog.setCancelable(true);
progressdialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
webView.stopLoading();
// webView.clearView();
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyChromeClient());
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if (progressdialog != null && progressdialog.isShowing()) {
progressdialog.dismiss();
} else {
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
webView.loadUrl("url");
webView.getSettings().setBuiltInZoomControls(true);
public class MyChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
try {
if (progressdialog.isShowing()) {
progressdialog.setMessage(getString(R.string.loading)
+ newProgress + " %");
} else {
/*
* webView.stopLoading(); webView.clearView();
*/
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
you can extends WebViewClient instead of WebChromeClient and override the onPageStarted to show the ProgressBar and dismiss it in onPageReceived
I made some changes in the Activity.
use WebViewClient instead of WebChromeClient
and also use ProgressDialog instead of ProgressBar
public class WebActivity extends Activity {
WebView mWebView;
Context mContext;
ProgressDialog mProgressBar;
private static final int DIALOG2_KEY = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
this.mContext = getApplicationContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new MyWebChromeClient());
mWebView.getSettings().setBuiltInZoomControls(true);
showDialog(DIALOG2_KEY); }
#Override
protected void onResume() {
super.onResume();
mWebView.loadUrl("YOUR_URL");
}
private final class MyWebChromeClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
dismissDialog(DIALOG2_KEY);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("file")) {
return false;
} else{
view.loadUrl(url);
return true;
}
}
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id)
{
case DIALOG2_KEY:
{
mProgressBar.setMessage("Loading");
mProgressBar.setIndeterminate(true);
mProgressBar.setCancelable(false);
return mProgressBar;
}
}
return null;
}
}
Hope this help you
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();
}
}
}
}
I am trying to add a progress/loading bar to my application that uses WebView. I am confused on how to implement a progress bar that appears every time a link is clicked.
Current code:
public class CULearnBrowser extends Activity {
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://culearn.colorado.edu/webct/entryPageIns.dowebct");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
I have added few lines in your code and now its working fine with progress bar.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main );
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
setTitle("Loading...");
setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
pass your url in this method
private void startWebView(String url) {
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
progressDialog = new ProgressDialog(ContestActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(ContestActivity.this, "Error:" + description, Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl(url);
}
in oncreate method where you have set your Webview.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.web_view);
web_view = (WebView) findViewById(R.id.web_view);
pd = new ProgressDialog(SiteOpenInWebView.this);
pd.setMessage("Please wait Loading...");
pd.show();
web_view.setWebViewClient(new MyWebViewClient());
web_view.loadUrl("ur site name");
}
WebViewClient
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (!pd.isShowing()) {
pd.show();
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("on finish");
if (pd.isShowing()) {
pd.dismiss();
}
}
}
Here is the code that I am using:
Inside WebViewClient:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress1).setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progress1).setVisibility(View.GONE);
}
Here is the XML :
<ProgressBar
android:id="#+id/progress1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Hope this helps..
The best approch which worked for me is
webView.setWebViewClient(new WebViewClient() {
#Override public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(ProgressBar.VISIBLE);
webView.setVisibility(View.INVISIBLE);
}
#Override public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
mProgressBar.setVisibility(ProgressBar.GONE);
webView.setVisibility(View.VISIBLE);
isWebViewLoadingFirstPage=false;
}
}
Put a progress bar and the webview inside a relativelayout and set the properties for the progress bar as follows,
Make its visibility as GONE.
CENTRE it in the Relativelayout.
and then in onPageStarted() of the webclient make the progress bar visible so that it shows the progressbar when you have clicked on a link. In onPageFinished() make the progress bar visiblility as GONE so that it disappears when the page has finished loading... This will work fine for your scenario. Hope this helps...
I try dismis progress on method onPageFinished(), but not good too much, it has time delay to render webview.
try with onPageCommitVisible() better:
val progressBar = ProgressDialog(context)
progressBar.setCancelable(false)
progressBar.show()
val url = "your url here"
web_container.settings.javaScriptEnabled = true
web_container.loadUrl(url)
web_container.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(url)
progressBar.show()
return true
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
}
override fun onPageCommitVisible(view: WebView?, url: String?) {
super.onPageCommitVisible(view, url)
progressBar.dismiss()
}
}
web_container.setOnKeyListener(View.OnKeyListener { _, keyCode, event ->
if (keyCode == KEYCODE_BACK && event.action == MotionEvent.ACTION_UP
&& web_container.canGoBack()) {
web_container.goBack()
return#OnKeyListener true
}
return#OnKeyListener false
})
You can try this code into your activity
private void startWebView(WebView webView,String url) {
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
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);
}
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(SponceredDetailsActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
Call this method using this way:
startWebView(web_view,"Your Url");
Sometimes if URL is dead it will redirected and it will come to onLoadResource() before onPageFinished method. For this reason progress bar will not dismis. To solve this issue see my this Answer.
Thanks :)