This is my code for creating a Dialog Box
public void onClick(View v)
{
try{
Builder dialog= new AlertDialog.Builder(context);
dialog.setTitle(R.string.dialog_title1);
dialog.setMessage(R.string.url);
dialog.setPositiveButton(R.string.dialog_ok, null);
dialog.show();
}
I want to add a Event Listener to SetPositive Button(OK button). When OK is clicked My application should be closed i.e the user should exit from app. Can anyone help me achieving this?
When the user clicks a button, the Button object receives an on-click event. To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.
The instance of AlertDialog. Builder class call the setTitle(), setMessage(), setIcon() methods to set the dialog title, message, icon respectively. To set the action on alert dialog call the setPositiveButton(), setNeutralButton() and setNegativeButton() methods for positive, neutral and negative action respectively.
If you have more than one button click event, you can use switch case to identify which button is clicked. Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.
dialog.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
});
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