Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog setButton was deprecated

I use this code in my Eclipse Android project

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

        }
});

But Eclipse is saying:

This method was deprecated in API level 3. Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_POSITIVE

UPDATE AFTER 5 YEARS

This was my first step on entering a real programming adventure. It was like, oh I know how to do html website and this must be easy to code -native programming- so lets jump into it without reading any document. This is my first question on this awesome community which is a terrible question. One big suggestion that I can give to any beginner programmer which is; have some idea, dig some document and read open source about how others made before you jump into a new adventure. Patiently read the error that you encounter and try to realise what the real problem is. Thus you don't ask unnecessary question.

like image 735
Ataberk Avatar asked May 03 '15 19:05

Ataberk


People also ask

Is AlertDialog deprecated?

This method is deprecated.

What is the significance of AlertDialog builder?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

How many styles of Alertdialogs are there?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)


1 Answers

Java

AlertDialog alert = new AlertDialog.Builder(this).create();
            alert.setTitle("Error");
            alert.setMessage("Sorry, your device doesn't support flash light!");
            alert.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });

alert.show();

Kotlin

    var alert: AlertDialog = AlertDialog.Builder(this).create()
    alert.setTitle("Error")
    alert.setMessage("Sorry, your device doesn't support flash light!")
    alert.setButton(Dialog.BUTTON_POSITIVE, "OK", DialogInterface.OnClickListener {
            //do your own idea.
            dialog, which -> finish() })
    alert.show()
like image 142
Shahzad Afridi Avatar answered Sep 30 '22 13:09

Shahzad Afridi