Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ The simplest way to use SweetAlertDialog is to add the library as aar dependency

```
dependencies {
compile 'com.github.cazaea:sweet-alert-dialog:1.0.0'
}
implementation 'com.github.yuxiaohui78:SweetAlertDialog:1.1.1'
}
```

**Gradle**(Project)
Expand Down Expand Up @@ -74,11 +74,46 @@ You can customize progress bar dynamically with materialish-progress methods via
- setRimColor(int rimColor)
- getSpinSpeed()
- setSpinSpeed(float spinSpeed)

- setDialogWidth (int dpWidth)
- setDialogHeight (int dpHeight)
- setDialogLayout (int dpWidth, int dpHeight)


thanks to the project [materialish-progress](https://github.com/pnikosis/materialish-progress) and [@pedant](https://github.com/pedant) participation.

more usages about progress, please see the sample.

Touch the outside to cancel the dialog :

SweetAlertDialog sd = new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
.setCustomImage(R.drawable.pill_icon)
.setTitleText("3:00PM")
.setContentText("Asprin 50 mg. Take 2 pills")
.setNeutralText("Later")
.setCancelText("Skip it")
.setConfirmText("Take it!");

sd.setCancelable(true);
sd.setCanceledOnTouchOutside(true);
sd.show();
sd.setDialogWidth (300); //dynamically to change the dialog width (unit is dp)



Add a neutral button :

new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
.setCustomImage(R.drawable.pill_icon)
.setTitleText("3:00PM")
.setContentText("Asprin 50 mg. Take 2 pills")
.setNeutralText("Later")
.setCancelText("Skip it")
.setConfirmText("Take it!")
.show();



A basic message:

new SweetAlertDialog(this)
Expand Down Expand Up @@ -173,6 +208,8 @@ Show the cancel button and bind listener to it:
}
})
.show();



[more android tech shares: cazaea.com](http://www.cazaea.com)

Expand Down
47 changes: 44 additions & 3 deletions app/src/main/java/com/cazaea/sweetalert/sample/SampleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Handler;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;

import com.cazaea.sweetalert.SweetAlertDialog;

Expand All @@ -25,11 +26,48 @@ public void onCreate(Bundle savedInstanceState) {
findViewById(R.id.warning_cancel_test).setOnClickListener(this);
findViewById(R.id.custom_img_test).setOnClickListener(this);
findViewById(R.id.progress_dialog).setOnClickListener(this);
findViewById(R.id.three_button).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.three_button:
SweetAlertDialog sd1 = new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
.setCustomImage(R.drawable.pill_icon)
.setTitleText("3:00PM")
.setContentText("Asprin 50 mg. Take 2 pills")
.setNeutralText("Later")
.setCancelText("Skip it")

.setConfirmText("Take it!")
.setNeutralClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
Toast.makeText(SampleActivity.this, "Take it later!", Toast.LENGTH_SHORT).show();
sweetAlertDialog.dismiss();
}
})
.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
Toast.makeText(SampleActivity.this, "Skip it!", Toast.LENGTH_SHORT).show();
sweetAlertDialog.dismiss();
}
})
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
Toast.makeText(SampleActivity.this, "Take it now!", Toast.LENGTH_SHORT).show();
sweetAlertDialog.dismiss();
}
});

sd1.setCancelable(true);
sd1.setCanceledOnTouchOutside(true);
sd1.show();
sd1.setDialogLayout(320, 200);
break;
case R.id.basic_test:
// default title "Here's a message!"
SweetAlertDialog sd = new SweetAlertDialog(this);
Expand Down Expand Up @@ -84,7 +122,7 @@ public void onClick(SweetAlertDialog sDialog) {
.show();
break;
case R.id.warning_cancel_test:
new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
SweetAlertDialog sdd = new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
.setTitleText("Are you sure?")
.setContentText("Won't be able to recover this file!")
.setCancelText("No,cancel plx!")
Expand Down Expand Up @@ -122,8 +160,11 @@ public void onClick(SweetAlertDialog sDialog) {
.setConfirmClickListener(null)
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
}
})
.show();
});


sdd.show();
sdd.setDialogWidth(300);
break;
case R.id.custom_img_test:
new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
Expand Down
Binary file added app/src/main/res/drawable/pill_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions app/src/main/res/layout/sample_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
android:contentDescription="@string/app_name"
android:src="@drawable/logo_big" />



<TextView
android:id="@+id/txt_0"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -178,6 +180,25 @@
android:layout_margin="10dp"
android:text="Try me!" />

<TextView
android:id="@+id/txt_10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/custom_img_test"
android:layout_below="@id/custom_img_test"
android:layout_marginLeft="15dp"
android:text="Three button"
android:textColor="#797979"
android:textSize="14sp" />

<Button
android:id="@+id/three_button"
style="@style/dialog_blue_button"
android:layout_below="@id/txt_10"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Three button" />

</RelativeLayout>
</ScrollView>

79 changes: 79 additions & 0 deletions library/src/main/java/com/cazaea/sweetalert/SweetAlertDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
Expand All @@ -14,6 +15,7 @@
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cazaea.materialishprogress.ProgressWheel;
Expand All @@ -35,9 +37,11 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener {
private String mContentText;
private boolean mShowCancel;
private boolean mShowConfirm;
private boolean mShowNeutral;
private boolean mShowContent;
private String mCancelText;
private String mConfirmText;
private String mNeutralText;
private int mAlertType;
private FrameLayout mErrorFrame;
private FrameLayout mSuccessFrame;
Expand All @@ -50,10 +54,12 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener {
private ImageView mCustomImage;
private Button mConfirmButton;
private Button mCancelButton;
private Button mNeutralButton;
private ProgressHelper mProgressHelper;
private FrameLayout mWarningFrame;
private OnSweetClickListener mCancelClickListener;
private OnSweetClickListener mConfirmClickListener;
private OnSweetClickListener mNeutralClickListener;
private boolean mCloseFromCancel;

public static final int NORMAL_TYPE = 0;
Expand Down Expand Up @@ -153,16 +159,41 @@ protected void onCreate(Bundle savedInstanceState) {
mWarningFrame = (FrameLayout)findViewById(R.id.warning_frame);
mConfirmButton = (Button)findViewById(R.id.confirm_button);
mCancelButton = (Button)findViewById(R.id.cancel_button);
mNeutralButton = (Button)findViewById(R.id.neutral_button);
mProgressHelper.setProgressWheel((ProgressWheel)findViewById(R.id.progressWheel));
mConfirmButton.setOnClickListener(this);
mCancelButton.setOnClickListener(this);
mNeutralButton.setOnClickListener(this);

setTitleText(mTitleText);
setContentText(mContentText);
setCancelText(mCancelText);
setNeutralText(mNeutralText);
setConfirmText(mConfirmText);
changeAlertType(mAlertType, true);
}

public static float convertDpToPixel(float dp, Context context) {
return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}

public void setDialogLayout (int dpWidth, int dpHeight){
LinearLayout ly = findViewById(R.id.loading);
ly.getLayoutParams().width = (int)convertDpToPixel(dpWidth, getContext());
ly.getLayoutParams().height = (int)convertDpToPixel(dpHeight, getContext());
ly.setLayoutParams(ly.getLayoutParams());
}

public void setDialogWidth (int dpWidth){
LinearLayout ly = findViewById(R.id.loading);
ly.getLayoutParams().width = (int)convertDpToPixel(dpWidth, getContext());
ly.setLayoutParams(ly.getLayoutParams());
}

public void setDialogHeight (int dpHeight){
LinearLayout ly = findViewById(R.id.loading);
ly.getLayoutParams().height = (int)convertDpToPixel(dpHeight, getContext());
ly.setLayoutParams(ly.getLayoutParams());
}

private void restore () {
Expand Down Expand Up @@ -274,6 +305,18 @@ public SweetAlertDialog setContentText (String text) {
return this;
}

public boolean isShowCancelButton () {
return mShowCancel;
}

public boolean isShowNeutralButton () {
return mShowNeutral;
}

public boolean isShowConfirmButton () {
return mShowConfirm;
}

public SweetAlertDialog showCancelButton (boolean isShow) {
mShowCancel = isShow;
if (mCancelButton != null) {
Expand All @@ -290,6 +333,18 @@ public SweetAlertDialog showConfirmButton (boolean isShow) {
return this;
}

public SweetAlertDialog showNeutralButton (boolean isShow) {
mShowNeutral = isShow;
if (mNeutralButton != null) {
mNeutralButton.setVisibility(mShowNeutral ? View.VISIBLE : View.GONE);
}
return this;
}

public boolean isShowContentText () {
return mShowContent;
}

public SweetAlertDialog showContentText (boolean isShow) {
mShowContent = isShow;
if (mContentTextView != null) {
Expand All @@ -315,6 +370,19 @@ public String getConfirmText () {
return mConfirmText;
}

public SweetAlertDialog setNeutralText (String text) {
mNeutralText = text;
if (mNeutralButton != null && mNeutralText != null) {
showNeutralButton(true);
mNeutralButton.setText(mNeutralText);
}
return this;
}

public String getNeutralText() {
return mNeutralText;
}

public SweetAlertDialog setConfirmText (String text) {
mConfirmText = text;
if (mConfirmButton != null && mConfirmText != null) {
Expand All @@ -334,6 +402,11 @@ public SweetAlertDialog setConfirmClickListener (OnSweetClickListener listener)
return this;
}

public SweetAlertDialog setNeutralClickListener (OnSweetClickListener listener) {
mNeutralClickListener = listener;
return this;
}

protected void onStart() {
mDialogView.startAnimation(mModalInAnim);
playAnimation();
Expand Down Expand Up @@ -374,6 +447,12 @@ public void onClick(View v) {
} else {
dismissWithAnimation();
}
} else if (v.getId() == R.id.neutral_button) {
if (mNeutralClickListener != null) {
mNeutralClickListener.onClick(SweetAlertDialog.this);
} else {
dismissWithAnimation();
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions library/src/main/res/layout/alert_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@
android:text="@string/dialog_cancel"
android:visibility="gone" />

<Button
android:id="@+id/neutral_button"
style="@style/dialog_blue_button"
android:background="@drawable/gray_button_background"
android:layout_marginRight="10dp"
android:visibility="gone"
android:text="@string/dialog_neutral" />

<Button
android:id="@+id/confirm_button"
style="@style/dialog_blue_button"
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<string name="dialog_ok">OK</string>
<string name="dialog_cancel">Cancel</string>
<string name="LOADING">Loading...</string>
<string name="dialog_neutral">Neutral</string>
</resources>