Android - creating a ProgressDialog without showing it - android

How do you create a ProgressDialog without showing it? When the WebView starts trying to open a page I want a progress dialog to appear if the page hasn't loaded after two seconds (and disappear if it still hasn't loaded after 30 seconds). The following code is failing because ProgressDialog progress = new ProgressDialog(MainActivity.this);, despite what I've seen in various SO answers, throws a NullPointerException error.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
// ProgressDialog progress = new ProgressDialog(MainActivity.this); // gives NullPointerException
ProgressDialog progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
timerBeginDialog(2000, progress);
progress.setCancelable(true);
timerRemoveDialog(30000, progress);
}
#Override
public void onPageFinished(WebView view, String url) {
drawer.closeDrawer(GravityCompat.START);
progress.dismiss();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.loadUrl("file:///android_asset/www/error.html");
drawer.openDrawer(GravityCompat.START);
progress.dismiss();
}
});
.
.
}
public void timerBeginDialog(long time, final ProgressDialog pd){
new Handler().postDelayed(new Runnable() {
public void run() {
pd = ProgressDialog.show(MainActivity.this, "", "Loading...", true); // gives error "Cannot assign a value to final variable pd"
}
}, time);
}
public void timerRemoveDialog(long time, final ProgressDialog pd){
new Handler().postDelayed(new Runnable() {
public void run() {
pd.dismiss();
}
}, time);
}

That's because the context has not been created yet when your initializer runs.
Just place the initialization inside onCreate method.

Move
ProgressDialog progress = new ProgressDialog(MainActivity.this);
inside onCreate

You cannot pass a variable by reference on Java, so what you need to do is to define the variable within the class, which you apparantly have done, and its name is progress, but withing the method call you're referring to variable 'pd' which is the one it takes as parameter.
You should instead set the value of 'progress' to the new ProgressDialog like this:
progress = ProgressDialog.show(MainActivity.this, "", "Loading...", true);
and then on other method
progress.dismiss();

Related

ProgressDialog over a WebView appears dimmed

I have a WebView in my Android app which displays a web page with a specific URL. The URL is a signal to the app to close the WebView and perform other actions.
I'd like to display a ProgressDialog over the WebView while the app loads the functions required by the URL. However when the ProgressDialog appears, it appears dimmed, as if there is an overlay superimposed on top of it. Displaying an AlertDialog appears as normal. Can anyone advise why the PD appears dimmed?
private class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading");
}
return true;
}
}
In your code your private Inner class name is WebViewthat need to change.
It could be MyWebView or anything else. But should not WebView.
Because,
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
}
In this override method there is a paramiter WebView that is build in class in android from android.webkit package.
If you keep your class name WebView that means you not overriding method form WebViewClient shouldOverrideUrlLoadingand it will not work.
Then
You can override onPageStarted to show your progress dialog
because it call on loading the page....
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading..");
}
}
You must ensure that if (url.contains("my/special/url")) is true. otherwise it never execute...because if(false) never execute ... in this case else part will execute if have..
For showing progress in webview, WebViewChromeClient is used. Try to implement webview using below example. Hope it solves your problem.
public class webView extends AppCompatActivity {
private String mUrl;
public WebView mWebView;
// public ProgressBar progressBar;
private ProgressBar progressBar;//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
Log.e("Webview class", "I'm in web view");
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
//set progress bar max limit
progressBar.setMax(100);
//get the url from the intent of this activity called from the fragments
mUrl = getIntent().getExtras().getString("url");
// set the title of webview to be th url
setTitle(mUrl);
mWebView = (WebView) findViewById(R.id.webview);
//mWebView = new WebView(this);
mWebView.setWebViewClient(new myWebViewClient());
mWebView.setWebChromeClient(new WebChromeClientDemo());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(mUrl);
//setContentView(mWebView);
}
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) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
}

Dismiss ProgressDialog after WebView has shown

How can I use ProgressDialog to wait while WebView is loading?
Sometimes myText has a huge text and some old devices can't load WebView fast. I want to show ProgressDialog while WebView is loading and when it's already shown I want to dismiss the ProgressDialog.
I have:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
String myText = "some text";
WebView myWebView = (WebView)findViewById(R.id.webView);
myWebView.loadDataWithBaseURL("", myText, "text/html", "UTF-8", "");
dialog.dismiss();
It doesn't show any ProgressDialog.
If I don't use dismiss, I always see my ProgressDialog.
Use AsyncTask.
// ..onCreate code come here...
Button button = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
new LongOperation().execute(); // here calling LongOperation class to do `AsyncTask`.
}
});
private class LongOperation extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.class);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
}
#Override
protected String doInBackground(Void... params) {
// background operation comes here..
}
#Override
protected void onPostExecute(String result) {
// close progressDialog here by calling
dialog.dismiss();
}
}
Put this code in your current Java class which contains web view..
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(dialog.isShowing() && dialog != null) {
dialog.dismiss();
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!dialog.isShowing() && dialog != null) {
dialog.show();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
And set this
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
below you initialized your webview
just try this,
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url)
{
progDailog.dismiss();
}
});

Website wrapped in App - Loading

I'm working on an Android App that simply wraps a mobile version of a website inside of a native App container.
At present I call (example):
wv.loadUrl("http://www.websitehere.com/");
I have exported the entire project and tested it on my phone and when loading a new page inside of the App it takes a few moments, however not being on a browser there is no loading indicator, so it is a still screen on the current page for a few moments (depending on internet speed) while the new page loads.
What would be the best way to implement a loading indicator so that the user knows that a new page is infact loading?
EDIT: Loading added, but only for the initial pageload of app.
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ProgressDialog pd = ProgressDialog.show(this, "Page is", "Loading...",true);
WebView wv = (WebView)findViewById(R.id.my_webview);
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing()&&pd!=null);
{
pd.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pd = ProgressDialog.show(this, "Page is", "Loading...", true);
view.loadUrl(url);
return true;
}
});
wv.loadUrl("http://www.websitehere.com");
}
}
If your website is html based and not server side (asp/php etc) you could package the website in the app so no internet is required. Downside to this is updating 2 websites.
Alternatively you could add a loading script (javascript) to the website itself.
You could start a ProgressDialog before you load the website and dismiss it after the loading is done.
Code should look something like this:
ProgressDialog progressDialog = ProgressDialog.show(this, "", "Loading...");
wv.loadUrl("http://www.yourwebsite.com");
progressDialog.dismiss();
EDIT: new code for every new url clicked
You should add this to your WebViewClient and also make you (ProgressDialog) pd a member variable.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pd = ProgressDialog.show(this, "Page is:", "Loading...",true);
view.loadUrl(url);
return true;
}
EDIT:
You should make the ProgressDialog pd a member variable.
public class Main extends Activity {
private ProgressDialog pd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pd = ProgressDialog.show(this, "Page is", "Loading...",true);
WebView wv = (WebView)findViewById(R.id.my_webview);
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing()&&pd!=null);
{
pd.dismiss();
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd = ProgressDialog.show(Main.this, "Page is", "Loading...", true);
}
});
wv.loadUrl("http://www.websitehere.com");
}
}
EDIT: using onPageStarted.

progressBar and refresh webview combination android

EDIT:
I am using a ProgressBar for a Webview that works fine. Now I want to refresh my Webview, which also works. When I am trying to combine them, the Spinner doesn't end at all. This is my code:
private final Runnable m_Runnable = new Runnable()
{
public void run()
{
//Toast.makeText(WebcamsScreen.this,"in runnable",Toast.LENGTH_SHORT).show();
doRefreshingStuff(id);
WebcamsScreen.this.mHandler.postDelayed(m_Runnable, 20000);
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialitions
doRefreshingStuff(id);
this.mHandler = new Handler();
this.mHandler.postDelayed(m_Runnable,20000);
doRefreshingStuff(id);
}
public void doRefreshingStuff(String id){
setContentView(R.layout.web4);
webView7 = (WebView) findViewById(R.id.web_4_1);
webView7.getSettings().setJavaScriptEnabled(true);
webView7.getSettings().setJavaScriptEnabled(true);
progressBar = ProgressDialog.show(WebcamsScreen.this, "example", "...");
url="my url";
webView7.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url1) {
view.loadUrl(url1);
return true;
}
public void onPageFinished(WebView view, String url1) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
webView7.loadUrl(url);
what must I change?
It looks like you are refreshing your WebView every 20 seconds and popping up a new progress dialog when you do. Your code assumes that the old progress dialog has already been dismissed. If it hasn't, then the progress dialog just became orphaned and will remain on display forever. You can probably fix this by inserting this code:
if (progressBar != null && progressBar.isShowing()) {
progressBar.dismiss();
}
just before the line progressBar = ....
But I wouldn't fix your code that way. You are doing a huge amount of work that you don't need to do. (For instance, you don't need to reload the same content view.) Here's my revised version:
private final Runnable m_Runnable = new Runnable() {
public void run() {
doRefreshingStuff(id);
mHandler.postDelayed(m_Runnable, 20000);
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialitions
setContentView(R.layout.web4);
webView7 = (WebView) findViewById(R.id.web_4_1);
webView7.getSettings().setJavaScriptEnabled(true);
webView7.getSettings().setJavaScriptEnabled(true);
webView7.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (!mProgressBar.isShowing()) {
mProgressBar.show();
}
}
public void onPageFinished(WebView view, String url1) {
if (mProgressBar.isShowing()) {
mProgressBar.dismiss();
}
}
});
mProgressBar = new ProgressDialog(this);
mProgressBar.setTitle("example");
mProgressBar.setMessage("...");
mHandler = new Handler();
}
protected void onResume() {
super.onResume();
mHandler.post(m_Runnable);
}
protected void onPause() {
super.onPause();
mHandler.removeCallbacks(m_Runnable);
}
public void doRefreshingStuff(String id) {
url="my url";
webView7.loadUrl(url);
}
Note that I moved the starting of the page loading loop to onResume() and kill the looping in onPause(). You may want slightly different logic, but you should definitely stop looping somehow when your activity pauses or finishes.

How to show a progress dialog while HTML page is loaded in WebView

I am using the web view for showing html pages, and I want to show a progress dialog until the page is loaded. When that is done, the dialog has to disappear. I have used AsyncTask for this, but the dialog doesn't show. See my code below:
class DownloadAysnc extends AsyncTask<String, String, Void>
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(OverView.this, "", "Please Wait ...");
}
#Override
protected Void doInBackground(String... arg0) {
webView.loadUrl("http://marico.com/html/investor/overview.php");
return null;
}
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
progressDialog.dismiss();
}
}
And if I take the help of google docs to show the web page, then the HTML Tag is shown, but not the page. Below is that code:
String url = "http://google.co.in/";
String googleDocsUrl = "http://docs.google.com/viewer?url="+url;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(googleDocsUrl ), "text/html");
startActivity(intent);
this.myWebView.loadUrl(googleDocsUrl);
Can someone help me with this?
Use this code:
webView.setWebViewClient(new WebViewClient() {
ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
prDialog = ProgressDialog.show(Activity.this, null, "loading, please wait...");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
prDialog.dismiss();
super.onPageFinished(view, url);
}
});
webView.loadUrl(url);
v.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
WebView v=(WebView)findViewById(R.id.wv);
//Toast.makeText(mContext, v.getUrl() + newProgress +"uploading...", Toast.LENGTH_SHORT).show();
ProgressBar s=(ProgressBar)findViewById(R.id.progressBar1);
s.setMax(100);
s.setProgress(newProgress);
if(newProgress==100){
v.setVisibility(0);
//Toast.makeText(mContext, "upload finished...", Toast.LENGTH_SHORT).show();
}else{
v.setVisibility(8);
//Toast.makeText(mContext, "uploading...", Toast.LENGTH_SHORT).show();
}
}
});
You can show the Progress of the WebView in the Title Bar of the WebView. Here is a complete example for the same that show Progress Status in the Title Bar of the WebView.
You can try following code,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}

Categories

Resources