Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Created February 9, 2013 22:59
Show Gist options
  • Save joshdholtz/4747487 to your computer and use it in GitHub Desktop.
Save joshdholtz/4747487 to your computer and use it in GitHub Desktop.
Manages alerts and dialogs in Android - Used to subclass out Activity for these methods but now that I use Fragment, FragmentActivity, and Activity this just seemed easier
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import java.util.HashMap;
import java.util.Map;
public class AlertUtils {
Map<Context, AlertDialog> alertHolder;
Map<Context, ProgressDialog> progressHolder;
private static AlertUtils ourInstance = new AlertUtils();
public static AlertUtils getInstance() {
return ourInstance;
}
private AlertUtils() {
alertHolder = new HashMap<Context, AlertDialog>();
progressHolder = new HashMap<Context, ProgressDialog>();
}
/**
* Shows alert - if an alert or progress dialog is already visible for this context (Activity), it will get dismissed
* @param context
* @param title
* @param message
*/
public void showAlert(Context context, String title, String message) {
showAlert(context, title, message, null);
}
/**
* Shows alert - if an alert or progress dialog is already visible for this context (Activity), it will get dismissed
* @param context
* @param title
* @param message
* @param onClickListener
*/
public void showAlert(Context context, String title, String message, DialogInterface.OnClickListener onClickListener) {
// Checks if an alert already exists - if so it gets dismissed
hideAlert(context);
// Checks if a progress dialog already exists - if so it gets dismissed
hideProgress(context);
// Adds defaul click listener
if (onClickListener == null) {
onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
};
}
// Creates, stores, and shows
AlertDialog alert = new AlertDialog.Builder(context).setTitle(title).setMessage(message).setPositiveButton("Okay", onClickListener).create();
alertHolder.put(context, alert);
alert.show();
}
/**
* Hides an alert if one is shown for this context
* @param context
*/
public void hideAlert(Context context) {
AlertDialog alert = alertHolder.get(context);
if (alert != null) {
alert.dismiss();
alert = null;
alertHolder.remove(context);
}
}
/**
* Shows progress dialog - if an alert or progress dialog is already visible for this context (Activity), it will get dismissed
* @param context
* @param title
* @param message
*/
public void showProgress(Context context, String title, String message) {
// Checks if an alert already exists - if so it gets dismissed
hideAlert(context);
// Checks if a progress dialog already exists - if so it gets dismissed
hideProgress(context);
ProgressDialog progress = ProgressDialog.show(context, title, message, true);
progressHolder.put(context, progress);
}
/**
* Hides a progress dialog if one is shown for this context
* @param context
*/
public void hideProgress(Context context) {
ProgressDialog progress = progressHolder.get(context);
if (progress != null) {
progress.dismiss();
progress = null;
progressHolder.remove(context);
}
}
}
public class SomeActivity extends SherlockActivity {
AQuery aq;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
...
// Show progress
AlertUtils.getInstance().showProgress(this, "Logging in", "");
// Do some task
..
if (taskSuccess) {
// Closes progress
AlertUtils.getInstance().hideProgress(this);
} else {
// Closes progress and shows error
AlertUtils.getInstance().showAlert(this, "Error", "Task failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment