ProgressDialogue box not appearing in webview - java

I am just stuck at a little thing for about 1 full day I created a webview android app and its working fine but i want a progress dialogue box which basically appears when the next page loads. here is my code
//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("Your Internet is Slow..");
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
Please help me fix this and tell me the error
and i did imported android.app.ProgressDialog;

ProgressDialog pd; //Global
pd=new ProgressDialog(MainActivity.this); // OnCreate
pd.setTitle("Please Wait..");
pd.setMessage("Your Internet is Slow..");
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon); // Should be First
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url); //Should be First
pd.dismiss();
}

Related

how to stop the web view and display a reload message if the page does not load for 30 seconds?

I have a web view and i have this code (P.S i am a beginer in programming)
private WebChromeClient getChromeClient() {
return new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressDialog.show();
if (newProgress ==100){
progressDialog.dismiss();
}
super.onProgressChanged(view, newProgress);
mWebView.setVisibility(View.GONE);
}
};
}
I already have webview client and webchrome client, you can see and download my code files here (cloud.mail.ru/public/2hHz/25DMkxh3U)
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mWebView.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
if (mbErrorOccured == false && mbReloadPressed) {
hideErrorLayout();
mbReloadPressed = false;
}
super.onPageFinished(view, url);
mWebView.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
At the top you can see my code in Main activity.
You can check how to find if the webview loaded completely at this link :
here
Once you start loading the webview, you can start a handler and check if the page is loaded completely, or you can reload the webpage.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// check if the webview is loaded completely after 30 seconds here
//if the page is not loaded then call the below line to reload
// mWebview.loadUrl("http://www.google.com");
}
}, 30000);
Override Webviewclient class and start a countdowntimer in onPageStarted method and cancel the timer in onPageFinished , invoke your method in onfinish of countdowntimer.

Not able reload custom URL sometimes on webviewclient

I'm using an URL in webview, when I click a link it should direct it to a custom url like paynow://xxxxx.
When I use the webview client it is coming sometimes and not redirect sometime.
Is there any wrong in my code.
please check the url onPageStarted and shouldOverrideUrlLoading
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
MainApplication.getComponent(this).inject(this);
back=findViewById(R.id.back_w);
front=findViewById(R.id.right_w);
wv1=(WebView)findViewById(R.id.webview);
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setWebViewClient(new MyBrowser());
wv1.setWebChromeClient(new WebChromeClient());
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(Constants.Load_url);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return super.shouldOverrideUrlLoading(view, uri.toString());
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (url.startsWith("paynow://"))
{
if (url.contains("order"))
{
startActivityForResult(new Intent(context,TipsActivity.class).putExtra("url",url),1);
}
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
wv1.setVisibility(View.GONE);
}
}
Answer:
WebView is blocking popup which redirect to custom url
by adding wv1.getSettings().setPluginState(WebSettings.PluginState.ON); it worked
You need to move the logic in onPageStarted to the shouldOverrideUrlLoading functions.
WebView is blocking popup which redirect to custom url, by adding wv1.getSettings().setPluginState(WebSettings.PluginState.ON); it worked

How to add iframe into an activity using android studio (I want to add google calender from a website)

This is my iframe code :
<iframe src="https://www.google.com/calendar/embed?mode=WEEK&height=600&wkst=7&bgcolor=%23FFFFFF&src=diit.info_m7f27lakenu0t49fbe2aojn9rg#group.calendar.google.com&color=%23865A5A&ctz=Asia%2FDhaka" style=" border-width:0 " width="750" height="440" frameborder="0" scrolling="no"></iframe>`
Now I want to show this google calendar into my activity. Please let me know how can i do that.
İf you want to show google calendar in your app. You have to use google calendar api.
Here is the step by step education in google developers.
google calendar app
İf you want to show a web site in your app,you can use WebView.
enter code public class WebViewActivity extends AppCompatActivity {
private WebView mWebView;
private static final String urlTag = "url";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String postUrl = intent.getStringExtra(urlTag);
String title=intent.getStringExtra("title");
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl(postUrl);
// setContentView(mWebView);
// TODO:YUKLEME PROGRESS DENE
mWebView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(WebViewActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.e("I am loading Here ", "Start");
progressDialog.setTitle("Loading");
progressDialog.setMessage("Please wait....");
progressDialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("I am loading Here ", "Override");
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
}
});
setContentView(mWebView);
}
Using WebVew I guess...?
Or are you encountering any problems using it?

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

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();
}
}

Android WebViewClient before and after load resources

I would like to be notify each time before and after the WebViewClient loads a resource.
I found onLoadResource and it seams to be before load.
Is there any possibility to be notified after the resource is loaded?
Try the below code-
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
//Do something
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//Do something
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
//Do something
super.onPageFinished(view, url);
}
}
It may help you.

Categories

Resources