1

I need to set positive and negative buttons for custom dialog.

    public void newVisitorDialog(String title, String msg) {
    Dialog visitorDialog = new Dialog(FindVisitorMobile.this);
    visitorDialog.setCanceledOnTouchOutside(true);

    visitorDialog.setContentView(R.layout.new_visitor_dialog);
    TextView titleText = visitorDialog.findViewById(R.id.title);
    titleText.setText(title);
    TextView body = visitorDialog.findViewById(R.id.visitorData);
    body.setText(msg);
    visitorDialog.show();
}

Thanks,

Marvix
  • 179
  • 3
  • 16

5 Answers5

1

Add Negative and Positive button in Xml layout.

Find the view of your button.

Set setOnClickListener for both negative and positive button.

    Button negative = (Button) visitorDialog.findViewById(R.id.negative_btn);
    Button positive = (Button) visitorDialog.findViewById(R.id.positive_btn);

    negative.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //process your code here for negative
        }
    });

   positive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //process your code here for positive
        }
    });
Lakshman
  • 31
  • 8
0

Do this:

// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
       .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // FIRE ZE MISSILES!
           }
       })
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User cancelled the dialog
           }
       });
// Create the AlertDialog object and return it
return builder.create();
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
0

For custom dialogs you should include in your R.layout.new_dialog_visitor the two buttons.

Then in your newVisitorDialog method you find the buttons with the .findViewById and call the .setOnClickListener(..) on them.

Bagoly Sz.
  • 156
  • 7
0

If its a custom dialog you can create a whole another layout for it
Heres a link It shows how to add buttons ,textview,images to a dialog.Hope it helps

Kevin Kurien
  • 812
  • 6
  • 14
0

I found the best way is to set the dialog as a private variable in the class.

    private Dialog visitorDialog;
public void newVisitorDialog(String title, String msg) {
    visitorDialog = new Dialog(FindVisitorMobile.this);
    visitorDialog.setCanceledOnTouchOutside(true);

    visitorDialog.setContentView(R.layout.new_visitor_dialog);
    TextView titleText = visitorDialog.findViewById(R.id.title);
    titleText.setText(title);
    TextView body = visitorDialog.findViewById(R.id.visitorData);
    body.setText(msg);
    visitorDialog.show();
}

/**
 * Cancel the visitor dialog
 * @param view
 */
public void dialogCancel(View view){
    visitorDialog.dismiss();
}
Marvix
  • 179
  • 3
  • 16