Dismiss ProgressDialog after WebView has shown - android

How can I use ProgressDialog to wait while WebView is loading?
Sometimes myText has a huge text and some old devices can't load WebView fast. I want to show ProgressDialog while WebView is loading and when it's already shown I want to dismiss the ProgressDialog.
I have:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
String myText = "some text";
WebView myWebView = (WebView)findViewById(R.id.webView);
myWebView.loadDataWithBaseURL("", myText, "text/html", "UTF-8", "");
dialog.dismiss();
It doesn't show any ProgressDialog.
If I don't use dismiss, I always see my ProgressDialog.

Use AsyncTask.
// ..onCreate code come here...
Button button = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
new LongOperation().execute(); // here calling LongOperation class to do `AsyncTask`.
}
});
private class LongOperation extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.class);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
}
#Override
protected String doInBackground(Void... params) {
// background operation comes here..
}
#Override
protected void onPostExecute(String result) {
// close progressDialog here by calling
dialog.dismiss();
}
}

Put this code in your current Java class which contains web view..
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(dialog.isShowing() && dialog != null) {
dialog.dismiss();
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!dialog.isShowing() && dialog != null) {
dialog.show();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
And set this
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
below you initialized your webview

just try this,
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url)
{
progDailog.dismiss();
}
});

Related

Webview not loading url on Nexus Devices

Earlier webbview works fine but today strange problem came that showing this when I load url..
Code for this is....
public class ShowSubjectListItemsInWebView extends ActionBarActivity {
private WebView webView;
private Toolbar mToolbar;
private SessionManager sessionManager;
private Security security;
private ProgressDialog dialog;
private String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_subject_list_items_in_web_view);
this.initViews();
this.setToolBar();
try{
sessionManager = new SessionManager(getApplicationContext());
security = new Security(getApplicationContext());
boolean isInternetPresent = security.isConnectingToInternet();
if (isInternetPresent) {
//I hide url due to some privacy contents
loadWebView(url);
} else {
showCustomToast("Internet not available \n Please check your internet connection");
}
}catch(Exception ex){
showCustomToast("Something went wrong");
}
}
//initializes all views
private void initViews() {
webView = (WebView) findViewById(R.id.filesWebView);
}
//show Url in WebView
public class myWebClient extends WebViewClient {
#JavascriptInterface
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
dialog = new ProgressDialog(ShowSubjectListItemsInWebView.this);
dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
dialog.setCancelable(false);
dialog.setMessage(Constant.KEY_PLEASE_WAIT);
dialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
public void loadWebView(String decryptedUrl) {
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setVisibility(View.VISIBLE);
webView.loadUrl(decryptedUrl);
}
}
I tested this code on lenovo, moto and MI devices...works fine but when I choose nexus device for loading url it gives the above stated error.

Progress Dialog not showing in webview

I have a webview in my android application which renders html pages from a local folder, Now I want to show progress dialog when navigating from one html page to another but the progress dialog which I am using isn't showing up. Here is my code snippet:
#JavascriptInterface
public void save(String respString, boolean ifEndNode) throws JSONException {
ProgressDialog progDialog = null;
try {
if(ifEndNode){
//start loader
progDialog = new ProgressDialog(webView.getContext());
progDialog.setMessage("Saving survey, Please DON'T close the Application!! ");
progDialog.setCanceledOnTouchOutside(false);
progDialog.setCancelable(false);
progDialog.show();
}
//some code here
//...
}
catch (Exception e)
{
Logger.e(context,"exception", "jsonObjectexception");
}
finally {
if(progDialog != null && progDialog.isShowing())
progDialog.dismiss();
}
Can anyone suggest what the problem could be?
You can use WebView with ProgressDialog this way. This is nice and simple approch.
private WebView webView;
ProgressDialog prDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_news);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
webView = (WebView) findViewById(R.id.wv_news);
webView.setWebViewClient(new MyWebViewClient());
String url = "http://google.com/";
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.loadUrl(url);
}
private class MyWebViewClient 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(NewsActivity.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 - WebView OnPageStarted, OnPageFinished & shouldOverrideUrlLoading not getting called

I am developing an app in that when a user navigates from one link to another link I want to change the content of the edittext and want to show current url in the edittext.
But these 3 methods are not working, not even printing the logs. Here is the code.
wv.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url_new = view.getUrl();
Log.v("","Webview URL: "+url);
addressbar.setText(url_new);
return false;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
String url_new = view.getUrl();
Log.v("","Webview Function URL: "+url_new);
addressbar.setText(url_new);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
String url_new = view.getUrl();
Log.v("","Webview Function URL: "+url_new);
addressbar.setText(url_new);
}
});
wv is the WebView and addressbar is the EditText in above code.
What I am missing??
Please help..!!
EDIT : Whole Code..
public class Clip_From_Web_Home extends Activity
{
WebView wv;
TextView back, clip;
ProgressDialog dialog;
Button previous, reload, next, go;
LinearLayout ll;
public static final int REQUEST_CODE_CROP_IMAGE = 0x1;
Bitmap drawingCache;
public File mFileTemp;
public static final String TEMP_PHOTO_FILE_NAME = "temp_photo_web_clip.png";
FileOutputStream out;
EditText addressbar;
String url;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.clip_from_web);
wv = (WebView) findViewById(R.id.clip_from_web_webView1);
back = (TextView) findViewById(R.id.clip_from_web_textview_back);
back.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
finish();
}
});
url = "http://www.google.com";
addressbar = (EditText) findViewById(R.id.clip_web_address_bar);
addressbar.setText(url);
//wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url_new = view.getUrl();
Log.v("","Webview URL: "+url);
addressbar.setText(url_new);
return false;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
String url_new = view.getUrl();
Log.v("","Webview Function URL: "+url_new);
addressbar.setText(url_new);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
String url_new = view.getUrl();
Log.v("","Webview Function URL: "+url_new);
addressbar.setText(url_new);
}
});
go = (Button) findViewById(R.id.clip_web_go);
go.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
url = addressbar.getText().toString();
Log.v("","URL ENTERED: "+url);
if(url.equals(""))
{
url = "http://www.google.com";
dialog = ProgressDialog.show(Clip_From_Web_Home.this,"","Loading",true,false);
new webview_load().execute();
}
else
{
dialog = ProgressDialog.show(Clip_From_Web_Home.this,"","Loading",true,false);
new webview_load().execute();
}
}
});
dialog = ProgressDialog.show(Clip_From_Web_Home.this,"","Loading",true,false);
new webview_load().execute();
}
class webview_load extends AsyncTask<Void, Void, Void>
{
#Override
protected Void doInBackground(Void... arg0)
{
// TODO Auto-generated method stub
return null;
}
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onPostExecute(Void result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
wv.setBackgroundColor(0);
wv.setBackgroundResource(android.R.color.black);
wv.setWebChromeClient(new WebChromeClient());
wv.setWebViewClient(new WebViewClient());
WebSettings settings = wv.getSettings();
settings.setBuiltInZoomControls(true);
//settings.setDefaultZoom(ZoomDensity.MEDIUM);
settings.setDefaultZoom(ZoomDensity.FAR);
wv.setInitialScale(125);
// settings.setUseWideViewPort(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(false);
wv.loadUrl(url);
int secondsDelayed = 5;
new Handler().postDelayed(new Runnable()
{
public void run()
{
Log.v("","In Handler");
dialog.dismiss();
}
}, secondsDelayed * 1000);
}
}
}
You're overwriting your WebViewClient in your AsyncTask object:
wv.setWebViewClient(new WebViewClient());
This overwrites your custom WebViewClient you set in Activity.onCreate().
You can probably get rid of the whole AsyncTask implementation, too. You can call loadUrl in a WebView in the UI thread with no problems. This might simplify things for you greatly.

Android progressbar inside WebView

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

How to show a progress dialog while HTML page is loaded in WebView

I am using the web view for showing html pages, and I want to show a progress dialog until the page is loaded. When that is done, the dialog has to disappear. I have used AsyncTask for this, but the dialog doesn't show. See my code below:
class DownloadAysnc extends AsyncTask<String, String, Void>
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(OverView.this, "", "Please Wait ...");
}
#Override
protected Void doInBackground(String... arg0) {
webView.loadUrl("http://marico.com/html/investor/overview.php");
return null;
}
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
progressDialog.dismiss();
}
}
And if I take the help of google docs to show the web page, then the HTML Tag is shown, but not the page. Below is that code:
String url = "http://google.co.in/";
String googleDocsUrl = "http://docs.google.com/viewer?url="+url;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(googleDocsUrl ), "text/html");
startActivity(intent);
this.myWebView.loadUrl(googleDocsUrl);
Can someone help me with this?
Use this code:
webView.setWebViewClient(new WebViewClient() {
ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
prDialog = ProgressDialog.show(Activity.this, null, "loading, please wait...");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
prDialog.dismiss();
super.onPageFinished(view, url);
}
});
webView.loadUrl(url);
v.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
WebView v=(WebView)findViewById(R.id.wv);
//Toast.makeText(mContext, v.getUrl() + newProgress +"uploading...", Toast.LENGTH_SHORT).show();
ProgressBar s=(ProgressBar)findViewById(R.id.progressBar1);
s.setMax(100);
s.setProgress(newProgress);
if(newProgress==100){
v.setVisibility(0);
//Toast.makeText(mContext, "upload finished...", Toast.LENGTH_SHORT).show();
}else{
v.setVisibility(8);
//Toast.makeText(mContext, "uploading...", Toast.LENGTH_SHORT).show();
}
}
});
You can show the Progress of the WebView in the Title Bar of the WebView. Here is a complete example for the same that show Progress Status in the Title Bar of the WebView.
You can try following code,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}

Categories

Resources