Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"/>

like image 394
Jakob Klemm Avatar asked Jun 21 '16 18:06

Jakob Klemm


People also ask

How many buttons can be set up on an AlertDialog?

Dialog Design AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

What's the difference between dialog and AlertDialog in Android?

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 .


1 Answers

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();
    }
});
like image 75
tinoe Avatar answered Oct 04 '22 01:10

tinoe