1

I am new to an android studio and previously was coding in swift. I tried to look into creating an alert view in an android studio but most of them don't give me more than 2 alert selections. In swift, I would be doing something like :

 @IBAction func showAlertButtonTapped(_ sender: UIButton) {

    // create the alert
    let alert = UIAlertController(title: "Hi there", message: "You have three selections to choose from", preferredStyle: UIAlertControllerStyle.alert)

    // the actions and handler to decide what happens when user clicks on it
    alert.addAction(UIAlertAction(title: "Selection 1", style: UIAlertActionStyle.default, handler: nil))
    alert.addAction(UIAlertAction(title: "Selection 2", style: UIAlertActionStyle.default, handler: nil))
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))

    // show the alert
    self.present(alert, animated: true, completion: nil)
}

How am I able to do it in the android studio?? Any help is appreciated. Below is what I tried, but stuck after this as I can't put another alert

AlertDialog alertDialog = new AlertDialog.Builder(MyActivity.this).create();
    alertDialog.setTitle("Hey there!");
    alertDialog.setMessage("You have three selections to choose from");
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Selection 1",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
    alertDialog.show();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
William Loke
  • 377
  • 1
  • 4
  • 25

2 Answers2

3

this is alert dialog with 3 button

    public class MainActivity extends AppCompatActivity {

    public void showAlertDialogButtonClicked(View view) {

        // setup the alert builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Notice");
        builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");

        // add the buttons
        builder.setPositiveButton("Launch missile", null);
        builder.setNeutralButton("Remind me later", null);
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });

        // create and show the alert dialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
  • thank you, got it! so the "null", i would replace with the code that i intend to run whenever user click the selection yea? – William Loke Aug 12 '18 at 10:59
  • 1
    yes replace null with: new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } } – Farshad Asgharzadeh Aug 12 '18 at 11:15
1

Maybe this code snipped could help you.

    AlertDialog.Builder b=  new  AlertDialog.Builder(getActivity())
.setTitle("Enter Players")
.setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // do something...
        }
    }
)
.setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    }
);

taken from: Adding positive / negative Button to DialogFragment's Dialog

josxha
  • 1,110
  • 8
  • 23
  • Hi! thx for the reply! i did try look into this but in this, only 2 selections, is there any ways I can add a third one? – William Loke Aug 12 '18 at 10:49