How to put two buttons in an alert box

I have created a simple alert box program in android.now I need to put two OK and Cancel buttons, but when I run the program it only shows the Cancel button ... my code is like below:

Main.java

public class MainActivity extends Activity {
Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button);
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        alertDialog.setTitle("Title");
        alertDialog.setMessage("Message");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
              // TODO Add your code for the button here.
               Toast.makeText(getApplicationContext(), "well come", 1).show();
           }
        });
        alertDialog.setButton("cancel",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
            }
        });
        // Set the Icon for the Dialog
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
        // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button  
    }
});
    }
}

      

thank you in advance.

0


source to share


7 replies


I had the same problem. This is what has been done.

public class MainActivity extends Activity {
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.button);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            alertDialog.setTitle("Title");
            alertDialog.setMessage("Message");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                   Toast.makeText(getApplicationContext(), "well come", 1).show();
               }
            });
        alertDialog.setButton2("cancel",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
            }
        });
        // Set the Icon for the Dialog
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
        // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button  
    }
});
    }
}

      



This works great. You must dial the buttons for the warning.

+4


source


change like this: setPositiveButton

ok

and setNegativeButton

there are buttons cancel

.



 final AlertDialog.Builder alertDialog= new AlertDialog.Builder(this);

 alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
          // TODO Add your code for the button here.
           Toast.makeText(getApplicationContext(), "well come", 1).show();
       }
    });
    alertDialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
        }
    });

      

+1


source


Correct way to add buttons:

For positive

alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
        // perform your action
    }
});

      

Negative

alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
        // if this button is clicked, just close
        // the dialog box and do nothing
        dialog.cancel();
    }
});

      

Android Alert Dialog Example

+1


source


Simple alert

 private AlertDialog AskOption()
 {
    AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this) 
        //set message, title, and icon
        .setTitle("Title") 
        .setMessage("Message") 
        .setIcon(R.drawable.icon)

        .setPositiveButton("yes", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) { 

                //your code
            }   
        })

        .setNeutralButton("No", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 

                dialog.dismiss();
         } 
        })

        .create();
        return myQuittingDialogBox;

    }

      

Using

AlertDialog al = AskOption();
al.show();

      

+1


source


public void showDialog(Activity activity, String title, CharSequence message) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
if (title != null)
    builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", null);
        builder.setNegativeButton("Cancel", null);
builder.show();

      

}

0


source


You can use the AlertDialog.Builder class. There are several ways to place a button on a dialog using this builder class.

0


source


you need to use setPositiveButton () and setnegativeButton () like,

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button);
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        alertDialog.setTitle("Title");
        alertDialog.setMessage("Message");
        alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                     Toast.makeText(getApplicationContext(), "well come", 1).show();
                }
              });
        alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                     Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
                }
            });
        // Set the Icon for the Dialog
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
        // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button  
    }
});
    }
}

      

0


source







All Articles