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(); } });
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.
There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface.
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.
Change your alertdialog to this:
Builder alert = new AlertDialog.Builder(screen4.this); alert.setTitle("Doctor"); alert.setMessage("message"); alert.setPositiveButton("OK",null); alert.show();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With