Xamarin AlertDialog closes on Positive Button click even if not input provided

DelwarDelwar USMember ✭✭

I've a simple input alert dialog that should receive user input but the alert dialog closes even if user click ok button without enter any input. Could anyone help please. Here is the code thats being used. By the way, I need to make this alert dialog await for calling method to receive the input. You may also please suggest better way to await the input alert dialog.

public async Task NumberInputDialog()
{
try
{
var inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
var inputView = inflater.Inflate(Resource.Layout.NumberInput, null);
var builder = new AlertDialog.Builder(this.context);
builder.SetTitle("Enter Qty");
builder.SetCancelable(false);
builder.SetView(inputView);
builder.SetPositiveButton("OK", OK);
builder.SetNegativeButton("CANCEL", delegate { builder.Dispose(); });
builder.Show();

            while (string.IsNullOrEmpty(this.Number))
            {
                await Task.Delay(25);
            }

            return this.Number;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

//BUTTON CLICK EVENT
private void OK(object sender, DialogClickEventArgs e)
{
var dialog = (AlertDialog)sender;
var NumberInput = (EditText)dialog.FindViewById(Resource.Id.txtNumberInput);

        try
        {
            if (!string.IsNullOrEmpty(NumberInput.Text))
            {
                this.Number = NumberInput.Text;
                dialog.Dismiss();
            }else{
                NumberInput.RequestFocus();
            }
        }
        catch(Exception ex)
        {
            new AlertDialog.Builder(this.context).SetMessage(ex.Message).Show();
        }
    }

Best Answer

  • DelwarDelwar US ✭✭
    Accepted Answer

    Hi, thank you very much for being with me and trying to help me. I resolved the issue in following way and now it works as I intended.

        //builder.Show();
                var dialog = builder.Create();
                dialog.Show();
    
                var btnOk = dialog.GetButton((int)DialogButtonType.Positive).Enabled=false;
    
                EditText input = (EditText)inputView.FindViewById(Resource.Id.txtNumberInput);
    
                input.AfterTextChanged += (sender, e) =>
                  {
                      if(!string.IsNullOrEmpty(input.Text))
                      {
                          dialog.GetButton((int)DialogButtonType.Positive).Enabled = true;
                      }
                  };
    

Answers

  • sonny_ppgsonny_ppg USMember, University ✭✭

    I see a few problems, you are returning strings, but method is set to return type of 'Task' you should change the return type to Task. Also you are creating a loop that goes on until number is set, you should set number in the OK event and not loop until its set. Are you getting any runtime errors being caught?

  • DelwarDelwar USMember ✭✭

    I get the string return output over Task without any issues or error. My problem is when OK button is pressed without any input the dialog closes anyway. I need the dialog to stay open if there is input. How do I achieve that.

  • sonny_ppgsonny_ppg USMember, University ✭✭

    The button press will automatically dismiss the view, you might want to disable the OK button until input is entered.
    like so
    builder.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

  • DelwarDelwar USMember ✭✭

    Hi, builder in the below code does not have any .GetButton method

    builder.SetTitle("Enter Qty");
    builder.SetCancelable(false);
    builder.SetView(inputView);
    builder.SetPositiveButton("OK", OK);
    builder.SetNegativeButton("CANCEL", delegate { builder.Dispose(); });
    builder.Show();

  • sonny_ppgsonny_ppg USMember, University ✭✭

    Instead of showing the the alert dialog "builder.Show();" create it, pass it, then disable the button like with the code below

    //builder.Show();
    var aDialog = builder.Create();
    aDialog.GetButton((int)DialogButtonType.Positive).Enabled = false;
    aDialog.Show();

    Here is a link to the API

    https://developer.android.com/reference/android/app/AlertDialog.Builder.html

    https://developer.android.com/reference/android/app/AlertDialog.html

    Xamarin version of get button

    https://developer.xamarin.com/api/member/Android.App.AlertDialog.GetButton/p/System.Int32/

  • DelwarDelwar USMember ✭✭
    Accepted Answer

    Hi, thank you very much for being with me and trying to help me. I resolved the issue in following way and now it works as I intended.

        //builder.Show();
                var dialog = builder.Create();
                dialog.Show();
    
                var btnOk = dialog.GetButton((int)DialogButtonType.Positive).Enabled=false;
    
                EditText input = (EditText)inputView.FindViewById(Resource.Id.txtNumberInput);
    
                input.AfterTextChanged += (sender, e) =>
                  {
                      if(!string.IsNullOrEmpty(input.Text))
                      {
                          dialog.GetButton((int)DialogButtonType.Positive).Enabled = true;
                      }
                  };
    
Sign In or Register to comment.