Skip to content

Commit 8958a6f

Browse files
committed
Adds the README and setting icon support
1 parent f1bce02 commit 8958a6f

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Android View Helpers
2+
3+
This library is a collection of simple view helpers to make Android development easier. Right now, this library just has a few helpful classes but they will expand over time.
4+
5+
## SimpleAlertDialog
6+
7+
This is a simple way to bring up a modal alert dialog within a DialogFragment within a FragmentActivity. The dialog fragment used is the `v4.support` Fragment to support all Android versions.
8+
9+
### Usage
10+
11+
Creating an alert to display is easy:
12+
13+
```java
14+
class MyActivity extends FragmentActivity {
15+
public void showDialog() {
16+
SimpleAlertDialog.build(this,
17+
"Sure you want to continue?", new SimpleAlertListener() {
18+
@Override
19+
public void onPositive() {
20+
// handle OK
21+
}
22+
23+
@Override
24+
public void onNegative() {
25+
// handle cancel
26+
}
27+
}
28+
).show();
29+
}
30+
}
31+
```
32+
33+
By default, this will display the following alert:
34+
35+
![AlertDialog](http://i.imgur.com/3xjhVlZ.png)
36+
37+
You can also adjust the alert to include no buttons, or change the button text.
38+
To display just an "Sure" button:
39+
40+
```java
41+
SimpleAlertDialog.build(this,
42+
"Sure you want to continue?", "Sure", null, new SimpleAlertListener()
43+
).show();
44+
```
45+
46+
To display a "Yes" and "No" button:
47+
48+
```java
49+
SimpleAlertDialog.build(this,
50+
"Sure you want to continue?", "Yes", "No", new SimpleAlertListener()
51+
).show();
52+
```
53+
54+
You can also customize the icon in the alert and other properties:
55+
56+
```java
57+
SimpleAlertDialog alert = SimpleAlertDialog.build(this, "Confirm?", new SimpleAlertListener());
58+
alert.setAlertIcon(R.drawable.ic_some_icon);
59+
alert.setAlertButtons("Yes", "No");
60+
alert.show();
61+
```

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
public class SimpleAlertDialog extends DialogFragment {
1212
SimpleAlertListener listener;
1313
String alertTitle, positiveText, negativeText;
14+
int alertIcon;
1415
FragmentActivity activity;
1516

1617
// SimpleAlertDialog.build(activity, "Do you want to delete that?",
@@ -20,6 +21,7 @@ public static SimpleAlertDialog build(FragmentActivity activity, String body,
2021
SimpleAlertListener listener) {
2122
SimpleAlertDialog dialogAlert = new SimpleAlertDialog();
2223
dialogAlert.setAlertTitle(body);
24+
dialogAlert.setAlertIcon(android.R.drawable.ic_dialog_alert);
2325
dialogAlert.setAlertListener(listener);
2426
dialogAlert.setAlertActivity(activity);
2527
dialogAlert.setAlertButtonText(positiveText, negativeText);
@@ -36,9 +38,18 @@ public void show() {
3638
this.show(activity.getSupportFragmentManager(), null);
3739
}
3840

39-
protected void setAlertTitle(String title) {
41+
public void setAlertTitle(String title) {
4042
this.alertTitle = title;
4143
}
44+
45+
public void setAlertIcon(int drawable) {
46+
this.alertIcon = drawable;
47+
}
48+
49+
public void setAlertButtonText(String positiveText, String negativeText) {
50+
this.positiveText = positiveText;
51+
this.negativeText = negativeText;
52+
}
4253

4354
protected void setAlertListener(SimpleAlertListener listener) {
4455
this.listener = listener;
@@ -47,17 +58,12 @@ protected void setAlertListener(SimpleAlertListener listener) {
4758
protected void setAlertActivity(FragmentActivity activity) {
4859
this.activity = activity;
4960
}
50-
51-
protected void setAlertButtonText(String positiveText, String negativeText) {
52-
this.positiveText = positiveText;
53-
this.negativeText = negativeText;
54-
}
5561

5662
@Override
5763
public Dialog onCreateDialog(Bundle savedInstanceState) {
5864
AlertDialog.Builder builder =
5965
new AlertDialog.Builder(getActivity())
60-
.setIcon(android.R.drawable.ic_popup_reminder)
66+
.setIcon(alertIcon)
6167
.setTitle(alertTitle);
6268
if (positiveText != null) {
6369
builder.setPositiveButton(positiveText,

0 commit comments

Comments
 (0)