I created AlertDialog that contains 4 buttons
OptionDialog = new AlertDialog.Builder(this);
OptionDialog.setTitle("Options");
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.options, null, false);
background = (Button) v.findViewById(R.id.bkgSpinnerLabel);
SoundVib = (Button) v.findViewById(R.id.SoundVibSpinnerLabel);
OptionDialog.setView(v);
OptionDialog.setCancelable(true);
OptionDialog.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
background.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SetBackground();
// here I want to dismiss it after SetBackground() method
}
});
SoundVib.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent soundVibIntent = new Intent(SebhaActivity.this, EditPreferences.class);
startActivity(soundVibIntent);
}
});
OptionDialog.show();
I want to dismiss it after SetBackground() method, how can I do this? thanks in advance.
AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.
Android AlertDialog class is used to display a dialog box to alert the user with positive and negative buttons. Positive button is used to continue with the action specified. Negative button is used to dismiss the alerted action.
I use onCreateDialog(int id) to create each dialog and I use showDialog(int id) and dismissDialog(int id) method show and dismiss each dialog respectively.
Actually there is no any cancel()
or dismiss()
method from AlertDialog.Builder Class.
So Instead of AlertDialog.Builder optionDialog
use AlertDialog
instance.
Like,
AlertDialog optionDialog = new AlertDialog.Builder(this).create();
Now, Just call optionDialog.dismiss();
background.setOnClickListener(new OnClickListener() { public void onClick(View v) { SetBackground(); // here I want to dismiss it after SetBackground() method optionDialog.dismiss(); } });
I think there's a simpler solution: Just use the DialogInterface
argument that is passed to the onClick
method.
AlertDialog.Builder db = new AlertDialog.Builder(context); db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface d, int arg1) { db.cancel(); //here db.cancel will dismiss the builder }; });
See, for example, http://www.mkyong.com/android/android-alert-dialog-example.
Try this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog OptionDialog = builder.create();
background.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SetBackground();
OptionDialog .dismiss();
}
});
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