I've tried everything I know how to do to kill it. What I want it to do is load the webpage and then kill the ProgressDialog. How do?
package com.calebfultz.package;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class Lists extends Activity {
private WebView webView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
ProgressDialog pd = ProgressDialog.show(Lists.this, "",
"Loading. Please wait...", true);
// Create reference to UI elements
webView = (WebView) findViewById(R.id.web_engine);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://cfultz.tumblr.com");
// workaround so that the default browser doesn't take over
webView.setWebViewClient(new MyWebViewClient()
);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You may want to make your ProgressDialog variable as a member variable of the Lists class and in your shouldOverrideUrlLoading method, add the line
pd.dismiss();
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// Show your progress dialog here
prDialog = ProgressDialog.show(this, "", "Loading, please wait...", true);
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
//Finish your progress dialog here
if (prDialog.isShowing() || prDialog != null) {
prDialog.dismiss();
}
super.onPageFinished(view, url);
}
I think you can override the onPageFinished function in MyWebViewClient: you can dismiss the ProgressDialog in this function. Or you can override the onProgressChanged in MyWebChromeClient(extends WebChromeClient): when the progress is 100, you can dismiss the ProgressDialog.
Please see this code:
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWebView.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
it is working fine for me.........enjoy.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//start loading animation
//loading is a ProgressBar type
this.loading.setVisibility(0);
view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress >= 100) {
this.loading.setVisibility(8);
}
}
});
Related
I am creating a simple app Android webview, but also following the guides, I can not implement a progress bar to my code.
how can I implement a loading bar that will disappear after the page loads?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("http://www.abcdefcsadfg.org");
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You can initialize a Progressbar in onCreate, like below and set its initial visibility to View.GONE.
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);
And your MyBrowser should look like this
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
}
UPDATE:
Check this link
You should declare progress bar in xml file and refer to it in the main activity so that it appears in your application.
Set its progress in MyBrowser.
Refer this link, it will help you.
http://javatechig.com/android/progressbar-while-loading-webview
When a page loads up I show the Progress Dialog, and I dismiss it when the page finishes loading. After that if there a picture that loads when I scroll down, the Progress Dialog will show up, but doesn't disappears after the picture is loaded. How can I fix this problem?
This is my code, use it and you will know exactly what I mean. I will appreciate it if you can help.
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
// Declaring
WebView browser;
ProgressDialog pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing
browser = (WebView) findViewById(R.id.webView1);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
browser.getSettings().setBuiltInZoomControls(true);
// Loading
browser.loadUrl("http://www.tumblr.com/tagged/cool%20pictures");
// Progress Dialog
pd = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
view.loadUrl(url);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public void onLoadResource(WebView view, String url) {
try {
if (pd.isShowing() == false) {
pd = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void onPageFinished(WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}
}
});
}
}
After 3 hours of searching, I figured out the solution. Instead of showing the ProgressDialog inside the onLoadResource method, I used onPageStarted method. So my code would look like this.
import android.graphics.Bitmap;
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (pd.isShowing() == false) {
pd = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
}
}
I was wondering if there exits another effective way to get the URL that will load up on the WebView. My current code gives the current Url and not the one that will be loaded.
For example if my WebView load this: http://stackoverflow.com
After loading If I click on Questions, I would not get this url http://stackoverflow.com/questions, for some reason I would get http://stackoverflow.com
So my question is how would you get the url that will be loading on a WebView?
This is my code, please help!
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
// Declaring
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing
browser = (WebView) findViewById(R.id.webView1);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
browser.getSettings().setBuiltInZoomControls(true);
// Loading
browser.loadUrl("http://stackoverflow.com");
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
//here I get the Url, but its not accurate. Sometimes it works, sometiems it doesn't
Toast.makeText(getApplicationContext(), browser.getUrl(),
Toast.LENGTH_SHORT).show();
}
});
}
}
Try to get url from this function :
EDIT:
browser.loadUrl("http://stackoverflow.com");
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
Log.v("TEST", url);
if(url.equals("http://stackoverflow.com/questions")){
Toast.makeText(getApplicationContext(), "SKIP", Toast.LENGTH_SHORT).show();
}
else{
view.loadUrl(url);
}
return true;
}
});
Reference :
shouldOverrideUrlLoading
I know its very late but,for other users if it helps it would be my pleasure.
In ur WebViewClient u can use :
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String webUrl=view.getUrl();
return true;
}
}
I am loading url into webview:
WebView webview=(WebView)findViewById(R.id.webview);
webview.loadUrl(url);
It's taking some time to load url, during which it shows a blank screen. I want to display a progress dialog while the url is loading:
ProgressDialog dialog = ProgressDialog.show(this, "HI","Loading......", true);
However, the above is code is not working. If any have any ideas, please help.
set a WebViewClient to your WebView, start your progress dialog on you onCreate() method an dismiss it when the page has finished loading in onPageFinished(WebView view, String url)
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Main extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.webview = (WebView)findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("http://www.google.com");
}
}
your main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="#string/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
You will have to over ride onPageStarted and onPageFinished callbacks
mWebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (progressBar!= null && progressBar.isShowing()) {
progressBar.dismiss();
}
progressBar = ProgressDialog.show(WebViewActivity.this, "Application Name", "Loading...");
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
Check out the sample code. It help you.
private ProgressBar progressBar;
progressBar=(ProgressBar)findViewById(R.id.webloadProgressBar);
WebView urlWebView= new WebView(Context);
urlWebView.setWebViewClient(new AppWebViewClients(progressBar));
urlWebView.getSettings().setJavaScriptEnabled(true);
urlWebView.loadUrl(detailView.getUrl());
public class AppWebViewClients extends WebViewClient {
private ProgressBar progressBar;
public AppWebViewClients(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
#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);
progressBar.setVisibility(View.GONE);
}
}
Thanks.
You need to set an own WebViewClient for your WebView by extending the WebViewClient class.
You need to implement the two methods onPageStarted (show here) and onPageFinished (dismiss here).
More guidance for this topic can be found in Google's WebView tutorial
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
Here's one of the tabs I have that loads a page.
package realstrat.cfostudio.magazineapp;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import realstrat.cfostudio.magazineapp.R;
public class TabActivity3 extends Activity {
WebView mWebView;
private ProgressDialog progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl("--company URL--");
mWebView.setWebViewClient(new FirstTabWebViewClient());
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("OverviewMode", mWebView.getSettings().getLoadWithOverviewMode());
mWebView.saveState(savedInstanceState);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
((WebView)findViewById(R.id.webview1)).restoreState(savedInstanceState);
if (savedInstanceState.getBoolean("OverviewMode") == false) {
((WebView)findViewById(R.id.webpageview)).getSettings().setLoadWithOverviewMode(false);
((WebView)findViewById(R.id.webpageview)).getSettings().setUseWideViewPort(false);
}
else {
((WebView)findViewById(R.id.webpageview)).getSettings().setLoadWithOverviewMode(true);
((WebView)findViewById(R.id.webpageview)).getSettings().setUseWideViewPort(true);
}
return;
}
private class FirstTabWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// YouTube video link
if (url.startsWith("vnd.youtube"))
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return (true);
}
if (url.endsWith("-m.html")){
mWebView.getSettings().setLoadWithOverviewMode(false);
mWebView.getSettings().setUseWideViewPort(false);
}
else {
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
}
view.loadUrl(url);
return true;
}
public void onPageStarted(WebView view, String url, Bitmap favicon){
progressBar = ProgressDialog.show(TabActivity3.this, "", "Loading...", true);
}
public void onPageFinished(WebView view, String url) {
progressBar.hide();
if (url.endsWith("-m.html")){
mWebView.getSettings().setLoadWithOverviewMode(false);
mWebView.getSettings().setUseWideViewPort(false);
}
else {
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
}
return;
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Context context = getApplicationContext();
CharSequence text = "Desc: " + description;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Once in a while it will crash with a nullPointerException on the line progressBar.hide() under onPageFinished(). That doesn't make any sense since onPageStarted() starts the progressBar, and onPageStarted always comes before onPageFinished(). Why is this?
This only happens like, once in 10 times or something, which is really confusing to me.
It usually happens (always?) when the activity is being started for the first time.
try this
if(progressBar!=null)
progressBar.hide();
Maybe the Activity has been restarted in between the two callbacks? Try rotating the phone while the progressBar is shown to see what the results are.
Try to load progress bar as singleton object. If you create anther progress bar object before hide first one then second progress bar will crash in hide().
if(_progressBar == null)
_progressBar = new ProgressDialog(this);