Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ok Button for Alert Dialog on Android

This is my code for an alert, it shows a message when a button is pressed. How can I make an OK Button to 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 doesn't show any button to dismiss.

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();         }     }); 
like image 299
berk kaan Avatar asked Jan 18 '13 11:01

berk kaan


People also ask

What is alert dialog in Android?

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.

What are the three buttons that can be added to a dialog?

There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface.

What are dialog Buttons?

Dialog controls are modal UI overlays that provide contextual app information. They block interactions with the app window until being explicitly dismissed. They often request some kind of action from the user.


2 Answers

Change your alertdialog to this:

Builder alert = new AlertDialog.Builder(screen4.this); alert.setTitle("Doctor"); alert.setMessage("message"); alert.setPositiveButton("OK",null); alert.show();     
like image 83
Misha Bhardwaj Avatar answered Sep 16 '22 11:09

Misha Bhardwaj


You can create both OK and cancel button for dialog using this,

AlertDialog.Builder builder = new AlertDialog.Builder(                         getApplicationContext());                 builder.setCancelable(true);                 builder.setTitle("Title");                 builder.setInverseBackgroundForced(true);                 builder.setPositiveButton("Yes",                         new DialogInterface.OnClickListener() {                             @Override                             public void onClick(DialogInterface dialog,                                     int which) {                                 dialog.dismiss();                             }                         });                 builder.setNegativeButton("No",                         new DialogInterface.OnClickListener() {                             @Override                             public void onClick(DialogInterface dialog,                                     int which) {                                 dialog.dismiss();                             }                         });                 AlertDialog alert = builder.create();                 alert.show(); 
like image 28
Sahil Mahajan Mj Avatar answered Sep 17 '22 11:09

Sahil Mahajan Mj