Loading dialog in android - android

i have a problem with loading dialog in webview. my dialog was dismis but i have to wait some second again to display full webview.. what i want is, if loading dialog was dismis webview is done.. this is my code for webview :
progressBar = new ProgressDialog(this);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebViewFacebook.this, "Loading", "Please wait for a moment...");
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss(); //--> this progressbar always dismis although mw webview not done ><
}
}
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;
finish();
}
});
alertDialog.show();
}
});
webView.loadUrl(strurl);

See the below code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = (WebView) findViewById(R.id.WebView01);
view.setBackgroundColor(0); // set the background transparent
progressDialog = ProgressDialog.show(view.getContext(), "Loading...", "Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER );
view.setWebChromeClient(new MyWebChromeClient());
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.loadUrl("http://www.XXX.XXX");
}
And then create classs
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
private class MyWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if(progressDialog!=null){
if ( newProgress >=80 ) {
progressDialog.dismiss();
} else {
progressDialog.setMessage(newProgress + " % loaded");
}
}
super.onProgressChanged(view, newProgress);
}
}

Related

Progress dialog not dismiss after (progress == 100) in webview

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

set progress dialog over main layout

I have a code in which the progress dialog appears along with main layout. I want the progress dialog to be on the forefront and the main layout in background just like its showing the link enter link description here.
public class MainActivity extends Activity {
private WebView webView;
private EditText urlEditText;
private ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlEditText = (EditText) findViewById(R.id.urlField);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);
Button openUrl = (Button) findViewById(R.id.goButton);
openUrl.setOnClickListener(new OnClickListener() {
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onClick(View view) {
String url = urlEditText.getText().toString();
if (validateUrl(url)) {
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
}
private boolean validateUrl(String url) {
return true;
}
});
}
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) {
progress.setVisibility(View.GONE);
MainActivity.this.progress.setProgress(100);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
MainActivity.this.progress.setProgress(0);
super.onPageStarted(view, url, favicon);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
Try to use WebChromeClient instead of WebViewClient to show and update progress :
show pregress bar before call webview.loadUrl() :
if (validateUrl(url)) {
webView.getSettings().setJavaScriptEnabled(true);
progress.setVisibility(View.VISIBLE);
webView.loadUrl(url);
}
Update progress in onProgressChanged :
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress == 100) {
progress.setVisibility(View.GONE);
} else {
progress.setProgress(newProgress);
}
}
});

Displaying the webpage in a relativelayout using webview

Hello guys this the my xml code i am trying the display the browser in window but its always gets redirected to the browser
Xml:
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
Code:
public class Activity_Retailers extends Activity {
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_activity);
WebView webview = (WebView)findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webview.loadUrl("http://www.facebook.com");
webview.setWebViewClient(new WebViewClient());
}
Help me out guys thanks a lot
Set WebViewClient to your WebView in java-code and owerride shouldOwerrideUrlLoading() method.
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
Try following code,
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.string.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");
}
}

Android progressbar inside WebView

To create a webView recently I was using webViewClient. mWebView.setWebViewClient(new WebViewClient());
But I need to implement a progress bar. When user clicks a link, this progress bar will be visible. After page complete, progressbar will be hidden and webView will be visible. Regarding to this and this, So I added a WebChromeClient. But it loads first URL, but when I click a button inside my web page, a dialog opens and asks to open URL with which application.
I read that I should override shouldOverrideUrlLoading() method. but I get an error that "shouldOverrideUrlLoading" can't be overriden for WebChromeClient.
I would be happy if you can give an example that has progressbar and webView, also opens new URLs inside the same webView.
public class WebActivity extends Activity {
WebView mWebView;
ProgressBar mProgress;
Context mContext;
ProgressBar mProgressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
this.mContext = getApplicationContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebChromeClient(new myWebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUserAgentString("myApp");
mProgressBar = (ProgressBar) findViewById(R.id.webProgressBar);
mProgressBar.setMax(100);
}
public class myWebChromeClient extends WebChromeClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
//WebActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
}
try with this code
webView = (WebView) view.findViewById(R.id.transcationwebview);
progressdialog = ProgressDialog.show(mContext, "",
mContext.getString(R.string.please_wait));
progressdialog.setCancelable(true);
progressdialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
webView.stopLoading();
// webView.clearView();
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyChromeClient());
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if (progressdialog != null && progressdialog.isShowing()) {
progressdialog.dismiss();
} else {
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
webView.loadUrl("url");
webView.getSettings().setBuiltInZoomControls(true);
public class MyChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
try {
if (progressdialog.isShowing()) {
progressdialog.setMessage(getString(R.string.loading)
+ newProgress + " %");
} else {
/*
* webView.stopLoading(); webView.clearView();
*/
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
you can extends WebViewClient instead of WebChromeClient and override the onPageStarted to show the ProgressBar and dismiss it in onPageReceived
I made some changes in the Activity.
use WebViewClient instead of WebChromeClient
and also use ProgressDialog instead of ProgressBar
public class WebActivity extends Activity {
WebView mWebView;
Context mContext;
ProgressDialog mProgressBar;
private static final int DIALOG2_KEY = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
this.mContext = getApplicationContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new MyWebChromeClient());
mWebView.getSettings().setBuiltInZoomControls(true);
showDialog(DIALOG2_KEY); }
#Override
protected void onResume() {
super.onResume();
mWebView.loadUrl("YOUR_URL");
}
private final class MyWebChromeClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
dismissDialog(DIALOG2_KEY);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("file")) {
return false;
} else{
view.loadUrl(url);
return true;
}
}
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id)
{
case DIALOG2_KEY:
{
mProgressBar.setMessage("Loading");
mProgressBar.setIndeterminate(true);
mProgressBar.setCancelable(false);
return mProgressBar;
}
}
return null;
}
}
Hope this help you

Progress Dialog In Webview

Simply put how can I make a progress dialog show up every time a new link is clicked in my webview. I have tried many tutorials and methods but every one of them only shows the dialog once, when the app is initially loaded.
Here is my code
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(myActivity.this, "",
"Loading. Please wait...", true);
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
check out this:
wvCouponsAndOffers.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(CouponsWebViewUI.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
progressDialog.setCancelable(false);
progressDialog.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
else
return false;
}});
progressDialog.show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
progressDialog.setCancelable(false);
progressDialog.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
else
return false;
}});
progressDialog.show();
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
Try this,
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("http://slashdot.org/");
And if you want to make your own progressDialog then
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// Start PROGRESS DIALOG
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//HIDE PROGRESS DIALOG LOADING IT HAS FINISHED
}
});
And let me know what happen..
Try this easiest one,it work for me
public class HomeWebViewActivity extends AppCompatActivity{
private String weburl;
private WebView webview;
private Toolbar toolbar;
private ProgressDialog prDialog;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_webview_activity);
toolbar = (Toolbar) findViewById(R.id.toolbar_homewebview);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
weburl="ADD YOUR URL HERE";
webview = (WebView) findViewById(R.id.webviewinfo);
webview.setWebViewClient(new WebVwClientcls());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.loadUrl(weburl);
}
private class WebVwClientcls 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(HomeWebViewActivity.this);
prDialog.setMessage("Please wait ...");
prDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(prDialog!=null){
prDialog.dismiss();
}
}
}
}

Categories

Resources