Reputation: 35559
I have displayed AlertDialog
with 2 buttons i.e. Yes and Cancel, here is the code.
final AlertDialog.Builder builder = new AlertDialog.Builder(RiderDetailActivity.this);
builder.setCancelable(false);
builder.setMessage("Are you sure?");
final AlertDialog dialog = builder.create();
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
dialog.show();
This doesn't show yes and cancel button only on samsung galaxy s4
But when i use it with with dialog.setButton
it works perfectly
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
Note : builder.setPositiveButton
and builder.setNegativeButton
is creating problem on just Samsung Galaxy S4, for other devices it is working perfectly.
I need to create dialog after setting buttons to builder, but my concern is why it was working with other devices and just creating problem with Samsung Galaxy S4?
Upvotes: 5
Views: 7290
Reputation:
For example me, I didn't have my button showing on AlertDialog when was setting message to empty String ("").
Upvotes: 0
Reputation: 155
I get that error after call Builder.create(); before positive and negative button. So I do this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_custom_statistic, null);
builder.setView(view);
builder.setCancelable(false);
// Initialize view
...
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
builder.create().dismiss();
}
});
builder.setNegativeButton("Cancel", null);
final AlertDialog dialog = builder.create();
dialog.show();
// This for edit color and caps text
TextView ok = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
TextView no = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
ok.setTextColor(getResources().getColor(R.color.purple_200));
ok.setAllCaps(false);
no.setTextColor(getResources().getColor(R.color.purple_200));
no.setAllCaps(false);
Upvotes: 0
Reputation: 1568
You can try with this code..
new AlertDialog.Builder(this)
.setTitle("Add new Title")
.setMessage("Type Message here")
.setPositiveButton("Add", (dialog, whichButton) -> {
// Onclick event Here
})
.setNegativeButton("Close",(dialog, i) -> {
})
.show();
Upvotes: 0
Reputation: 18933
After your alertDialog.show()
method add these two lines
alertDialog.getButton(alertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(_context, R.color.red));
alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(_context, R.color.red));
Upvotes: 5
Reputation: 1604
Try this way. I hope this will work for you.
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Are you sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
Upvotes: 5
Reputation: 445
You are creating your Dialog before adding the buttons.
Just move
final AlertDialog dialog = builder.create();
after setting your buttons
Upvotes: 2