2

I have definition below. This is AlertDialog with timer. I need add button, how do it properly? Method setButton is deprecated. Any Help?

alertDialog = new AlertDialog.Builder(this).create();  
    alertDialog.setTitle("Alert 3");  
    alertDialog.setMessage("00:10");
    alertDialog.show();   // 

new CountDownTimer(10000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
       alertDialog.setMessage("00:"+ (millisUntilFinished/1000));
    }

    @Override
    public void onFinish() {
        info.setVisibility(View.GONE);
    }
}.start();
  • Maybe this can help [link](http://stackoverflow.com/questions/8227820/alert-dialog-two-buttons) – Sanjeev Jul 20 '15 at 13:59

2 Answers2

2

In the documentation

Use setButton(int, CharSequence, Message) with BUTTON_POSITIVE.

Sandro Machado
  • 9,921
  • 4
  • 36
  • 57
1

If you go throught Alert Dialog Documentation, you'll see that on setButton() says this :

This method was deprecated in API level 3. Use setButton(int, CharSequence, Message) with BUTTON_POSITIVE.

So, the easiest way to add a button in an Alert Dialog is :

Positive Button

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
     //Something you want to happen when user click
  }
});

Negative Button

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int id) {
        //Something you want to happen when user click
    }
});

For more info, see setButton (CharSequence text, DialogInterface.OnClickListener listener) documentation.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148