Prevent an AlertDialog from closing on PositiveButton click

WroahWroah NLMember

I'm new to Xamarin and I don't know how to do this in c#. I want to prevent an alertdialog from closing. I need to do some validation on the input first. If the input is correct, the dialog can close, else I will show a message with instructions.
Basically, I have the following code:


private void CreateAddProjectDialog() {
//some code
var alert = new AlertDialog.Builder (this);
alert.SetTitle ("Create new project");
alert.SetView (layoutProperties);
alert.SetCancelable (false);
alert.SetPositiveButton("Create", HandlePositiveButtonClick);
alert.SetNegativeButton("Cancel", HandelNegativeButtonClick);
}

private void HandlePositiveButtonClick (object sender, EventArgs e) {
//Do some validation here and return false (prevent closing of dialog) if invalid, else close....
}

Now, I red the following post on StackOverflow:
http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked

I think the code below (taken from StackOverflow) has the solution, but I don't know how to rewrite my c# code to implement the Java:


AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
builder.setPositiveButton("Test",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do nothing here because we override this button later to change the close behaviour.
//However, we still need this because on older versions of Android unless we
//pass a handler the button doesn't get instantiated
}
});
AlertDialog dialog = builder.create();
dialog.show();
//Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});

How to code this in c#? Especially the override part in the setPositiveButton area...

Posts

  • CarelMeyerCarelMeyer ✭✭ ZAMember ✭✭

    Have you find away around this?

    I am sitting with the same problem

  • CortezCortez ✭✭✭ USMember ✭✭✭

    Hello Guys!

    Previously i created this kind of function, a WifiActivityChecker. If the wifi not connected before the dialog not let you continue. In my code i didn't set the cancelable to false. With the dedicated hardware back button you can skip this dialog.

    With this function you need only initialize an instance, call the .Check() method somewhere and attached to the StartListeningEvent. If the criterion will be accepted(you connected to wifi) then the event fired and you will noticed there:

    WifiActivityChecker wac = new WifiActivityChecker(this); wac.Check(); wac.StartListeningEvent += () => { // The criterion accepted; }

  • WroahWroah NLMember

    Sorry, haven't been here for a while, but indeed, I found a solution, which is similar to Cortez suggestion:

    //I removed some code, but should be obvious
    
    using (var alert = new AlertDialog.Builder (this)) {
        alert.SetView (layoutProperties);
        alert.SetCancelable (false);
        alert.SetPositiveButton ("Create", (EventHandler<DialogClickEventArgs>)null);
        alert.SetNegativeButton ("Cancel", (EventHandler<DialogClickEventArgs>)null);
        _dialog = alert.Create ();
    }
    
    _dialogPositiveButton = _dialog.GetButton ((int)DialogButtonType.Positive);
    _dialogNegativeButton = _dialog.GetButton ((int)DialogButtonType.Negative);
    
    _dialogPositiveButton.Click += HandleDialogPositiveButtonClick;
    _dialogNegativeButton.Click += HandleDialogNegativeButtonClick;
    
    private void HandleDialogPositiveButtonClick (object sender, EventArgs e) {
        //do something here and dismiss the dialog if you want to:
        if (satisfied) {
            DismissDialog();
        }
    }
    
    private void HandleDialogNegativeButtonClick (object sender, EventArgs e) {
        DismissDialog ();
    }
    
    private void DismissDialog () {
        _dialogPositiveButton.Click -= HandleDialogPositiveButtonClick;
        _dialogNegativeButton.Click -= HandleDialogNegativeButtonClick;
        _dialog.Dismiss ();
    }
    
  • mehmedmehmed USMember

    Dialog is closing anyway without executing Dismiss() method of AlertDialog.

  • DiegoVarelaDiegoVarela ✭✭ COMember ✭✭

    The code > @Wroah said:

    Sorry, haven't been here for a while, but indeed, I found a solution, which is similar to Cortez suggestion:

    //I removed some code, but should be obvious

    using (var alert = new AlertDialog.Builder (this)) {
    alert.SetView (layoutProperties);
    alert.SetCancelable (false);
    alert.SetPositiveButton ("Create", (EventHandler)null);
    alert.SetNegativeButton ("Cancel", (EventHandler)null);
    _dialog = alert.Create ();
    }

    _dialogPositiveButton = _dialog.GetButton ((int)DialogButtonType.Positive);
    _dialogNegativeButton = _dialog.GetButton ((int)DialogButtonType.Negative);

    _dialogPositiveButton.Click += HandleDialogPositiveButtonClick;
    _dialogNegativeButton.Click += HandleDialogNegativeButtonClick;

    private void HandleDialogPositiveButtonClick (object sender, EventArgs e) {
    //do something here and dismiss the dialog if you want to:
    if (satisfied) {
    DismissDialog();
    }
    }

    private void HandleDialogNegativeButtonClick (object sender, EventArgs e) {
    DismissDialog ();
    }

    private void DismissDialog () {
    _dialogPositiveButton.Click -= HandleDialogPositiveButtonClick;
    _dialogNegativeButton.Click -= HandleDialogNegativeButtonClick;
    _dialog.Dismiss ();
    }

    it works good but the only thing that is left is to first show the dialog and then set the button click events.

  • cfl777cfl777 ✭✭ ZAMember ✭✭
    edited July 2016

    I got the original questions code working in c#

    Main parts:

    `
    ...
    buildingDialog.SetView(view)
    .SetPositiveButton("Ok", (senderAlert, args) => {})
    . SetNegativeButton("Cancel", (senderAlert, args) => {});

            _dialog = buildingDialog.Create();
            return _dialog;
        }
    
        public override void OnStart()
        {
            base.OnStart();
            AlertDialog alertdialogholder = (AlertDialog)Dialog;
            if(alertdialogholder != null)
            {
                Button positiveButton = (Button) alertdialogholder.GetButton((int)DialogButtonType.Positive);
                positiveButton.Click += delegate
                {
                    if (!isTitleValid(edT_building.Text))
                    {
                        Toast.MakeText(this.Activity, "stopped", ToastLength.Short).Show();
                    }
                    else
                    {
                        _dialog.Dismiss();
                    }
                };
            }
        }
    
        private bool isTitleValid(string edT_buildingText)
        {
            if (edT_buildingText == "")
                return false;
    
            return true;
        }`
    
  • RavindraKumarRavindraKumar ✭✭ USMember ✭✭

    Hey Guys,

    Hope this code snippet may help you:

    AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert.SetMessage(message).SetCancelable(false); // make true if you need cancel button alert.SetPositiveButton("OK", (senderAlert, args) => { // write your code here //for e.g. //if (editText != null) //editText.RequestFocus(); }); AlertDialog dialog = alert.Create(); dialog.Show();

  • momemome ✭✭ Member ✭✭

    @Wroah said:
    Sorry, haven't been here for a while, but indeed, I found a solution, which is similar to Cortez suggestion:

    //I removed some code, but should be obvious

    using (var alert = new AlertDialog.Builder (this)) {
    alert.SetView (layoutProperties);
    alert.SetCancelable (false);
    alert.SetPositiveButton ("Create", (EventHandler)null);
    alert.SetNegativeButton ("Cancel", (EventHandler)null);
    _dialog = alert.Create ();
    }

    _dialogPositiveButton = _dialog.GetButton ((int)DialogButtonType.Positive);
    _dialogNegativeButton = _dialog.GetButton ((int)DialogButtonType.Negative);

    _dialogPositiveButton.Click += HandleDialogPositiveButtonClick;
    _dialogNegativeButton.Click += HandleDialogNegativeButtonClick;

    private void HandleDialogPositiveButtonClick (object sender, EventArgs e) {
    //do something here and dismiss the dialog if you want to:
    if (satisfied) {
    DismissDialog();
    }
    }

    private void HandleDialogNegativeButtonClick (object sender, EventArgs e) {
    DismissDialog ();
    }

    private void DismissDialog () {
    _dialogPositiveButton.Click -= HandleDialogPositiveButtonClick;
    _dialogNegativeButton.Click -= HandleDialogNegativeButtonClick;
    _dialog.Dismiss ();
    }

    Sth like this one worked for me too, Thanks

Sign In or Register to comment.