Search code examples
androidandroid-alertdialog

I want to show OK and cancel button in my alert dialog?


I want to show ok and cancel button in my alert dialog.I tried many solutions but didnt succeede..guide me plese..thanks

AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Check Your Internet Connection!! you are not connected to the Internet..");

            AlertDialog alert = builder.create();
                             alert.show();} 

Solution

  • AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setView(alertDialogView);
    adb.setTitle("Title of alert dialog");
    adb.setIconAttribute(android.R.attr.alertDialogIcon);
    adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);
            Toast.makeText(Tutoriel18_Android.this, et.getText(),
                    Toast.LENGTH_SHORT).show();
        }
    });
    adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    adb.show();