Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom View Feature #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
118 changes: 82 additions & 36 deletions library/src/main/java/cn/pedant/SweetAlert/SweetAlertDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
Expand All @@ -15,6 +17,8 @@
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.pnikosis.materialishprogress.ProgressWheel;
Expand Down Expand Up @@ -52,19 +56,23 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener {
private Button mCancelButton;
private ProgressHelper mProgressHelper;
private FrameLayout mWarningFrame;
private FrameLayout mCustomViewFrame;
private OnSweetClickListener mCancelClickListener;
private OnSweetClickListener mConfirmClickListener;
private boolean mCloseFromCancel;
private View customView;
private LinearLayout mMainDialogView;

public static final int NORMAL_TYPE = 0;
public static final int ERROR_TYPE = 1;
public static final int SUCCESS_TYPE = 2;
public static final int WARNING_TYPE = 3;
public static final int CUSTOM_IMAGE_TYPE = 4;
public static final int PROGRESS_TYPE = 5;
public static final int CUSTOM_VIEW = 6;

public static interface OnSweetClickListener {
public void onClick (SweetAlertDialog sweetAlertDialog);
public void onClick(SweetAlertDialog sweetAlertDialog);
}

public SweetAlertDialog(Context context) {
Expand All @@ -78,13 +86,13 @@ public SweetAlertDialog(Context context, int alertType) {
mProgressHelper = new ProgressHelper(context);
mAlertType = alertType;
mErrorInAnim = OptAnimationLoader.loadAnimation(getContext(), R.anim.error_frame_in);
mErrorXInAnim = (AnimationSet)OptAnimationLoader.loadAnimation(getContext(), R.anim.error_x_in);
mErrorXInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.error_x_in);
// 2.3.x system don't support alpha-animation on layer-list drawable
// remove it from animation set
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
List<Animation> childAnims = mErrorXInAnim.getAnimations();
int idx = 0;
for (;idx < childAnims.size();idx++) {
for (; idx < childAnims.size(); idx++) {
if (childAnims.get(idx) instanceof AlphaAnimation) {
break;
}
Expand All @@ -94,7 +102,7 @@ public SweetAlertDialog(Context context, int alertType) {
}
}
mSuccessBowAnim = OptAnimationLoader.loadAnimation(getContext(), R.anim.success_bow_roate);
mSuccessLayoutAnimSet = (AnimationSet)OptAnimationLoader.loadAnimation(getContext(), R.anim.success_mask_layout);
mSuccessLayoutAnimSet = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.success_mask_layout);
mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_in);
mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_out);
mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
Expand Down Expand Up @@ -140,20 +148,22 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.alert_dialog);

mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
mTitleTextView = (TextView)findViewById(R.id.title_text);
mContentTextView = (TextView)findViewById(R.id.content_text);
mErrorFrame = (FrameLayout)findViewById(R.id.error_frame);
mErrorX = (ImageView)mErrorFrame.findViewById(R.id.error_x);
mSuccessFrame = (FrameLayout)findViewById(R.id.success_frame);
mProgressFrame = (FrameLayout)findViewById(R.id.progress_dialog);
mSuccessTick = (SuccessTickView)mSuccessFrame.findViewById(R.id.success_tick);
mMainDialogView = (LinearLayout) findViewById(R.id.loading);
mTitleTextView = (TextView) findViewById(R.id.title_text);
mContentTextView = (TextView) findViewById(R.id.content_text);
mErrorFrame = (FrameLayout) findViewById(R.id.error_frame);
mErrorX = (ImageView) mErrorFrame.findViewById(R.id.error_x);
mSuccessFrame = (FrameLayout) findViewById(R.id.success_frame);
mProgressFrame = (FrameLayout) findViewById(R.id.progress_dialog);
mSuccessTick = (SuccessTickView) mSuccessFrame.findViewById(R.id.success_tick);
mSuccessLeftMask = mSuccessFrame.findViewById(R.id.mask_left);
mSuccessRightMask = mSuccessFrame.findViewById(R.id.mask_right);
mCustomImage = (ImageView)findViewById(R.id.custom_image);
mWarningFrame = (FrameLayout)findViewById(R.id.warning_frame);
mConfirmButton = (Button)findViewById(R.id.confirm_button);
mCancelButton = (Button)findViewById(R.id.cancel_button);
mProgressHelper.setProgressWheel((ProgressWheel)findViewById(R.id.progressWheel));
mCustomImage = (ImageView) findViewById(R.id.custom_image);
mCustomViewFrame = (FrameLayout) findViewById(R.id.sweet_custom_view_frame);
mWarningFrame = (FrameLayout) findViewById(R.id.warning_frame);
mConfirmButton = (Button) findViewById(R.id.confirm_button);
mCancelButton = (Button) findViewById(R.id.cancel_button);
mProgressHelper.setProgressWheel((ProgressWheel) findViewById(R.id.progressWheel));
mConfirmButton.setOnClickListener(this);
mCancelButton.setOnClickListener(this);

Expand All @@ -165,7 +175,8 @@ protected void onCreate(Bundle savedInstanceState) {

}

private void restore () {
private void restore() {
mCustomViewFrame.setVisibility(View.GONE);
mCustomImage.setVisibility(View.GONE);
mErrorFrame.setVisibility(View.GONE);
mSuccessFrame.setVisibility(View.GONE);
Expand All @@ -181,7 +192,7 @@ private void restore () {
mSuccessRightMask.clearAnimation();
}

private void playAnimation () {
private void playAnimation() {
if (mAlertType == ERROR_TYPE) {
mErrorFrame.startAnimation(mErrorInAnim);
mErrorX.startAnimation(mErrorXInAnim);
Expand Down Expand Up @@ -220,14 +231,19 @@ private void changeAlertType(int alertType, boolean fromCreate) {
mProgressFrame.setVisibility(View.VISIBLE);
mConfirmButton.setVisibility(View.GONE);
break;
case CUSTOM_VIEW:
mMainDialogView.setPadding(30, 0, 30, 20);
mMainDialogView.setGravity(Gravity.START);
mCustomViewFrame.setVisibility(View.VISIBLE);
break;
}
if (!fromCreate) {
playAnimation();
}
}
}

public int getAlerType () {
public int getAlerType() {
return mAlertType;
}

Expand All @@ -236,19 +252,19 @@ public void changeAlertType(int alertType) {
}


public String getTitleText () {
public String getTitleText() {
return mTitleText;
}

public SweetAlertDialog setTitleText (String text) {
public SweetAlertDialog setTitleText(String text) {
mTitleText = text;
if (mTitleTextView != null && mTitleText != null) {
mTitleTextView.setText(mTitleText);
}
return this;
}

public SweetAlertDialog setCustomImage (Drawable drawable) {
public SweetAlertDialog setCustomImage(Drawable drawable) {
mCustomImgDrawable = drawable;
if (mCustomImage != null && mCustomImgDrawable != null) {
mCustomImage.setVisibility(View.VISIBLE);
Expand All @@ -257,15 +273,45 @@ public SweetAlertDialog setCustomImage (Drawable drawable) {
return this;
}

public SweetAlertDialog setCustomImage (int resourceId) {
public SweetAlertDialog setCustomImage(int resourceId) {
return setCustomImage(getContext().getResources().getDrawable(resourceId));
}

public String getContentText () {
public SweetAlertDialog setCustomView(View view, boolean wrapCustomViewInScroll) {
customView = view;
View innerView = view;
if (innerView.getParent() != null) {
((ViewGroup) innerView.getParent()).removeView(innerView);
}

if (wrapCustomViewInScroll) {
final ScrollView sv = new ScrollView(getContext());
sv.setClipToPadding(false);
sv.setPadding(0, 16, 0, 16);
sv.addView(innerView, new ScrollView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
innerView = sv;
}
mCustomViewFrame.addView(innerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return this;
}

public View getCustomView() {
if (null != customView) {
return customView;
} else {
return null;
}
}


public String getContentText() {
return mContentText;
}

public SweetAlertDialog setContentText (String text) {
public SweetAlertDialog setContentText(String text) {
mContentText = text;
if (mContentTextView != null && mContentText != null) {
showContentText(true);
Expand All @@ -274,35 +320,35 @@ public SweetAlertDialog setContentText (String text) {
return this;
}

public boolean isShowCancelButton () {
public boolean isShowCancelButton() {
return mShowCancel;
}

public SweetAlertDialog showCancelButton (boolean isShow) {
public SweetAlertDialog showCancelButton(boolean isShow) {
mShowCancel = isShow;
if (mCancelButton != null) {
mCancelButton.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);
}
return this;
}

public boolean isShowContentText () {
public boolean isShowContentText() {
return mShowContent;
}

public SweetAlertDialog showContentText (boolean isShow) {
public SweetAlertDialog showContentText(boolean isShow) {
mShowContent = isShow;
if (mContentTextView != null) {
mContentTextView.setVisibility(mShowContent ? View.VISIBLE : View.GONE);
}
return this;
}

public String getCancelText () {
public String getCancelText() {
return mCancelText;
}

public SweetAlertDialog setCancelText (String text) {
public SweetAlertDialog setCancelText(String text) {
mCancelText = text;
if (mCancelButton != null && mCancelText != null) {
showCancelButton(true);
Expand All @@ -311,24 +357,24 @@ public SweetAlertDialog setCancelText (String text) {
return this;
}

public String getConfirmText () {
public String getConfirmText() {
return mConfirmText;
}

public SweetAlertDialog setConfirmText (String text) {
public SweetAlertDialog setConfirmText(String text) {
mConfirmText = text;
if (mConfirmButton != null && mConfirmText != null) {
mConfirmButton.setText(mConfirmText);
}
return this;
}

public SweetAlertDialog setCancelClickListener (OnSweetClickListener listener) {
public SweetAlertDialog setCancelClickListener(OnSweetClickListener listener) {
mCancelClickListener = listener;
return this;
}

public SweetAlertDialog setConfirmClickListener (OnSweetClickListener listener) {
public SweetAlertDialog setConfirmClickListener(OnSweetClickListener listener) {
mConfirmClickListener = listener;
return this;
}
Expand Down Expand Up @@ -376,7 +422,7 @@ public void onClick(View v) {
}
}

public ProgressHelper getProgressHelper () {
public ProgressHelper getProgressHelper() {
return mProgressHelper;
}
}
7 changes: 7 additions & 0 deletions library/src/main/res/layout/alert_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@
android:singleLine="true"
android:text="@string/dialog_default_title" />

<FrameLayout
android:id="@+id/sweet_custom_view_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:visibility="gone" />

<TextView
android:id="@+id/content_text"
android:layout_marginTop="10dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;

import cn.pedant.SweetAlert.SweetAlertDialog;
Expand All @@ -23,6 +24,7 @@ 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.custom_view_test).setOnClickListener(this);
}

@Override
Expand Down Expand Up @@ -58,15 +60,15 @@ public void onClick(View v) {
.setContentText("Won't be able to recover this file!")
.setConfirmText("Yes,delete it!")
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sDialog) {
// reuse previous dialog instance
sDialog.setTitleText("Deleted!")
.setContentText("Your imaginary file has been deleted!")
.setConfirmText("OK")
.setConfirmClickListener(null)
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
}
@Override
public void onClick(SweetAlertDialog sDialog) {
// reuse previous dialog instance
sDialog.setTitleText("Deleted!")
.setContentText("Your imaginary file has been deleted!")
.setConfirmText("OK")
.setConfirmClickListener(null)
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
}
})
.show();
break;
Expand Down Expand Up @@ -128,7 +130,7 @@ public void onClick(SweetAlertDialog sDialog) {
public void onTick(long millisUntilFinished) {
// you can change the progress bar color by ProgressHelper every 800 millis
i++;
switch (i){
switch (i) {
case 0:
pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
break;
Expand Down Expand Up @@ -160,6 +162,15 @@ public void onFinish() {
.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
}
}.start();
break;
case R.id.custom_view_test:
View view = LayoutInflater.from(this).inflate(R.layout.dialog_custom_view, null);
SweetAlertDialog dialog = new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_VIEW)
.setTitleText("Sweet!");
dialog.setCanceledOnTouchOutside(true);
dialog.show();
dialog.setCustomView(view, false);

break;
}
}
Expand Down
17 changes: 17 additions & 0 deletions sample/src/main/res/layout/dialog_custom_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please input content:" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />


</LinearLayout>
Loading