ProgressDialog is not dismiss - android

I am making a web view and trying to show loading message using progress Dialog.progress dialog is showing but not dismissed after all the content of URL is loaded.Please Help me.
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
prDialog = ProgressDialog.show(Web.this, null, "loading, please wait...");
super.onPageStarted(mWebview, url, favicon);
Toast.makeText(getApplicationContext(), "started!"+1, Toast.LENGTH_SHORT).show();
num++;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(mWebview, url);
prDialog.dismiss();
Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.google.com");
setContentView(mWebview);

package com.example.webviewtag;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
WebClientClass webViewClient = new WebClientClass();
webView.setWebViewClient(webViewClient);
WebChromeClient webChromeClient=new WebChromeClient();
webView.setWebChromeClient(webChromeClient);
setContentView(webView);
}
public class WebClientClass extends WebViewClient {
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd = new ProgressDialog(WebViewDemo.this);
pd.setTitle("Please wait");
pd.setMessage("Page is loading..");
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
}
public class WebChromeClass extends WebChromeClient{
}
}

Try this code,
private class MYWEBCLIENT extends WebViewClient {
private ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = ProgressDialog.show(activity, "", "Please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (prDialog != null && prDialog.isShowing())
prDialog.dismiss();
}
}
Load webview code,
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("YOUR_URL_OR_HTML_FILE", "text/html", "UTF-8");

You can try this java code:
Just need to replace my entities with yours. Such as class name or other variables or stuff.
public class WebViewDemo extends Activity
{
WebView mWebview;
ProgressDialog prDialog;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebview = (WebView) findViewById(R.id.your_id);
mWebview.getSettings().setJavaScriptEnabled(true);
CustomWebClient webClient =new CustomWebClient(WebViewDemo.this);
mWebview.setWebViewClient(webClient);
mWebview .loadUrl("http://www.google.com");
}
// This is your custom webviewclient
public class CustomWebClient extends WebViewClient
{
public Context context;
public CustomWebClient(Context context)
{
// TODO Auto-generated constructor stub
this.context = context;
prDialog = new ProgressDialog(context);
prDialog.setMessage("loading please wait...");
prDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
prDialog.show();
}
#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);
try
{
if ((prDialog != null) && prDialog.isShowing())
{
prDialog.dismiss();
}
} catch (final IllegalArgumentException ae) {
} catch (final Exception excep) {
} finally {
prDialog = null;
}
}
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm)
{
// TODO Auto-generated method stub
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
{
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
}
You can run my java class to test, is it working or not at your end.
Hope this helps you.

Related

Android webView cannot handle pageFinish

If i return true on shouldOverrideUrlLoading, web view cannot handle onPageFinish when load new url.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.abc.com")) {
Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
Disable progress on page finish.
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
try this code
webview.setWebViewClient(new myWebClient());
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progressInt)
{
if (progressInt < 80 && progress.getVisibility() == ProgressBar.GONE)
{
progress.setVisibility(ProgressBar.VISIBLE);
}
if (progressInt >= 80)
{
progress.setVisibility(ProgressBar.GONE);
}
}
});
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
progress.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);
progress.setVisibility(View.GONE);
}
}

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.

How to load HTML file in webview on onitemclick

i have more than 100 html files and i want each file to open on row click in listview and each html file should open in the webview , i tried this code but this is not working, only the webview opens on row click , the html file is not showing up.
package com.Example.apk;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebViewClient;
public class WebView extends Activity {
public class WebViewActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
web = (WebView) findViewById(R.id.webview1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
web.loadUrl("file:///android_asset/work.html");
}
else if(pos==1)
{
web.loadUrl("file:///android_asset/work1.html");
}
else if(pos==2)
{
web.loadUrl("file:///android_asset/work2.html");
}
else if(pos==3)
{
web.loadUrl("file:///android_asset/work3.html");
}
// similarly for 4 and 5 and so on.
}
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 onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
}
Pass the position to next Activity
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(MainActivtiy.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);
}
});
Then in WebViewActivity ie activity_webview)
public class WebViewActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
web = (WebView) findViewById(R.id.webview1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
web.loadUrl("file:///android_asset/work.html");
}
else if(pos==1)
{
web.loadUrl("file:///android_asset/work1.html");
}
else if(pos==2)
{
web.loadUrl("file:///android_asset/work2.html");
}
else if(pos==3)
{
web.loadUrl("file:///android_asset/work3.html");
}
...// similarly for 4 and 5 and so on.
}
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 onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
xml for WebViewActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Make an entry for WebViewActivity in Manifest file.
For your purpose use this
WebView wv = (WebView)rootView.findViewById(R.id.go_web_view);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(null, description, "text/html", "utf-8", null);
Here "description" is name of string containing data with or without html tags
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String url = m_ArrayList.get(arg2).your_url;
Intent myIntent = new Intent(MainActivtiy.this,AppWebView.class);
myIntent.putExtra("key",url);
startActivity(myIntent);
}
});
If you need to render url then try
public class AppWebView extends Activity{
WebView webView;
ProgressBar pBar;
#SuppressLint("SetJavaScriptEnabled")
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
pBar = (ProgressBar)findViewById(R.id.progressBar1);
String newUrl;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
newUrl = null;
} else {
newUrl = extras.getString("url");
}
} else {
newUrl = (String) savedInstanceState
.getSerializable("myJsonStringS");
}
Log.d("jitendra", newUrl);
//SharedPreferences sp = getSharedPreferences("booking_detail", 0);
//String jsonString = sp.getString("jsonString", "");
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl(newUrl);
}
public void moveToThanksPage()
{
Intent intent = new Intent(this,ThankYou.class);
startActivity(intent);
}
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
Log.d("sagarWeb", url);
if (url.startsWith("mailto:")) {
String[] blah_email = url.split(":");
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
// emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "what_ever_you_want_the_subject_to)");
Log.d("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + "what_ever_you_want_the_subject_to_be");
startActivity(emailIntent);
}
else if (url.startsWith("tel:")) {
Log.d("Web", "tell");
String uri = url;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
else if (url.endsWith("error.jsp")) {
Log.d("Web", "Error");
}
/*else if (url.contains("thankyou/app")) {
//===================== USE UNDERMENTIONED COMMENT ON FOR SELF THANKS PAGE ==================//
//moveToThanksPage();
}*/
else
{
view.loadUrl(url);
pBar.setVisibility(View.VISIBLE);
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
pBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
//progressBar.setVisibility(View.GONE);
}
}
}

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

Progress Dialog In Webview

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();
}
}
}
}

Categories

Resources