Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative setButton

Tags:

android

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!

like image 770
Myroslav Zozulia Avatar asked Nov 07 '12 10:11

Myroslav Zozulia


2 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

like image 103
Venkatesh S Avatar answered Oct 08 '22 20:10

Venkatesh S


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());
like image 42
Mahnster Avatar answered Oct 08 '22 21:10

Mahnster