Skip to content

Allowing continuos setup of dialog #1

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

Open
wants to merge 2 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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 19 additions & 12 deletions library/src/main/java/me/drakeet/materialdialog/MaterialDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -35,28 +35,32 @@ 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;
}

/**
* set positive button
* @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);
Expand All @@ -68,14 +72,15 @@ 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;
}

/**
* set negative button
* @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);
Expand All @@ -94,6 +99,7 @@ public void setNegativeButton(String text, final View.OnClickListener listener)
button.setLayoutParams(params);
mButtonLayout.addView(button);
}
return this;
}

public void dismiss() {
Expand All @@ -104,4 +110,5 @@ public int dip2px( float dpValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

}