I have an activity – a form with some TextFields and the user clicks a submit button after filling all the fields. When the user touches the submit button, it should show an alert dialog, and then when user touches OK, the rest of the OnClicklistener code will execute. Currently, my code is something like this.
listener for submit/finish button:
private final OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
displayAlert();
// Some other things to do here. Lets say showing some other activity
};
Alert dialog code:
public void displayAlert(){
new AlertDialog.Builder(this).setMessage("Hi , I am Alert Dialog")
.setTitle("My Alert")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
}
I am getting strange output. When I click the submit/finish button, it shows me the alert dialog but it disappears before I press touch the OK button. Why?
The code that you wish to execute after the user clicks OK should be moved to the onClick method of the AlertDialog. Right after
finish(). Otherwise it will begin executing while the dialog box is showing.