Ok Button for Alert Dialog on Android

berk kaan picture berk kaan · Jan 18, 2013 · Viewed 74.2k times · Source

This is my code for an alert, it shows a message when button is pressed. How can i make an OK Button for dismiss?

    Button bm1 = (Button) findViewById(R.id.button1);
    bm1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setCancelMessage(null);
            alert.show();
        }
    });
}}      

I have put alert.setCancelMessage(null) but it doesnt show any button to dismiss.

Thanks for all answers.

Solution:

      Button bm1 = (Button) findViewById(R.id.button1);
       bm1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Builder alert = new AlertDialog.Builder(screen4.this);
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setPositiveButton("OK", null);
            alert.show();
        }
    });

Answer

Misha Bhardwaj picture Misha Bhardwaj · Jan 18, 2013

Change your alertdialog to this:

Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show();