-1

I have used AlertDialog, when click on button its going to main screen. I am not getting alert box?

 alertDialog.setPositiveButton(R.id.button1, new 
    DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "save me**********************", Toast.LENGTH_SHORT).show();
            Toast.makeText(getActivity(), "you have pressed save", Toast.LENGTH_SHORT).show();

        }
    });

alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
nobalG
  • 4,544
  • 3
  • 34
  • 72

1 Answers1

0

Try this

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle(Your Title);

alertDialogBuilder.setMessage(Your Message);

// set positive button: Yes message

alertDialogBuilder.setPositiveButton(R.id.button1,new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog,int id) {

        //Your Code Here
    }
});

// set negative button: No message

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

    public void onClick(DialogInterface dialog,int id) {

        // cancel the alert box and put a Toast to the user
        dialog.cancel();

        //Your Code Here

    }

});
AlertDialog alertDialog = alertDialogBuilder.create();

// show alert

alertDialog.show();
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Hafiz.M.Usman
  • 223
  • 3
  • 21