66import android .content .res .Configuration ;
77import android .content .res .TypedArray ;
88import android .os .Build ;
9+ import android .text .Html ;
10+ import android .text .Spanned ;
911import android .view .LayoutInflater ;
1012import android .view .View ;
1113import android .view .ViewGroup ;
2426import dev .shreyaspatil .MaterialDialog .interfaces .OnDismissListener ;
2527import dev .shreyaspatil .MaterialDialog .interfaces .OnShowListener ;
2628import dev .shreyaspatil .MaterialDialog .model .DialogButton ;
29+ import dev .shreyaspatil .MaterialDialog .model .DialogText ;
30+ import dev .shreyaspatil .MaterialDialog .model .TextAlignment ;
2731
2832@ SuppressWarnings ("unused" )
29- public class AbstractDialog implements DialogInterface {
33+ public abstract class AbstractDialog implements DialogInterface {
3034
3135 //Constants
3236 public static final int BUTTON_POSITIVE = 1 ;
@@ -36,23 +40,25 @@ public class AbstractDialog implements DialogInterface {
3640
3741 protected Dialog mDialog ;
3842 protected Activity mActivity ;
39- protected String title ;
40- protected String message ;
43+ protected DialogText title ;
44+ protected DialogText message ;
4145 protected boolean mCancelable ;
4246 protected DialogButton mPositiveButton ;
4347 protected DialogButton mNegativeButton ;
4448 protected int mAnimationResId ;
4549 protected String mAnimationFile ;
4650 protected LottieAnimationView mAnimationView ;
4751
52+ protected TextAlignment mTitleTextAlignment ;
53+ protected TextAlignment mMessageTextAlignment ;
54+
4855 protected OnDismissListener mOnDismissListener ;
4956 protected OnCancelListener mOnCancelListener ;
5057 protected OnShowListener mOnShowListener ;
5158
52-
5359 protected AbstractDialog (@ NonNull Activity mActivity ,
54- @ NonNull String title ,
55- @ NonNull String message ,
60+ @ NonNull DialogText title ,
61+ @ NonNull DialogText message ,
5662 boolean mCancelable ,
5763 @ NonNull DialogButton mPositiveButton ,
5864 @ NonNull DialogButton mNegativeButton ,
@@ -83,15 +89,23 @@ protected View createView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
8389 // Set Title
8490 if (title != null ) {
8591 mTitleView .setVisibility (View .VISIBLE );
86- mTitleView .setText (title );
92+ mTitleView .setText (title .getText ());
93+ mTitleView .setTextAlignment (title .getTextAlignment ().getAlignment ());
8794 } else {
8895 mTitleView .setVisibility (View .GONE );
8996 }
9097
9198 // Set Message
9299 if (message != null ) {
93100 mMessageView .setVisibility (View .VISIBLE );
94- mMessageView .setText (message );
101+ Spanned spannedMessage = null ;
102+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .N ) {
103+ spannedMessage = Html .fromHtml (message .getText (), Html .FROM_HTML_MODE_COMPACT );
104+ } else {
105+ spannedMessage = Html .fromHtml (message .getText ());
106+ }
107+ mMessageView .setText (spannedMessage );
108+ mMessageView .setTextAlignment (message .getTextAlignment ().getAlignment ());
95109 } else {
96110 mMessageView .setVisibility (View .GONE );
97111 }
@@ -329,4 +343,157 @@ private void throwNullDialog() {
329343 public interface OnClickListener {
330344 void onClick (DialogInterface dialogInterface , int which );
331345 }
346+
347+ /**
348+ * Builder for {@link AbstractDialog}.
349+ */
350+ public static abstract class Builder <D extends AbstractDialog > {
351+ protected final Activity activity ;
352+ protected DialogText title ;
353+ protected DialogText message ;
354+ protected boolean isCancelable ;
355+ protected DialogButton positiveButton ;
356+ protected DialogButton negativeButton ;
357+ protected int animationResId = NO_ANIMATION ;
358+ protected String animationFile ;
359+
360+ /**
361+ * @param activity where Material Dialog is to be built.
362+ */
363+ public Builder (@ NonNull Activity activity ) {
364+ this .activity = activity ;
365+ }
366+
367+ /**
368+ * @param title Sets the Title of Material Dialog with the default alignment as center.
369+ * @return this, for chaining.
370+ */
371+ @ NonNull
372+ public Builder <D > setTitle (@ NonNull String title ) {
373+ return setTitle (title , TextAlignment .CENTER );
374+ }
375+
376+ /**
377+ * @param title Sets the Title of Material Dialog.
378+ * @param alignment Sets the Alignment for the title.
379+ * @return this, for chaining.
380+ */
381+ @ NonNull
382+ public Builder <D > setTitle (@ NonNull String title , @ NonNull TextAlignment alignment ) {
383+ this .title = new DialogText (title , alignment );
384+ return this ;
385+ }
386+
387+ /**
388+ * @param message Sets the Message of Material Dialog with the default alignment as center.
389+ * @return this, for chaining.
390+ */
391+ @ NonNull
392+ public Builder <D > setMessage (@ NonNull String message ) {
393+ return setMessage (message , TextAlignment .CENTER );
394+ }
395+
396+ /**
397+ * @param message Sets the Message of Material Dialog.
398+ * @param alignment Sets the Alignment for the message.
399+ * @return this, for chaining.
400+ */
401+ @ NonNull
402+ public Builder <D > setMessage (@ NonNull String message , @ NonNull TextAlignment alignment ) {
403+ this .message = new DialogText (message , alignment );
404+ return this ;
405+ }
406+
407+ /**
408+ * @param isCancelable Sets cancelable property of Material Dialog.
409+ * @return this, for chaining.
410+ */
411+ @ NonNull
412+ public Builder <D > setCancelable (boolean isCancelable ) {
413+ this .isCancelable = isCancelable ;
414+ return this ;
415+ }
416+
417+ /**
418+ * Sets the Positive Button to Material Dialog without icon
419+ *
420+ * @param name sets the name/label of button.
421+ * @param onClickListener interface for callback event on click of button.
422+ * @return this, for chaining.
423+ */
424+ @ NonNull
425+ public Builder <D > setPositiveButton (@ NonNull String name , @ NonNull OnClickListener onClickListener ) {
426+ return setPositiveButton (name , NO_ICON , onClickListener );
427+ }
428+
429+ /**
430+ * Sets the Positive Button to Material Dialog with icon
431+ *
432+ * @param name sets the name/label of button.
433+ * @param icon sets the resource icon for button.
434+ * @param onClickListener interface for callback event on click of button.
435+ * @return this, for chaining.
436+ */
437+ @ NonNull
438+ public Builder <D > setPositiveButton (@ NonNull String name , int icon , @ NonNull OnClickListener onClickListener ) {
439+ positiveButton = new DialogButton (name , icon , onClickListener );
440+ return this ;
441+ }
442+
443+ /**
444+ * Sets the Negative Button to Material Dialog without icon.
445+ *
446+ * @param name sets the name/label of button.
447+ * @param onClickListener interface for callback event on click of button.
448+ * @return this, for chaining.
449+ */
450+ @ NonNull
451+ public Builder <D > setNegativeButton (@ NonNull String name , @ NonNull OnClickListener onClickListener ) {
452+ return setNegativeButton (name , NO_ICON , onClickListener );
453+ }
454+
455+ /**
456+ * Sets the Negative Button to Material Dialog with icon
457+ *
458+ * @param name sets the name/label of button.
459+ * @param icon sets the resource icon for button.
460+ * @param onClickListener interface for callback event on click of button.
461+ * @return this, for chaining.
462+ */
463+ @ NonNull
464+ public Builder <D > setNegativeButton (@ NonNull String name , int icon , @ NonNull OnClickListener onClickListener ) {
465+ negativeButton = new DialogButton (name , icon , onClickListener );
466+ return this ;
467+ }
468+
469+ /**
470+ * It sets the resource json to the {@link com.airbnb.lottie.LottieAnimationView}.
471+ *
472+ * @param animationResId sets the resource to {@link com.airbnb.lottie.LottieAnimationView}.
473+ * @return this, for chaining.
474+ */
475+ @ NonNull
476+ public Builder <D > setAnimation (@ RawRes int animationResId ) {
477+ this .animationResId = animationResId ;
478+ return this ;
479+ }
480+
481+ /**
482+ * It sets the json file to the {@link com.airbnb.lottie.LottieAnimationView} from assets.
483+ *
484+ * @param fileName sets the file from assets to {@link com.airbnb.lottie.LottieAnimationView}.
485+ * @return this, for chaining.
486+ */
487+ @ NonNull
488+ public Builder <D > setAnimation (@ NonNull String fileName ) {
489+ this .animationFile = fileName ;
490+ return this ;
491+ }
492+
493+ /**
494+ * Builds the dialog.
495+ */
496+ @ NonNull
497+ public abstract D build ();
498+ }
332499}
0 commit comments