Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick Listener for OK button

Tags:

android

dialog

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?

like image 941
suraj Avatar asked Mar 07 '12 04:03

suraj


People also ask

How to set onClick method in android Studio?

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.

How to use AlertDialog in Kotlin?

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.

How do I use OnClickListener?

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.


1 Answers

dialog.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
    MyActivity.this.finish();         
  }
});
like image 94
TotoroTotoro Avatar answered Sep 20 '22 09:09

TotoroTotoro