Skip to content

SimpleFormDialog

Philipp Niedermayer edited this page May 28, 2021 · 18 revisions

Form dialog

extends CustomViewDialog, Full API reference

A Dialog that can be used to create simple or more complex forms.

One can either use one of the following predefined dialogs or use an arbitrary combination of fields as shown below.

  • Login dialog (username or email)
  • Email input
  • Password input
  • Number input

It is also simple to use custom layout containers like FlexboxLayout by extending this dialog (see #17).

Building dialogs

Examples

SimpleFormDialog.buildLogin(USERNAME, PASSWORD)
                .show(this, LOGIN_DIALOG);
SimpleFormDialog.build()
                .title(R.string.register)
                .msg(R.string.please_fill_in_form)
                .fields(
                        Input.name(NAME).required().hint(R.string.name),
                        Input.password(PASSWORD).required().max(20).validatePatternStrongPassword(),
                        Input.email(EMAIL).required(),
                        Input.plain(COUNTRY).hint(R.string.country)
                             .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)
                             .suggest(R.array.countries_locale).forceSuggestion(),
                        Check.box(null).label(R.string.terms_accept).required()
                )
                .show(this, REGISTRATION_DIALOG);

FormElements

Full API reference

FormElements can be added using the .fields(FormElement...) method as shown above.

Each FormElement gets assigned a custom key-string when created. You can receive the entered value in onResult using this key (see below).
You can also mark every form element as required, so that the user cannot continue, until text was entered, the checkbox checked or a value from the spinner chosen.

The following predefined FormElements exists, but new ones can be created easily by extending the FormElement and FormElementViewHolder classes:

  • Input fields Input
    Options: InputType, hint, initial text, min/max length, suggestions, password visibility toggle button, input validation (see below)
    Required flag: requires this field to contain non white-space text
  • Checkboxes Check
    Options: Label, initial state
    Required flag: requires this field to be checked
  • Spinners Spinner
    Options: Items, label, place-holder text if nothing is selected, initially selected item
    Required flag: requires a value to be chosen. This will remove the place-holder text from the list of choice
  • Color picker ColorField
  • Text hint Hint

Input validation

There are several possibilities to validate user input and display error messages for an Input form element:

  • Prevent empty input
    An Input field flagged as required must contain non-whitespace caracters (.required(true))
  • Specify min/max input length
    Upper and/or lower limits can be set using the .min(int minLength) and .max(int maxLength) methods
  • Ensure that a given suggestion is entered
    Calling .forceSuggestion() on an input field will require the user to enter one of the provided suggestions in order to continue
  • Pattern validation
    Specify a regular expression pattern using the .validatePattern(String pattern, String errorMsg) method. The following predefined patterns are available:
    .validatePatternEmail() – Email address pattern
    .validatePatternStrongPassword() – Must have 8 chars and include numbers, upper- and lowercase letters and a special character
    .validatePatternLetters() – Only allows for the 26 letters A-Z (either lower- or uppercase)
    .validatePatternAlphanumeric() – Like the above, but also allows for numbers
  • Custom validation
    If neither of the above suits your needs, you can make use of the SimpleFormDialog.InputValidator.
    Let the fragment or activity were you show your dialog implement this Interface and overwrite the callback method .validate that will be called for validation (other validations will still be performed). If the input is valid you should return null from this callback. Otherwise, the returned string will be displayed as an error message.

Receiving results

In onResults get the entered values from the Bundle using the keys set when the dialog was created like this:

String text = extras.getString(INPUT_KEY);
boolean checked = extras.getBoolean(CHECKBOX_KEY);
int selected = extras.getInt(SPINNER_KEY);

See SimpleDialog

Clone this wiki locally