Why is there no gap between my two AlertDialog buttons?
Another way to do this for all your AlertDialog's in your app is to change the styles.xml
Add a custom theme to your AlertDialog's
//Kotlin code
val alertDialog = AlertDialog.Builder(this, R.style.AlertDialogCustom))
//Java Code
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
in the styles.xml
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
//whichever parent theme you want to use here
//add these 2 to allow all APIs
<item name="buttonBarPositiveButtonStyle">@style/PositiveAlertButtonStyle</item>
<item name="android:buttonBarPositiveButtonStyle">@style/PositiveAlertButtonStyle</item>
//change the marginStart of the Positive button to put a gap between the buttons
//you can also change text color and background etc. here
<style name="PositiveAlertButtonStyle">
<item name="android:layout_marginStart">10dp</item>
Why should there be a gap between them? The positive and negative buttons get their layout dimensions from the AlertDialog
class and does not, from what I can recall, have any margin in between the buttons.
In order to add a margin, you can either make your own buttons and not use the positive and negative buttons from the AlertDialog
or you can add margin to the buttons in a similar manner that you styled the buttons.
ad.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Context context = MainActivity.this;
Window view = ((AlertDialog)dialog).getWindow();
view.setBackgroundDrawableResource(R.color.colorPrompt);
Button negButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
negButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton));
negButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(20,0,0,0);
negButton.setLayoutParams(params);
}
});
Why don't we improvise if we need a gap between your buttons? Instead of using the negative button you can use the neutral button. This is because the positive and the negative are inseparable, But the neutral and positive works fine one in the far right side and one in the far left side. So just change negative button to neutral and it works. See code below:
AlertDialog.Builder builder = new AlertDialog.Builder(preview.this);
builder.setTitle("Title here");
builder.setMessage("message on dialog here");
builder.setCancelable(true);
builder.setNeutralButton("Name of button N", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//your code here
}
});
builder.setPositiveButton("Name of button P", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//your code here
}
});
This is an example which is working perfectly to me, from your code you can edit it like below:
builder.setCancelable(true).setView(R.layout.customdialoglayout)
.setNeutralButton("One", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"CANCEL clicked",Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("Two", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"SET clicked",Toast.LENGTH_SHORT).show();
}
});