Dismiss ProgressDialog after WebView loads url does not work - android

I created a webview and while the page is loading, there will be a ProgressDialog. Well, until this point all works.
But now I want that the ProgressDialog is dismiss after loading the url.
This doesn't work.
My Code:
private WebView webViewNews;
private ProgressDialog progressDialogWebViewNews;
webViewNews = (WebView) findViewById(R.id.webViewNews);
WebSettings webSettings = webViewNews.getSettings();
webSettings.setJavaScriptEnabled(true);
webViewNews.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialogWebViewNews = new ProgressDialog(MainActivity.this);
progressDialogWebViewNews.setCancelable(false);
progressDialogWebViewNews.setTitle("Demo...");
progressDialogWebViewNews.setMessage("Demo");
progressDialogWebViewNews.show();
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialogWebViewNews.dismiss();
super.onPageFinished(view, url);
}
});
webViewNews.loadUrl("http://www.demo.com");

When you pass a variable to an anonymous class, the variable automatically becomes final which stops it from changing. Try having the progress dialog as a member of the inner client class instead. This also is a better design and makes your code more maintainable.
class MyClient extends WebViewClient() {
private ProgressDialog progressDialogWebViewNews;
public MyClient(Context c){
progressDialogWebViewNews = new ProgressDialog(c);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialogWebViewNews.setCancelable(false);
progressDialogWebViewNews.setTitle("Demo...");
progressDialogWebViewNews.setMessage("Demo");
progressDialogWebViewNews.show();
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialogWebViewNews.dismiss();
super.onPageFinished(view, url);
}
}
webViewNews.setWebViewClient(new MyClient(MainActivity.this));

Delete super.onPageFinished(view, url),Then first check if progress dialog is showing before you dismiss it in your callback method.
If (progressDialogWebViewNews.showing){
progressDialogWebViewNews.dismiss();
}

Maybe you should create a simple WebView object:
WebView webView = (WebView) getActivity().findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("yourURL");
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
progressDialog.setCancelable(false);
webView.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url)
{
progressDialog.dismiss();
}
});
It works for me.

Related

ProgressDialog over a WebView appears dimmed

I have a WebView in my Android app which displays a web page with a specific URL. The URL is a signal to the app to close the WebView and perform other actions.
I'd like to display a ProgressDialog over the WebView while the app loads the functions required by the URL. However when the ProgressDialog appears, it appears dimmed, as if there is an overlay superimposed on top of it. Displaying an AlertDialog appears as normal. Can anyone advise why the PD appears dimmed?
private class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading");
}
return true;
}
}
In your code your private Inner class name is WebViewthat need to change.
It could be MyWebView or anything else. But should not WebView.
Because,
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
}
In this override method there is a paramiter WebView that is build in class in android from android.webkit package.
If you keep your class name WebView that means you not overriding method form WebViewClient shouldOverrideUrlLoadingand it will not work.
Then
You can override onPageStarted to show your progress dialog
because it call on loading the page....
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading..");
}
}
You must ensure that if (url.contains("my/special/url")) is true. otherwise it never execute...because if(false) never execute ... in this case else part will execute if have..
For showing progress in webview, WebViewChromeClient is used. Try to implement webview using below example. Hope it solves your problem.
public class webView extends AppCompatActivity {
private String mUrl;
public WebView mWebView;
// public ProgressBar progressBar;
private ProgressBar progressBar;//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
Log.e("Webview class", "I'm in web view");
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
//set progress bar max limit
progressBar.setMax(100);
//get the url from the intent of this activity called from the fragments
mUrl = getIntent().getExtras().getString("url");
// set the title of webview to be th url
setTitle(mUrl);
mWebView = (WebView) findViewById(R.id.webview);
//mWebView = new WebView(this);
mWebView.setWebViewClient(new myWebViewClient());
mWebView.setWebChromeClient(new WebChromeClientDemo());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(mUrl);
//setContentView(mWebView);
}
private class myWebViewClient extends WebViewClient {
#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);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
}

How to know if webView started rendering in android

I am loading a pdf in WebView.
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient());
String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");
webView.loadUrl(url);
setContentView(webView);
Whenever the activity is started, WebView is shown blank. But I want to show a progress dialog till the WebView starts rendering pdf file. How can I add a listener to the WebView indicating that the files has been started visible to the user.
public class FGWebViewClient extends WebViewClient {
private WebViewClient wvc;
private static final String TAG=FGWebViewClient.class.getCanonicalName();
ProgressDialog mProgressDialog;
// when you add the webviewclient then show the progressDialog.
public FGWebViewClient(Context context) {
Log.i(TAG, "FGWebViewClient Called...");
this.wvc=wvc;
mProgressDialog=new ProgressDialog(context);
mProgressDialog.setMessage("URL Loading...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCanceledOnTouchOutside(false);
if(mProgressDialog!=null){
mProgressDialog.show();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG,"shouldOverrideUrlLoading");
return wvc.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i(TAG,"onPageStarted");
wvc.onPageStarted(view, url, favicon);
}
// OnpageFinished just dismiss the progressDialog.
#Override
public void onPageFinished(WebView view, String url) {
if(mProgressDialog!=null&&mProgressDialog.isShowing()){
mProgressDialog.dismiss();
mProgressDialog=null;
}
Log.i(TAG,"onPageFinished");
//To clear cache of webview
//view.clearCache(true);
//clearEveryThing(view);
wvc.onPageFinished(view,url);
}
and call like this
WebView webView=(WebView)v.findViewById(R.id.webView1);
FGWebViewClient webViewClient= new FGWebViewClient(context);
webView.setWebViewClient(webViewClient);
webView.setWebChromeClient(new WebChromeClient());
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl((webUrl).toString());
You can use setWebViewClient and display showProgress as below code
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginState(WebSettings.PluginState.ON);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
showProgress();
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
hideProgress();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
hideProgress();
}
});
String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");
webView.loadUrl(url);
You could create custom class and extend it with WebViewClient. Then override method onPageStarted where you could consider that it started rendering. For e.g :
private class CustomWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
showProgress();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dismissDialog();
}
}

App Crashes when Dismissing ProgressDialog

I am using ProgressDialog in my WebView app.
In onPageStarted, I show the dialog but when using pd.dismiss in onPageFinished, the app crashes.
web.setWebViewClient(new WebViewClient() {
//Opens all the Clicked Links in App itself.. (No Opera/Browser/Chrome/Uc popup)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Shows a Dialog when there is an Error
#Override
public void onReceivedError(WebView view, int errorcode,String description, String fallingUrl) {
Toast.makeText(getActivity(),"NetWork Error", Toast.LENGTH_SHORT).show();
}
//Showing ProgressBar when Page is Loading
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
ProgressDialog pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.show();
}
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
});
it happens for nullPointerException. You need to make the ProgressDialog as an instance filed of that class and initialize it onCreate. Like the following:
private ProgressDialog pd;
#override
public void onCreate () {
pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
}
now when you need to show that just call show() method on that pd progressDialog and you can dismiss at any time by calling dismiss() method.
ProgressDialog pd = new ProgressDialog(getActivity());
web.setWebViewClient(new WebViewClient() {
//Opens all the Clicked Links in App itself.. (No Opera/Browser/Chrome/Uc popup)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Shows a Dialog when there is an Error
#Override
public void onReceivedError(WebView view, int errorcode,String description, String fallingUrl) {
Toast.makeText(getActivity(),"NetWork Error", Toast.LENGTH_SHORT).show();
}
//Showing ProgressBar when Page is Loading
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
pd.setMessage("Loading...");
pd.show();
}
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
});
It works for me.
Try this:
//Declare it globally
LinearLayout progressLayout;
In the onCreate():
progressLayout = (LinearLayout) findViewById(R.id.ll_refreshing_progress);
wv_tandc.setWebViewClient(new MyWebViewClient());
wv_tandc.loadUrl(getString(R.string.terms_and_conditions_url));
Implement the WebViewClient :
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressLayout.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("on finish");
progressLayout.setVisibility(View.GONE);
}
}
Write the below code in your xml,where you have defined your WebView:
<LinearLayout
android:id="#+id/ll_refreshing_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:padding="5dp"
android:orientation="vertical">
<ProgressBar
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sf_refreshing"/>
</LinearLayout>
You do not need to use progressdialog,instead you can use custom layout.
Based on this, I assume pd is a member variable.
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
However, you seem to think this is assigning that same pd variable, but it isn't. See variable shadowing
//Showing ProgressBar when Page is Loading
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
ProgressDialog pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.show();
}
Remove ProgressDialog from ProgressDialog pd =, so just pd = new ...
Declare ProgressDialog object as member variable
public void onPageFinished(WebView view,String Url){
if(pd ! =null)
pd.dismiss();
}
Just change
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.show();
}
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
and declare dialog object as
private ProgressDialog pd;

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 not displaying video using WebViewClient

i want to play a video from youtube on webview.. it displays the video,But i want to play it on the same page i mean i've to use WebViewClient.. but using that it doesn't play the video.. (on pressing play button it doesn't play the video) what should i do? my code is
setContentView(R.layout.main);
wvSpecials = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wvSpecials.getSettings();
webSettings.setJavaScriptEnabled(true);
wvSpecials.loadUrl("http://here.com/is link/");
wvSpecials.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(
specialsActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.setMessage("Please wait...");
progressDialog.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
}
You may use the below listed code:
public class YouTube extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView;
myWebView = (WebView) findViewById( R.id.web);
myWebView.setWebViewClient(new MyWebViewClient());
String pre="<iframe class=youtube-player type=text/html width=";
String height=" height=";
String suffix=" src=http://www.youtube.com/embed/**xxxxxxxxxxx**?autoplay=1 frameborder=0>"; // replace xxxxxxxxxxx with the specific embed id of your video
String playVideo=pre+260+height+150+suffix;
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadData(playVideo,"text/html","UTF-8");
}
// override default behaviour of the browser
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "",
"Loading. Please wait...", true);
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
} }

Categories

Resources