I used code below to show progress bar, but it's not dismissing automatically (But when i touch on screen it dismiss)
N.B: Progress bar dismiss only home page in my webview. not any other page of posts. Please suggest me how to dismiss the progress bar dialogue automatically without touching it.
wv.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
wv.loadUrl(mypage_error);}
//ProgressDialogue
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("Loading...");
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
Don't allow the Progressbar to dismiss ,when you touch,set it like below
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("Loading...");
pd.setCancelable(true);
pd.show();
//ProgressDialogue
final ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("Loading...");
wv.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
wv.loadUrl(mypage_error);}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
Related
Can anyone help me when I add this ProgressDialog to my Webview app its work fine but when the page is loaded the progress dialog is not dismissed can anyone help me I am using this code in Fragment
public static ProgressDialog progressDialog;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
progressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
getActivity().setTitle(view.getTitle());
if(progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
});
Edited code which is working for me now thanks to everyone who help me
public static ProgressDialog progressDialog;
//Under onCreate View
progressDialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
getActivity().setTitle(view.getTitle());
if(progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog.hide();
}
super.onPageFinished(view, url);
}
});
Make sure that if(progressDialog!=null && progressDialog.isShowing()) this condition returns true. Put a break point there and make sure they both are true.
try
progressDialog.hide()
Move below line out of onPageStarted() method, as it will be called for each underlying URL and it will keep on showing unless every css and js files are loaded
progressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
Can you please try this code
progressDialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
Hope it helps
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.
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;
progressdialog not dismiss after page loading, here what i am missing
final ProgressDialog progressDialog = new ProgressDialog(Webview.this );
WebView webview= (WebView) findViewById(R.id.webViewID);
webview.setWebViewClient(new WebViewClient());
//webview.loadData(content, "text/html", "UTF-8");
webview.loadUrl("http://hop.betfanplus.com/10000/0/mega4");
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int progress) {
// progressDialog.show();
ProgressDialog
.show(Webview.this,
"ProgressDialog Title",
"ProgressDialog Body");
if (progress == 100) {
progressDialog.dismiss();
}
}
});
any help is appreciated.
You should still use onPageStarted and onPageFinished.
From my experience, using progress percentage will reach 100 even if the webpage fails to load, so that won't work.
boolean loadingFinished = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog progressDialog = new ProgressDialog(Webview.this );
progressDialog.setMessage("ProgressDialog Body");
progressDialog.show();
WebView webview= (WebView) findViewById(R.id.webViewID);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
loadingFinished = false;
}
public void onPageFinished(WebView view, String url) {
loadingFinished = true;
progressDialog.cancel();
}
});
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://hop.betfanplus.com/10000/0/mega4");
}
In my experience , is to declare the webview and then the progressDialog with the webview context in oncreate Method as it is shown here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = findViewById(R.id.web_view);
progressDialog = new ProgressDialog(webView.getContext(), R.style.CustomAlertDialogStyle);
}
Then set the setWebCrhomeListener
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (!progressDialog.isShowing()) {
progressDialog.show();
}
progressDialog.setProgress(progress);
});
}
Then control the Dialog show() and dismiss() in onPageFinished() and onPageStarted()
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.show();
}
I want to hide the progress bar on webview as and when webview started rendering the content. I tried webview.getProgress() to get the current progress of webview. It returns progress in the range between 0-100. But I couldn't conclude that at which range actually webview starts render the content.
I would like to know that, Is there any method to identify the start of rendering the content in webview android.
Your help is much appreciated.
Thank you
You can try to add WebViewClient() and in callback methods hide progress.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
});
Found the solution after a lot of searching! There is a "onPageCommitVisible" method in webViewClient which does exactly this, displays the progressbar until the page renders and the disappears.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
mProgressBar.setVisibility(View.INVISIBLE);
}
});
Hope it helps :D
final ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait....");
pDialog.setCancelable(false);
pDialog.show();
String url = "url";
final Activity activity = (Activity) context;
pdfView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setProgress(progress * 1000);
pDialog.incrementProgressBy(progress);
if(progress == 100 && pDialog.isShowing())
pDialog.dismiss();
}
});
Dialog will dismiss when webview start rendering the content.
class MyWebViewClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
class MYWebViewChromeClient extends WebChromeClient{
#Override
public void onProgressChanged(WebView view, int newProgress) {
//When the schedule is equal to 100 of the time, that is, the load is completed, you can hide the progress bar
super.onProgressChanged(view, newProgress);
}
}