Articles in this section
Category / Section

How to add DataForm (SfDataForm) to AlertDialog in Xamarin.Android

1 min read

You can add SfDataForm to alert dialog using the AlertDialog builder in Xamarin.Android.

Initialize SfDataForm, set DataObject and other requirements of SfDataForm in the button click event handler.

private void OnLogin(object sender, System.EventArgs e)
{
var dataForm = new SfDataForm(this);
dataForm.DataObject = new LoginInfo();
dataForm.CommitMode = CommitMode.PropertyChanged;
dataForm.RegisterEditor("Password", "Password");
…
}

Initialize the AlertDialog.Builder and set SfDataForm as builder view.

private void OnLogin(object sender, System.EventArgs e)
{
…
var builder = new AlertDialog.Builder(this);
builder.SetCancelable(false);
builder.SetTitle("Enter credentials");
builder.SetPositiveButton("Ok", (s1, e1) => {
});
builder.SetView(dataForm);
…
}

For AlertDialog, set SoftInptMode to specify the action needs to be done on showing the soft keyboard. And, clear the NotFocusable flags to enable the soft keyboard and focus the fields of SfDataForm.

private void OnLogin(object sender, System.EventArgs e)
{
            …
 
            AlertDialog dialog = builder.Show();
            dialog.Window.SetSoftInputMode(SoftInput.AdjustPan);
            dialog.Window.ClearFlags(WindowManagerFlags.NotFocusable | WindowManagerFlags.AltFocusableIm);
}

Output

DataForm alert dialog

Download the sample from GitHub.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied