From 6d8d52df62ca53079f31bf43d602c93452ff66de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hor=C3=A1cio=20Com=C3=A9?= Date: Sat, 13 Oct 2018 13:09:22 +0200 Subject: [PATCH 1/2] add a return to all methods that alter something on the MaterialDialog. by doing this it is now possible to setup all the configuration at the same line --- .../materialdialog/MaterialDialog.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/library/src/main/java/me/drakeet/materialdialog/MaterialDialog.java b/library/src/main/java/me/drakeet/materialdialog/MaterialDialog.java index 57c2f00..24fde2e 100644 --- a/library/src/main/java/me/drakeet/materialdialog/MaterialDialog.java +++ b/library/src/main/java/me/drakeet/materialdialog/MaterialDialog.java @@ -14,15 +14,15 @@ * Created by drakeet on 9/28/14. */ public class MaterialDialog { - Context mContext; - AlertDialog mAlertDialog; - TextView mTitleView; - TextView mMessageView; - LinearLayout mButtonLayout; + private Context mContext; + private AlertDialog mAlertDialog; + private TextView mTitleView; + private TextView mMessageView; + private LinearLayout mButtonLayout; public MaterialDialog(Context context) { this.mContext = context; - mAlertDialog = new android.app.AlertDialog.Builder(mContext).create(); + mAlertDialog = new AlertDialog.Builder(mContext).create(); show(); Window window = mAlertDialog.getWindow(); window.setContentView(R.layout.layout_materialdialog); @@ -35,20 +35,24 @@ public void show() { mAlertDialog.show(); } - public void setTitle(int resId) { + public MaterialDialog setTitle(int resId) { mTitleView.setText(resId); + return this; } - public void setTitle(String title) { + public MaterialDialog setTitle(String title) { mTitleView.setText(title); + return this; } - public void setMessage(int resId) { + public MaterialDialog setMessage(int resId) { mMessageView.setText(resId); + return this; } - public void setMessage(String message) { + public MaterialDialog setMessage(String message) { mMessageView.setText(message); + return this; } /** @@ -56,7 +60,7 @@ public void setMessage(String message) { * @param text the name of button * @param listener */ - public void setPositiveButton(String text, final View.OnClickListener listener) { + public MaterialDialog setPositiveButton(String text, final View.OnClickListener listener) { Button button = new Button(mContext); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); @@ -68,6 +72,7 @@ public void setPositiveButton(String text, final View.OnClickListener listener) button.setPadding(dip2px(12), 0, dip2px(32), dip2px(16)); button.setOnClickListener(listener); mButtonLayout.addView(button); + return this; } /** @@ -75,7 +80,7 @@ public void setPositiveButton(String text, final View.OnClickListener listener) * @param text the name of button * @param listener */ - public void setNegativeButton(String text, final View.OnClickListener listener) { + public MaterialDialog setNegativeButton(String text, final View.OnClickListener listener) { Button button = new Button(mContext); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); @@ -94,6 +99,7 @@ public void setNegativeButton(String text, final View.OnClickListener listener) button.setLayoutParams(params); mButtonLayout.addView(button); } + return this; } public void dismiss() { @@ -104,4 +110,5 @@ public int dip2px( float dpValue) { final float scale = mContext.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } + } \ No newline at end of file From 24c27966ab39e593e43abadcac13edceb4ba74b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hor=C3=A1cio=20Com=C3=A9?= Date: Sat, 13 Oct 2018 13:20:40 +0200 Subject: [PATCH 2/2] illustrated how this new continuos configuration works --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 096249b..469f336 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,30 @@ mMaterialDialog.setNegativeButton("CANCLE", new View.OnClickListener() { } }); ``` + +Or + +```java +mMaterialDialog = new MaterialDialog(this); +mMaterialDialog.setTitle("MaterialDialog") + .setMessage("hello world!") + .setPositiveButton("OK", new View.OnClickListener() { + @Override + public void onClick(View v) { + mMaterialDialog.dismiss(); + ... + } + }) + .setNegativeButton("CANCLE", new View.OnClickListener() { + @Override + public void onClick(View v) { + mMaterialDialog.dismiss(); + ... + } + }) + .show(); +``` + With the first init, it will show automatedly. Or, you can init it and call the `mMaterialDialog.show()` to show the dialog simply. ## About me