Reputation: 2053
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!
Upvotes: 0
Views: 2478
Reputation: 25803
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();
Upvotes: 2
Reputation: 3098
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
Upvotes: 2
Reputation: 18592
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
Upvotes: 2