I have a procedure that extracts data from a database and populates it to the list. I want to display progress dialog box while query is executed, but it visually appears only after the query is executed. I believe I have to run a ProgressDialog in a separate thread, but followed few suggestions and could not make it work.
So in my Activity I just have
private void DisplayAllproductListView(String SqlStatement) {
ProgressDialog dialog =
ProgressDialog.show(MyActivity.context, "Loading", "Please wait...", true);
//..................
//..................
//execute sql query here
dialog.dismiss();
}
thanks
1.show your process dialog in main thread
2.start a new thread (such as Thread A) to process your heavy job
3.when done, use handler to send a message from Thread A to main thread, the latter dismisses the process dialog
code like this
private ProcessDialog pd;
private void startDialog()
{
pd = ProgressDialog.show(MainActivity.this, "title", "loading");
//start a new thread to process job
new Thread(new Runnable() {
#Override
public void run() {
//heavy job here
//send message to main thread
handler.sendEmptyMessage(0);
}
}).start();
}
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
pd.dismiss();
}
};
Try something like this:
private class MyAwesomeAsyncTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog mProgress;
#Override
protected void onPreExecute() {
//Create progress dialog here and show it
}
#Override
protected Void doInBackground(Void... params) {
// Execute query here
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//update your listView adapter here
//Dismiss your dialog
}
}
To call it:
new MyAwesomeAsyncTask().execute();
All you need to do, is to tell Android to run it on the main UI thread. No need to create a Handler.
runOnUiThread(new Runnable() {
public void run() {
progressDialog.dismiss();
}
});
Related
In my Activity, I load the content for a list from a DB, and want to display a ProgressDialog while it´s loading.
I got both working on it´s own, but if I load the data in a thread (which I should do?), the list is displayed before it´s data is loaded. But if I use join, the ProgressDialog doesnt even appear.
How can I combine this? Or is this not possible at all with threads? (AsyncTask maybe?)
Here´s the code for reference:
final ProgressDialog progressD=ProgressDialog.show(ShopSwitchActivity.this, "", "Loading..", true);
Thread myThread = new Thread(new Runnable() {
#Override
public void run() {
try
{
getData();
}catch(Exception e){}
}
});
myThread.start();
try {
myThread.join();
} catch (InterruptedException e) {
}
progressD.dismiss();
EDIT: Updated Code with AsyncTask:
public class LoadList extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
ShopSwitchActivity activity;
public LoadList(ShopSwitchActivity activity) {
this.activity = activity;
dialog = new ProgressDialog(activity);
}
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
protected Boolean doInBackground(final String... args) {
try{
activity.getData();
} catch (Exception e) {
Log.e("error", e.getMessage());
}
return true;
}
}
Edit: My Solution
Using AsyncTask now to load the Data, and after it´s done, I refresh the list with the new data.
You can do it with AsyncTask. Write AsyncTask class in your main class that you want to do your operations. You can create the progress dialog in preexcecute of your async class and dismiss in onpostexecute of async class. Here is how you will do this:
class MyAsync extends AsyncTask<String, Void, Void> {
ProgressDialog pd;
Context co;
MyActivity ma;
public MyAsync (MyActivity ma){
this.ma= ma;
this.co = ma;
pd= new ProgressDialog(co);
}
#Override
protected void onPreExecute() {
this.pd.show();
super.onPreExecute();
}
#Override
protected Void doInBackground(String... params) {
// do your database operations here
return null;
}
#Override
protected void onPostExecute(Void result) {
// show db results and dismiss progress dialog pd.dismiss();
super.onPostExecute(result);
}
}
in MyActivity call as :
MyActivity ma = this;
new MyAsync(ma).execute();
You seem to miss the point of a thread. A thread occurs at the same time as your application. So your app doesn't call start then wait for the thread to be over- if it did you could just use a function. Instead your code continues to run. So if you just call join immediately, you're not doing anything. You'd get around a NetworkOnMainThreadException this way, but you'd still hold up the UI thread making your app totally non-responsive (and as a result not showing the dialog), and you'd eventually crash when a watchdog timer kills you.
Instead, the best way to handle this is to use an AsyncTask. Call getData in doInBackground(). Then dismiss the dialog in onPostExecute.
You should use AsyncTask instead actually.
Here is the link to the library. It is fairly simple:
1) onPreExecute() = show ProgressDialog
2) doInBackground() = execute your code
3) onPostExecute() = dismiss ProgressDialog
Here's a nice tutorial too.
In general:
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(this.context);
dialog.setMessage("Loading...");
dialog.setCanceledOnTouchOutside(false);
}
#Override
protected void onPostExecute(String result) {
if(dialog.isShowing()) {
dialog.dismiss();
}
}
private Thread myThread;
private ProgressDialog mProgDialog;
mProgDialog = ProgressDialog.show(ShopSwitchActivity.this,"","Laden..", true);
myThread= new Thread(new Runnable()
{
public void run()
{
myThread.setPriority(Thread.MIN_PRIORITY);
try
{
getData();
}catch(Exception e){}
runOnUiThread(new Runnable()
{
public void run()
{
if (mProgDialog != null&& mProgDialog.isShowing())
mProgDialog.dismiss();
} }
});
}
});
myThread.start();
my code:
OnClickListener()//onclicklistener for button
{
ProgressBar = new ProgressDialog(LoginPageActivity.this);
ProgressBar.setMessage("please wait..");
ProgressBar.show();
TcpConnection loginConnect = new TcpConnection();//TcpConnection is a class
loginConnect.run();
ProgressBar.dismiss();
}
i tried to show progress dialog before calling another class and dismiss it after the call is over. but progressbar will not showing and it dismissed early . but i want to show progress bar for certain period of time.
Inside tcp connection class: having socket connection for user name password thats y i need to display progress for certain period of time
i dont know how to do it!
How I understand you need to use threads. Like this
ProgressBar p = new ProgressDialog(LoginPageActivity.this);
Private Handler handler = new Handler();
p.setVisibality(0); //makes visible
new Thread(new Runnable() {
public void run() {
TcpConnection loginConnect = new TcpConnection();
loginConnect.run();
handler.post(new Runnable() {
public void run() {
p.setVisibility(8);//Makes Invisible
}
});
}
}).start();
I think it will help you
Use AsyncTask to achieve your objective. You can show the progressbar(inside onPreExecute()) until your task gets over(inside doInBackground()) and then you can dismiss it after the task is finished(inside onPostExecute()).
Check this link for more details:
http://developer.android.com/reference/android/os/AsyncTask.html
My guess is that loginConnect.run() is running in its own thread. That's why the progress dialog is being dismissed instantly.
Here's what you should do instead:
class LoginTask extends AsyncTask<Void, Void, Void>{
ProgressDialog d;
#Override
protected void onPreExecute() {
super.onPreExecute();
d = new ProgressDialog(LoginPageActivity.this);
d.setMessage("please wait..");
d.show();
}
#Override
protected Void doInBackground(Void... params) {
TcpConnection loginConnect = new TcpConnection();
loginConnect.run();
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
d.dismiss();
}
}
And in your onClickListener call new LoginTask().execute();
I found this code in SO to show ProgressDialog while load Activity:
progDailog = ProgressDialog.show(MyActivity.this, "Process", "please wait....", true, true);
new Thread(new Runnable() {
public void run() {
// code for load activity
}).start();
Handler progressHandler = new Handler() {
public void handleMessage(Message msg1) {
progDailog.dismiss();
}
};
But I always get this exception:
java.lang.RuntimeException: Can't create handler inside thread that
has not called Looper.prepare()
I appreciate any help for this issue, thanks in advance.
Here is what I would do,
AsyncTask to do the "heavy work" in background:
public class MyTask extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog progressDialog;
public MyTask(Context context) {
this.context = context;
}
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.show();
}
#Override
protected String doInBackground(String... params) {
//Do your loading here
return "finish";
}
#Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
//Start other Activity or do whatever you want
}
}
Start the AsyncTask:
MyTask myTask = new MyTask(this);
myTask.execute("parameter");
Of course you can change the generic types of the AsyncTask to match your problems.
The problem is because you are trying to create Handler inside a worker Thread. It is not possible. Create your Handler inside of onCreate() or somewhere else on the main UI. And you can send message to your handler from your Worker Thread.
This is because Android doesn't allow you to modify the UI from any other Thread other than the Main UI thread itself.
You need to create your handler on the main thread rather than inside OnClick.
I am building a project in which i use async task to show progress bar.
I am using get() method to wait the main thread so we can do the other task before .
but progress bar is showing after completion of doInBackground thered.
I Want to show the loading bar when the loading starts.
It will dismiss when onPostExecute calls.
public class TempConverterActivity extends Activity {
pojo p;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b= (Button) findViewById(R.id.btn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showResult();
}
});
}
private void showResult() {
try {
new LoadData().execute().get();
} catch (Exception e) {
Log.e("async brix--", e.getMessage());
}
runned();
}
private void runned() {
ArrayList<String> al = p.getData();
for (String str : al){
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
private class LoadData extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(TempConverterActivity.this);
protected void onPreExecute() {
dialog.setMessage("Loading data...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected void onPostExecute(final Void unused) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
#Override
protected Void doInBackground(Void... params) {
p = new pojo();
new SoapParser(p);
return null;
}
}}
Please help . Thanks in advance.
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();
}
}
Edited: In my previous answer I suggested using a Handler; however, AsyncTask eliminates the need to do this which I didn't spot.
Why do you feel the need to call AsyncTask.get()? This is a blocking call, and you call this from the UI thread, thus it is ultimately a race condition as to whether it or onPreExecute() is run first.
I see no reason why you should call get() in this context. You want to call runned() after the AsyncTask completes, but you could do this by launching a new thread from onPostExecute(). Alternatively you could do as you do now, using get(), but call that from a new thread instead of the UI thread.
I am build one application in which, I can get data from a server so simply I want to put progress bar on it but I can't give specific time duration. it is dismiss randomly while fetch entire data from server. so can you tell me how can I do this ?
can you give one simple example with elaboration
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.event_detail);
pd = ProgressDialog.show(this, "Please Wait", "Loading ....", true,true);
Thread thread = new Thread(this); //Implment Runnable;
thread.start();
// setData();
}
#Override
public void run()
{
//Do your all work except User Interface Updation
han.sendEmptyMessage(0);
}
private Handler han=new Handler()
{
#Override
public void handleMessage(Message msg)
{
dismiss dialog
}
};
You can use these http://developer.android.com/reference/android/widget/ProgressBar.html sample but it's better to integrate with async task Example of async task and since you don't know how long it will take just use the onPostExecute to close the progress.
hope it helps
What you need is to combine AsynTask and indeterminate ProgressDialog together. Something like this:
private class DownloadTask extends AsyncTask<...> {
private ProgressDialog progressBar;
protected void onPreExecute() {
progressBar = ...;
progressBar.show();
}
protected Long doInBackground() {
// blah blah
}
protected void onPostExecute() {
progressBar.dismiss();
}
}
Don't forget to handle if user wants to cancel the progress.
U can use AsyncTask
private class Myclass extends AsyncTask<Void, Integer, Void> {
ProgressDialog pDialog;
#Override
protected void onPreExecute() {
pDialog= ProgressDialog.show(context, "", "loading...");
pDialog.show();
super.onPreExecute();
}
#Override
protected Long doInBackground(Void... unused) {
// your code here
return null;
}
#Override
protected void onPostExecute(Void unused) {
pDialog.dismiss();
}
}