Home » Android » Alternative setButton

Alternative setButton

Posted by: admin February 23, 2018 Leave a comment

Questions:

I use this code in my android project:

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });

But, Eclipse says that setButton() is deprecated. Please, help me with an alternative solution. Thanks!

Answers:
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("ALERTTILESTRING")
        .setMessage("alertNameString")
        .setCancelable(false)
        .setNegativeButton("Close",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

I hope the above code is a good one in which dialog works very well

Questions:
Answers:

setButton() isn’t what is deprecated, but that function + argument combination. There is still setButton(), but you need to give an ID for the button as the first argument for setButton():

alertDialog.setButton(0, "OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
      <do something>;
    }
});

This is useful if you want to give all your buttons the same OnClickListener:

class alertDialogOnClickListener implements DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int which) {
        switch(which){
            case 1:
                <do something>;
                break;
            case 2:
                <do something>;
                break;
        }
    }
}
alertDialog.setButton(1, "OK", new alertDialogOnClickListener());
alertDialog.setButton(2, "Cancel", new alertDialogOnClickListener());

Questions:
Answers:

Use: setButton(int whichButton, CharSequence text, DialogInterface.OnClickListener listener);

For whichButton use one of the following:

DialogInterface.BUTTON_POSITIVE 
DialogInterface.BUTTON_NEGATIVE
DialogInterface.BUTTON_NEUTRAL 

Questions:
Answers:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Alert");
            builder.setIcon(R.drawable.ic_launcher);
            builder.setMessage("Attention");
            builder.setPositiveButton("Get Location",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });

            builder.setNeutralButton("Set Location",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {



                        }
                    });

            builder.setNegativeButton("Exit",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });
            builder.show();

Questions:
Answers:

Use this and set positive or negative button

setButton(int whichButton, CharSequence text, DialogInterface.OnClickListener listener);

refer this
http://developer.android.com/reference/android/app/AlertDialog.html#setButton(int, java.lang.CharSequence, android.content.DialogInterface.OnClickListener)

Questions:
Answers:

Here’s my short and sweet one, an optimization of Android Developer. It’s more concise and uses less memory.

    (new AlertDialog.Builder(/*activity*/))
    .setTitle("ALERTTILESTRING")
    .setMessage("alertNameString")
    .setCancelable(false)
    .setNegativeButton("Close",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    })
    /*insert other Alert Dialog Builder methods here*/
    .show();

Questions:
Answers:

you can set three kind of buttons with alertDialog

.setPositiveButton("name_of_button",LISTENER);

.setNegativeButton("name_of_button",LISTENER);

.setNeutralButton("name_of_button",LISTENER);

Listener may be outside class or anonymous class like

.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            alertDialog.cancel();
        }
    });

Questions:
Answers:

Good post by Tony Stark here and you can beautify your dialog by adding an icon .. Make sure you have the picture in your drawable folder.

 builder.setTitle("Message Sent!").setCancelable(false).setNegativeButton("Close",new DialogInterface.OnClickListener() 
                            {public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});
                            AlertDialog alert = builder.create();
                            alert.setIcon(R.drawable.send);//call your image for your icon here
                            alert.show();

Example
enter image description here