Dismissing ProgressDialog after WebPage finishes loading - android

Iam loading a url into a WebView. This is a login page. When the user is authenticated he is shown a page. I stop the loading at this point and start another activity.
When the login button is hit, the user is redirected to first url. I start a progress dialog at this point. After he reaches the authenticated page I want to stop the progress dialog and directly want to show the new activity. I dont want the user to see the authenticated page.
Right now the Progress dialog stops at the authenticated page. The user sees it and then he is shown the next activity
Any help on how this can be achieved?
Here is my code:-
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
final ProgressDialog progressBar = ProgressDialog.show(Details1.this, "Loading..", "Please wait!");
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
url= view.getUrl();
redirected= Uri.decode(url);
if(redirected!=null &&redirected.contains("Visited"))
{
myprogressDialog= new Utility.ProgressDialogFragment().newInstance();
myprogressDialog.show(getFragmentManager(), "Wait");
}
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
url= view.getUrl();
redirected= Uri.decode(url);
if(progressBar!=null && progressBar.isShowing())
{
progressBar.dismiss();
}
if(redirected!=null && redirected.startsWith(endpointHost+"/Authorize/index/"+device))//&& myprogressDialog!=null && myprogressDialog.isVisible())
{
myWebView.stopLoading();
myprogressDialog.dismiss();
String authorizationContentString = myurl;
authorizationContentString =authorizationContentString.replace("|", "%7C");
new AuthenticateDevice(Details1.this, Url).execute(authorizationContentString);
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
}
private static class ProgressDialogFragment extends DialogFragment
{
public static ProgressDialogFragment newInstance()
{
return new ProgressDialogFragment();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_dialog_progress, container, false);
}
}
}

myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
final ProgressDialog progressBar = ProgressDialog.show(Details1.this, "Loading..", "Please wait!");
myWebView.setWebViewClient(new WebViewClient()
{
boolean toStopLoading = false;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
url= view.getUrl();
redirected= Uri.decode(url);
if(redirected!=null &&redirected.contains("Visited"))
{
myprogressDialog= new Utility.ProgressDialogFragment().newInstance();
myprogressDialog.show(getFragmentManager(), "Wait");
toStopLoading = true;
// start your new activity here.
}
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
url= view.getUrl();
redirected= Uri.decode(url);
if(progressBar!=null && progressBar.isShowing())
{
progressBar.dismiss();
}
if(toStopLoading){
// finish your web view activity here.
}
if(redirected!=null && redirected.startsWith(endpointHost+"/Authorize/index/"+device))//&& myprogressDialog!=null && myprogressDialog.isVisible())
{
myWebView.stopLoading();
myprogressDialog.dismiss();
String authorizationContentString = myurl;
authorizationContentString =authorizationContentString.replace("|", "%7C");
new AuthenticateDevice(Details1.this, Url).execute(authorizationContentString);
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
}
private static class ProgressDialogFragment extends DialogFragment
{
public static ProgressDialogFragment newInstance()
{
return new ProgressDialogFragment();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_dialog_progress, container, false);
}
}
}
Try this..

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.

ProgressDialog is not dismiss

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.

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

Categories

Resources