How can I get a loading icon (like a small circle running rund) while I am logging into a FTP server in an Android application? I know that there is a progress bar in the AsyncTask
, but want to grey out the MainView (MainActivity)
and display a loading icon as long as the login takes place.
How to do that?
If you want the Progress "icon" to be shown as a Dialog (a smaller screen over your Activity) you can use ProgressDialog
: http://developer.android.com/reference/android/app/ProgressDialog.html
To use it:
ProgressDialog pd = new ProgressDialog(this);
pd.setTitle(title);
pd.setMessage(message);
pd.show();
...
pd.dismiss(); //Use it when the task is over
This is how it looks like:
If you just want your layout to be seen like it's deactivated you can use the alpha property (opacity). For example, in an application where I want a layout to be seen darker than usual I use:
AlphaAnimation alphaDeselected = new AlphaAnimation(1F, 0.25F);
alphaDeselected.setDuration(0);
view.startAnimation(alphaDeselected);
Of course, the view will be seen darker if the background is dark.
EDIT: How to apply it.
I usually show the ProgressDialog right before starting the AsyncTask.
pd = new ProgressDialog(this); // I declared pd as a global variable to access it from AsyncTask
pd.setTitle(title);
pd.setMessage(message);
pd.show();
mAuthTask = new UserLoginTask();
mAuthTask.execute((Void) null);
Then I dismiss the ProgressDialog when the AsyncTask finishes or is canceled:
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
...
}
@Override
protected void onPostExecute(final Boolean success) {
pd.dismiss();
...
}
@Override
protected void onCancelled() {
pd.dismiss();
...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.