Is possible to customize positive and negative buttons in AlertDialog ? I need to replace default look of positive and negative with custom.
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...
Can somebody tell me how to do that ?
There are two ways to change the dialog button color. I'll recommend adding a theme for AlertDialog in styles. xml so you don't have to write the same code again and again in each activity/dialog call. You can just create a style and apply that theme on the dialog box.
An alert dialog can have maximum three action buttons. If you want the user to accept the action, use Positive action button. It is normally displayed as OK/YES. If the user wants to cancel the action , then you can use Negative action button (NO).
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog. Here setNeutralButton() is used to create a neutral cancel button.
public class ComentarDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Mensaje de alerta")
.setTitle("Comentar")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
@Override
public void onStart() {
super.onStart();
//Personalizamos
Resources res = getResources();
//Buttons
Button positive_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
Button negative_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
int color = Color.parseColor("#304f5a");
//Title
int titleId = res.getIdentifier("alertTitle", "id", "android");
View title = getDialog().findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(color);
}
//Title divider
int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
View titleDivider = getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(color);
}
}
}
Yuo can set every view in dialog box. You can set view with two buttons and dont set positive & negative buttons.
example:
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
View dialogView = LayoutInflater.from(this)
.inflate(R.layout.my_layout, null);
builder.setView(dialogView);
if you want to customize you can use dialog instead of alert dialog here is the sample code
final Dialog dialog = new Dialog(ThisweekActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
View view = LayoutInflater.from(ThisweekActivity.this).inflate(R.layout.issue_cover_prompt_layout, null);
view.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
ImageView img = (ImageView) view.findViewById(R.id.issue_cover_img);
img.setImageBitmap(issue.getCoverImage());
dialog.setContentView(view);
dialog.show();
you can set set the layout in dialog and add click listner on it
if you want to change them, I will suggest using activity with your required layout and add them= Dialog in your activity, in Manifest file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With