-2

I'd rather not use Alert Dialog, but I will if I can set the positive Button to be the button I already have. If I can't do that, is there a way to set positive and negative Buttons in a custom dialog?

Carter Ray
  • 151
  • 1
  • 9
  • You do realize that AlertDialog has the ability to a custom content view via setContentView https://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(int) ? Then just link the button to your view so you can listen to the button being hit. – JoxTraex Mar 24 '17 at 02:35
  • @JoxTraex yes i know, but i couldn't tell it which button i wanted to be the positive button. – Carter Ray Mar 24 '17 at 23:57

1 Answers1

0

You can use the following custom alert dialog.

public class CustomAlertDialog {

    public void showDialog(Context activity, String msg, String buttonText, final CustomDialogListener customDialogListener){ //one button with callback
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.custom_alert);

        TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
        text.setText(msg);

        Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
        dialogButton.setText(buttonText);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                customDialogListener.onPositiveButtonClick();
            }
        });

        dialog.show();

    }

    public void showDialog(Context activity, String msg, String positiveButtonText, String negativeButtonText, final CustomDialogListener customDialogListener){//two button with callback
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.custom_alert_two_button);

        TextView text = (TextView) dialog.findViewById(R.id.text_alert_two_dialog);
        text.setText(msg);

        Button positiveDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_YES);
        Button negativeDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_NO);
        positiveDialogButton.setText(positiveButtonText);
        negativeDialogButton.setText(negativeButtonText);

        positiveDialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                customDialogListener.onPositiveButtonClick();
            }
        });

        negativeDialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                customDialogListener.onNegativeButtonClick();
            }
        });

        dialog.show();

    }


    public void showDialog(Context activity, String msg, String buttonText){ //simple alert without callback
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.custom_alert);

        TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
        text.setText(msg);

        Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
        dialogButton.setText(buttonText);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }

}

The custom_alert.xml contains one button and one textView to display the message.

The custom_alert_two_button.xml contains two buttons and one textView to display the message.

Last one contains only one textview to display message.

Ruthwik
  • 539
  • 1
  • 4
  • 5