Search code examples
androidandroid-layoutandroid-alertdialog

Why is there no gap between my two AlertDialog buttons?


In my AlertDialog, my Positive Button and Negative Button are "attached." I'm pretty sure there should be a gap between them. Can someone tell me why this is happening? I would be happy to provide any code. Here is what my AlertDialog looks like.

I have a custom View for the Body as well as the Title of my AlertDialog (I won't post that XML code because I don't think it's necessary, but let me know.) In MainActivity, I inflate my custom title and body Views, and override setPositive() and setNegative(), then I customize the color of my buttons using onShow().

Sorry for the convoluted code, but help would be much appreciated :). Here is my MainActivity:

public void openPrompt(View view){
    //builds and opens custom view with prompt.XML
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    EditText input = (EditText)promptView.findViewById(R.id.userInput);

    builder.setCancelable(true).setView(R.layout.customdialoglayout)
    .setNegativeButton("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();
        }
    });


    //set title with custom XML layout view
    LayoutInflater inflater = getLayoutInflater();
    View titleView = inflater.inflate(R.layout.cutomtitlebar,null);
    builder.setCustomTitle(titleView);

    AlertDialog ad = builder.create();

    //change colors of background and 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));

            Button posButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            posButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton));
            posButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText));
        }
    });


    ad.show();


}

EDIT Here is my XML I used for setView():

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="CUSTOM TEXT"
    android:id="@+id/textView3"
    android:layout_gravity="center"/>


Solution

  • 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);
            }
        });