show progress bar with message when clicked on button in android
i wanted to show progress bar with message login success when i click on login button how can i do in android can any body 开发者_高级运维guide me if possible please give the code
See what you need to do is : when user clicks on button, start background service (here authenticating the user). You have to display progressbar when doing background processing. Depending upon result display appropriate message whether user is authorized or not say for example in Toast or another Activity.
For doing lengthy operations you have to use Thread or AsyncTask. This handles UI as well as background processes at the same time.
What you have to do is : in onClick event call the AsyncTask. Here you have to create an object of LoginOperation and then simply call its LoginOperation.execute() method.
For displaying the results, you Toast a message like :
Toast.makeText(context,"Your Message",Toast.LENGTH_LONG).show();
Ofcourse you have to add it in onPostExecute() of AsyncTask.
private class LoginOperation extends AsyncTask<String, Void, String> {
private ProgressDialog Dialog = new ProgressDialog(ClassName.this);
@Override
protected String doInBackground(String... params) {
// perform long running operation operation
//Here you have to do your network operations..
return null;
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
// do your Display and data setting operation here
}
catch(Exception e)
{
}
//Here depending upon your validation, display appropriate message.
If(correct_user)
call another activity
else
display error toast message in the same activity
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
Dialog.setMessage("Authenticating.....");
Dialog.show();
// Things to be done before execution of long running operation. For example showing ProgessDialog
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(Void... values) {
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
}
精彩评论