PrettyDialog
PrettyDialog is an Android Dialog library which is the customizable equivalent of SCLAlertView in iOS.
Example is available in app module.
Download
Gradle:
Add the following to your project level build.gradle:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
Add this to your app build.gradle:
dependencies {
compile 'com.github.mjn1369:prettydialog:1.0.2'
}
Usage
PrettyDialog extends Dialog class, so feel free to use its inherited functions.
Note: Default dialog has no title, message or any buttons. Just a close icon on top which you can dismiss the dialog by clicking on it.
Simple Dialog, No Customization:
new PrettyDialog(this)
.setTitle("PrettyDialog Title")
.setMessage("PrettyDialog Message")
.show();
Output:
Change Icon:
- You can set the dialog icon resource:
.setIcon(R.drawable.pdlg_icon_info)
- And set a color tint for it:
.setIconTint(R.color.pdlg_color_green)
- Ultimately, define an OnClick callback:
.setIconCallback(new PrettyDialogCallback() {
@Override
public void onClick() {
// Do what you gotta do
}
})
- Put them all together:
.setIcon(
R.drawable.pdlg_icon_info, // icon resource
R.color.pdlg_color_green, // icon tint
new PrettyDialogCallback() { // icon OnClick listener
@Override
public void onClick() {
// Do what you gotta do
}
})
Output:
Add Buttons:
- You can add unlimited customized buttons to dialog:
// OK button
.addButton(
"OK", // button text
R.color.pdlg_color_white, // button text color
R.color.pdlg_color_green, // button background color
new PrettyDialogCallback() { // button OnClick listener
@Override
public void onClick() {
// Do what you gotta do
}
}
)
// Cancel button
.addButton(
"Cancel",
R.color.pdlg_color_white,
R.color.pdlg_color_red,
new PrettyDialogCallback() {
@Override
public void onClick() {
// Dismiss
}
}
)
// 3rd button
.addButton(
"Option 3",
R.color.pdlg_color_black,
R.color.pdlg_color_gray,
null
);
Output:
Note: To Dismiss PrettyDialog on a button click, you have to instantiate PrettyDialog and keep the variable, then call dismiss() on the variable inside button's onClickListener method:
PrettyDialog pDialog = new PrettyDialog(this);
pDialog
.setTitle("PrettyDialog Title")
.setMessage("PrettyDialog Message")
.addButton(
"Cancel",
R.color.pdlg_color_white,
R.color.pdlg_color_red,
new PrettyDialogCallback() {
@Override
public void onClick() {
pDialog.dismiss();
}
}
)
.show();
Custom Title, Message and Typeface:
Note: Typeface applies to all texts inside the dialog.
.setTitle("Do you agree?")
.setTitleColor(R.color.pdlg_color_blue)
.setMessage("By agreeing to our terms and conditions, you agree to our terms and conditions.")
.setMessageColor(R.color.pdlg_color_gray)
.setTypeface(Typeface.createFromAsset(getResources().getAssets(),"myfont.otf"))
Output:
- Enable/Disable dialog animation:
.setAnimationEnabled(true)
- Set dialog gravity:
.setGravity(Gravity.BOTTOM)
Functions
Functions | Description | Default |
---|---|---|
setTitle(String) | sets a title for dialog | "" |
setTitleColor(int) | sets title's color | #212121 (kinda black) |
setMessage(String) | sets a message for dialog | "" |
setMessageColor(int) | sets message's color | #212121 (kinda black) |
setIcon(int) | sets the dialog's icon | "close (X)" icon |
setIconTint(int) | sets tint for dialog's icon | #1976D2 (kinda blue) |
setIconCallback(PrettyDialogCallback) | sets dialog's icon callback | dismiss dialog |
setIcon(int,int,PrettyDialogCallback) | sets icon,icon tint,icon callback | |
addButton(String,int,int,PrettyDialogCallback) | adds button with text,text color,background color and callback | |
setTypeface(Typeface) | sets typeface for all texts | |
setAnimationEnabled(boolean) | enables or disables dialog animation | Enabled |
setGravity(int) | sets the dialog's gravity (TOP, BOTTOM, CENTER,...) | Gravity.CENTER |