So here is an example I created by following this tutorial
http://developer.android.com/intl/fr/guide/topics/ui/dialogs.html
under the heading "Example ProgressDialog with a second thread"
I modified it a little to load a webpage and use the progress of the
page to display/dismiss the dialog
package com.test.progressdialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class ProgressDialogTest extends Activity {
static final int PROGRESS_DIALOG = 0;
ProgressThread progressThread;
ProgressDialog progressDialog;
private Button mButton;
private WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.progressDialog);
mWebView = (WebView) findViewById(R.id.content);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mButton.setVisibility(View.GONE);
mWebView.loadUrl("http://en.m.wikipedia.org/wiki/::Home");
showDialog(PROGRESS_DIALOG);
}
});
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case PROGRESS_DIALOG:
progressDialog = new
ProgressDialog(ProgressDialogTest.this);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
progressThread = new ProgressThread(handler);
progressThread.start();
return progressDialog;
default:
return null;
}
}
// Define the Handler that receives messages from the thread
and update the progress
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
if (total >= 100) {
dismissDialog(PROGRESS_DIALOG);
progressThread.setState(ProgressThread.STATE_DONE);
}
}
};
/** Nested class that performs progress calculations (counting) */
private class ProgressThread extends Thread {
Handler mHandler;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 1;
int mState;
ProgressThread(Handler h) {
mHandler = h;
}
public void run() {
mState = STATE_RUNNING;
//total = 0;
while (mState == STATE_RUNNING) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Log.e("ERROR", "Thread Interrupted");
}
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", mWebView.getProgress());
msg.setData(b);
mHandler.sendMessage(msg);
}
}
/* sets the current state for the thread,
* used to stop the thread */
public void setState(int state) {
mState = state;
}
}
}
Hopefully that helps you in your task.
-Tope
On Mon, Feb 1, 2010 at 1:05 PM, Achanta <[email protected]> wrote:
> I saw that tutorial earlier and have also tried it.
>
> Yes for now even a progress dialog in that way works for me.
> I know i need to use getProgress but do not know how to send the
> progress to the thread running the progress dialog.
>
> Thanks again.
>
> On Feb 1, 11:58 am, Temitope Akinwande <[email protected]> wrote:
>> I found this tutorial
>>
>> http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
>>
>> What you can try to do and see if it works is to create a new thread
>> on button click so that when the page is loading, you can display an
>> indeterminate progress dialog while you are waiting on the page to be
>> done loading using Webview's getProgress()
>> methodhttp://developer.android.com/intl/fr/reference/android/webkit/WebView...()
>> .
>>
>> Create a handler so that when the page is done loading, you can
>> dismiss the dialog.
>> Hope that helps you(see the tutorial).
>>
>> On Mon, Feb 1, 2010 at 11:41 AM, Achanta <[email protected]> wrote:
>> > Thank you for the reply,
>>
>> >> as I'm sure this question has been asked before.
>> > I was also sure that it has been asked before because its a common
>> > task, But searching the web or this group did not give me the
>> > appropriate results.
>>
>> >> From what I gather, you'll have to start a thread using AsyncTask and
>> >> that way you can show your progress dialog.
>> > I may have to but I am not sure. Here I am loading a webpage and this
>> > is happening in the same thread or it appears to be. I can also get
>> > the progress of it from the WebView. I am not doing any background
>> > activity like calculations or downloading stuff, but loading the page
>> > which is on the same thread. So I am a little lost on how to get this
>> > done.
>>
>> > Thanks again.
>>
>> > On Feb 1, 11:12 am, Temitope Akinwande <[email protected]> wrote:
>> >> Hi,
>>
>> >> You can check this
>> >> outhttp://developer.android.com/intl/fr/guide/topics/ui/dialogs.html#Pro...
>>
>> >> From what I gather, you'll have to start a thread using AsyncTask and
>> >> that way you can show your progress dialog.
>> >> A search of progress dialog and async task should yield more results
>> >> as I'm sure this question has been asked before.
>>
>> >> -Tope
>>
>> >> On Mon, Feb 1, 2010 at 10:39 AM, Achanta <[email protected]>
>> >> wrote:
>> >> > I have a search box and a web view in my activity.
>>
>> >> > I also have a search button which when someone clicks opens a the url
>> >> > in the webview below the search box.
>>
>> >> > Everything is working fine except that it remains blank while the page
>> >> > loads.
>> >> > I want to show a loading message with a spinning icon/animation while
>> >> > the page loads. [similar to one in the market app].
>>
>> >> > please let me know how to do it.
>> >> > Thank you.
>>
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups "Android Developers" group.
>> >> > To post to this group, send email to [email protected]
>> >> > To unsubscribe from this group, send email to
>> >> > [email protected]
>> >> > For more options, visit this group at
>> >> >http://groups.google.com/group/android-developers?hl=en
>>
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Android Developers" group.
>> > To post to this group, send email to [email protected]
>> > To unsubscribe from this group, send email to
>> > [email protected]
>> > For more options, visit this group at
>> >http://groups.google.com/group/android-developers?hl=en
>>
>>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en