why the progress dialog doesn't disappear on some page? - java

this is my mainactivity, when i load some page the progress dialog doesn't disappear onpagefinished...where is the problem?
if you want to try the problem are with the page "accedi" or on the payment of the order
private WebView webView;
#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.getSettings().setDomStorageEnabled(true);
webView.loadUrl("http://www.alldrink.it");
WebClientClass webViewClient = new WebClientClass();
webView.setWebViewClient(webViewClient);
WebChromeClient webChromeClient=new WebChromeClient();
webView.setWebChromeClient(webChromeClient);
setContentView(webView);
webView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
}
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(MainActivity.this);
pd.setTitle("Attendere");
pd.setMessage("Caricamento in corso..");
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}
}
}
public class WebChromeClass extends WebChromeClient{
}
}

the dismiss() function is working, but your onPageStarted() has run two times, making dialog recreate again and look like it never disappear. Try changing your onPageStarted as:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!pd.isShowing())
{
pd.setTitle("Attendere");
pd.setMessage("Caricamento in corso..");
pd.show();
}
}

Related

Wait few seconds after onPageFinished

i have developed a Webview App. While loading WebView, an Splash Screen as "Progressbar" is shown. But I see the Splash Screen only 1 seconds because the Splashscreen are hidden after successful load of webview. The loading is very fast but i want to see the splash screen for 3 seconds. How can I implement it in a way that the splashscreen is shown for min 3 seconds?
MainActivity:
public class MainActivity extends AppCompatActivity {
String ShowOrHideWebViewInitialUse = "show";
private WebView webview ;
private ImageView spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getDelegate().setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setContentView(R.layout.activity_main);
webview =(WebView)findViewById(R.id.webView);
spinner = (ImageView)findViewById(R.id.logo_id3);
webview.setWebViewClient(new CustomWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("xxxxxxxxxx");
}
boolean doubleBackToExitPressedOnce = false;
#Override
public void onBackPressed() {
if (webview.canGoBack()){
webview.goBack();
Toast.makeText( this, "Going back inside a WebView", Toast.LENGTH_SHORT).show();
}else {
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Doppelt zurück um zu beenden", Toast.LENGTH_SHORT).show();
}
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
// Your Code
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
// only make it invisible the FIRST time the app is run
if (ShowOrHideWebViewInitialUse.equals("show")) {
webview.setVisibility(webview.INVISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url) {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(webview.VISIBLE);
super.onPageFinished(view, url);
}
}
}
In your case, you can delay your code this way:
public void onPageFinished(WebView view, String url) {
Thread.sleep(2000);
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(webview.VISIBLE);
super.onPageFinished(view, url);
}
The unit for 2000 is milliseconds. So this will pause the thread for 2 seconds and then continue
Or use the handler class like so:
public void onPageFinished(WebView view, String url) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(webview.VISIBLE);
super.onPageFinished(view, url);
}
}, 2000);
}
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// Hide the splash here
}
});
You can use a Handler with postDelayed
Here the code in postDelayed will execute after 3 seconds from calling onPageFinished
#Override
public void onPageFinished(WebView view, String url) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(webview.VISIBLE);
super.onPageFinished(view, url);
}
}, 3000); // 3 seconds (3000 millisec)
}

Website unavailable when loaded with WebView

I am trying to load a url with webview that loads perfectly fine on mobile using chrome, but the website says "Website temporarily unavailable". I tried changing the user agent to no avail. I can't figure out for the life of me why this is happening. The url in question is https://www.manototv.com/news
A screenshot of loading with webview
A screenshot of loading with chrome
Relevant portion of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
mWebView = findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
mWebView.setWebViewClient(new MyWeb());
mWebView.setWebChromeClient(new MyChrome());
webSettings.setJavaScriptEnabled(true);
webSettings.setMediaPlaybackRequiresUserGesture(false);
mWebView.loadUrl(path)
}
class MyWeb extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
mWebView.loadUrl(SCRIPT_FULLSCREEN);
mWebView.loadUrl(SCRIPT_CLICK);
mWebView.loadUrl(SCRIPT_PLAY);
mWebView.setVisibility(View.VISIBLE);
}
}
private class MyChrome extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
#Override
public Bitmap getDefaultVideoPoster() {
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
#Override
public void onHideCustomView() {
((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
#Override
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846);
}
}

pass string as url and load inside webView

I'm trying to send string as url from Fragment to Activity and load url inside webview inside activity. But I tried a lot and it is not working out.
Here is my Fragment code
intent.putExtra("url", "http://www.ekantipur.com/");
startActivity(intent);
and my webview activity contains
package com.nepalpolice.nepalinews;
public class web extends AppCompatActivity {
WebView webview;
ProgressBar pbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
String url = getIntent().getStringExtra("url");
webview = (WebView)findViewById(R.id.webView1);
pbar = (ProgressBar)findViewById(R.id.progressBar1);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.loadDataWithBaseURL(url, null, null, null, null);
}
public class WebViewClient extends android.webkit.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 onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
pbar.setVisibility(View.GONE);
webview.setVisibility((View.VISIBLE));
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
else
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}
It shows Progress bar and then blank white screen.
Thanks in advance.
Try like this
Instead of this
webview.loadDataWithBaseURL(url, null, null, null, null);
use like this
web.loadUrl(url);
and make sure, from the intent your getting the correct URL by printing in logs.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://appgeyser.com/");
}
It is very simple. Use this for load url.

Android Webview page load message

Im trying to add a message on the webview if the page thats trying to load to fails. Is this possible? If so, is it possible for the message to replace the default "the webpage failed to load" screen?
activity file
public class MainActivity extends Activity {
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.technotalkative.com");
}
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 onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

webview android with progress dialog

im trying to make an webview application for android with progress bar, i tried a lot of examples from here but none was working .. my problem is that on a frame page or a page with facebook likes or comment plugin the progressdialog never stoped .. here is my code.. please test it and tell me what is wrong.
private WebView webview;
private ProgressDialog progressDialog;
boolean loadingFinished = true;
boolean redirect = false;
int nr = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.string.webview);
final Activity activity = this;
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webview.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setTitle("PitziWorld");
progressDialog.setMessage(progressDialog.toString());
progressDialog.setCancelable(isFinishing());
progressDialog.show();
}
loadingFinished = false;
}
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
if (progressDialog.isShowing() || progressDialog!=null) {
progressDialog.hide();
progressDialog = null;
}
} else{
redirect = false;
}
}
});
webview.loadUrl("http://www.pitziworld.ro");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView webView = (WebView) findViewById(R.string.webview);
//Verifica daca tasta apasata a fost back si daca exista istoric
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// Daca nu a fost tasta back sau nu este istoric, returnam valoare buton
`
this is what you are looking for!
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mProgress.show();
}
to load progress bar in every page
this is it!

Categories

Resources