Skip to content

Commit 97f52fd

Browse files
committed
Adds SimpleProgressDialog utility
1 parent 8958a6f commit 97f52fd

File tree

4 files changed

+117
-19
lines changed

4 files changed

+117
-19
lines changed

README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ This is a simple way to bring up a modal alert dialog within a DialogFragment wi
88

99
### Usage
1010

11-
Creating an alert to display is easy:
11+
In apps, you often may want to display an alert asking the user a question or displaying a message
12+
to the user. Creating an alert to display is easy:
1213

1314
```java
1415
class MyActivity extends FragmentActivity {
@@ -58,4 +59,32 @@ SimpleAlertDialog alert = SimpleAlertDialog.build(this, "Confirm?", new SimpleAl
5859
alert.setAlertIcon(R.drawable.ic_some_icon);
5960
alert.setAlertButtons("Yes", "No");
6061
alert.show();
62+
```
63+
64+
## SimpleProgressDialog
65+
66+
Often in an application there might be a long running task that requires the UI to be blocked
67+
such as uploading an image. While this isn't the best UI, occassionally this can come up.
68+
69+
Creating an indeterminate modal progress dialog is easy:
70+
71+
```java
72+
SimpleProgressDialog dialog = SimpleProgressDialog.build(this);
73+
```
74+
75+
By default, this will display the following alert:
76+
77+
![ProgressDialog](http://i.imgur.com/rbEV8pM.png)
78+
79+
You can dismiss the dialog once the task is complete with:
80+
81+
```java
82+
dialog.dismiss();
83+
```
84+
85+
You can also adjust the progress dialog to have a custom message:
86+
87+
```java
88+
SimpleProgressDialog dialog = SimpleProgressDialog.build(this, "Uploading...");
89+
dialog.show();
6190
```

src/com/codepath/libraries/androidviewhelpers/SimpleAlertDialog.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
import android.app.Dialog;
66
import android.content.DialogInterface;
77
import android.os.Bundle;
8-
import android.support.v4.app.DialogFragment;
98
import android.support.v4.app.FragmentActivity;
109

11-
public class SimpleAlertDialog extends DialogFragment {
10+
public class SimpleAlertDialog extends SimpleDialogFragment {
1211
SimpleAlertListener listener;
13-
String alertTitle, positiveText, negativeText;
12+
String positiveText, negativeText;
1413
int alertIcon;
15-
FragmentActivity activity;
1614

1715
// SimpleAlertDialog.build(activity, "Do you want to delete that?",
1816
// "OK", "Cancel", ...).show();
1917
public static SimpleAlertDialog build(FragmentActivity activity, String body,
2018
String positiveText, String negativeText,
2119
SimpleAlertListener listener) {
2220
SimpleAlertDialog dialogAlert = new SimpleAlertDialog();
23-
dialogAlert.setAlertTitle(body);
21+
dialogAlert.setAlertMessage(body);
2422
dialogAlert.setAlertIcon(android.R.drawable.ic_dialog_alert);
2523
dialogAlert.setAlertListener(listener);
2624
dialogAlert.setAlertActivity(activity);
@@ -34,14 +32,6 @@ public static SimpleAlertDialog build(FragmentActivity activity, String body,
3432
return build(activity, body, "OK", "Cancel", listener);
3533
}
3634

37-
public void show() {
38-
this.show(activity.getSupportFragmentManager(), null);
39-
}
40-
41-
public void setAlertTitle(String title) {
42-
this.alertTitle = title;
43-
}
44-
4535
public void setAlertIcon(int drawable) {
4636
this.alertIcon = drawable;
4737
}
@@ -54,17 +44,13 @@ public void setAlertButtonText(String positiveText, String negativeText) {
5444
protected void setAlertListener(SimpleAlertListener listener) {
5545
this.listener = listener;
5646
}
57-
58-
protected void setAlertActivity(FragmentActivity activity) {
59-
this.activity = activity;
60-
}
6147

6248
@Override
6349
public Dialog onCreateDialog(Bundle savedInstanceState) {
6450
AlertDialog.Builder builder =
6551
new AlertDialog.Builder(getActivity())
6652
.setIcon(alertIcon)
67-
.setTitle(alertTitle);
53+
.setTitle(getAlertMessage());
6854
if (positiveText != null) {
6955
builder.setPositiveButton(positiveText,
7056
new DialogInterface.OnClickListener() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.codepath.libraries.androidviewhelpers;
2+
3+
import android.support.v4.app.DialogFragment;
4+
import android.support.v4.app.FragmentActivity;
5+
import android.support.v4.app.FragmentManager;
6+
7+
public abstract class SimpleDialogFragment extends DialogFragment {
8+
private FragmentActivity activity;
9+
private String alertMessage;
10+
11+
public void show() {
12+
this.show(activity.getSupportFragmentManager(), null);
13+
}
14+
15+
public void setAlertMessage(String message) {
16+
this.alertMessage = message;
17+
}
18+
19+
protected String getAlertMessage() {
20+
return alertMessage;
21+
}
22+
23+
protected void setAlertActivity(FragmentActivity activity) {
24+
this.activity = activity;
25+
}
26+
27+
protected FragmentManager getSupportFragmentManager() {
28+
return this.activity.getSupportFragmentManager();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.codepath.libraries.androidviewhelpers;
2+
3+
import android.app.Dialog;
4+
import android.app.ProgressDialog;
5+
import android.content.DialogInterface;
6+
import android.content.DialogInterface.OnKeyListener;
7+
import android.os.Bundle;
8+
import android.support.v4.app.FragmentActivity;
9+
import android.view.KeyEvent;
10+
11+
public class SimpleProgressDialog extends SimpleDialogFragment {
12+
// SimpleProgressDialog.build(activity, "Loading...").show();
13+
public static SimpleProgressDialog build(FragmentActivity activity,
14+
String message) {
15+
SimpleProgressDialog dialogProgress = new SimpleProgressDialog();
16+
dialogProgress.setAlertMessage(message);
17+
dialogProgress.setAlertActivity(activity);
18+
return dialogProgress;
19+
}
20+
21+
// SimpleProgressDialog.build(activity).show();
22+
public static SimpleProgressDialog build(FragmentActivity activity) {
23+
return build(activity, "Please wait...");
24+
}
25+
26+
@Override
27+
public Dialog onCreateDialog(Bundle savedInstanceState) {
28+
final ProgressDialog dialog = new ProgressDialog(getActivity());
29+
dialog.setMessage(getAlertMessage());
30+
dialog.setIndeterminate(true);
31+
dialog.setCancelable(false);
32+
dialog.setCanceledOnTouchOutside(false);
33+
disableBackButton(dialog);
34+
return dialog;
35+
}
36+
37+
// Disable the back button
38+
private void disableBackButton(ProgressDialog dialog) {
39+
OnKeyListener keyListener = new OnKeyListener() {
40+
@Override
41+
public boolean onKey(DialogInterface dialog, int keyCode,
42+
KeyEvent event) {
43+
44+
if (keyCode == KeyEvent.KEYCODE_BACK) {
45+
return true;
46+
}
47+
return false;
48+
}
49+
50+
};
51+
dialog.setOnKeyListener(keyListener);
52+
}
53+
}

0 commit comments

Comments
 (0)