Skip to content

1.1 Custom Notations

Egor Taflanidi edited this page Feb 6, 2019 · 1 revision
data class Notation(
    /**
     * A symbol in format string.
     */
    val character: Char,
    /**
     * An associated character set of acceptable input characters.
     */
    val characterSet: String,
    /**
     * Is it an optional symbol or mandatory?
     */
    val isOptional: Boolean
)

An advanced experimental feature. Use with caution.

Internal Mask compiler supports a series of symbols which represent letters and numbers in user input. Each symbol stands for its own character set.

For instance, 0 and 9 stand for a numeric character set. This means user can type any digit instead of 0 or 9. A and a represent letters, which means user can type any letter instead of A or a.

The difference between 0 and 9 is that 0 stands for a mandatory digit, while 9 represents an optional digit. With the mask like [099][A] user will be able to enter 1b, 12c or 123d, while with the mask [000][A] user won't be able to enter letters unless he has entered all three digits: 1 or 12 or 123 or 123e.

Summarizing, each symbol supported by the compiler has its own character set associated with it, and also has an option to be mandatory or not.

That said, you may configure your own symbols in addition to the default ones through the Notation objects:

Mask("[999][.][99]", listOf(Notation('.', ".,", true)))

or

Mask.getOrCreate("[999][.][99]", listOf(Notation('.', ".,", true)))

MaskedTextChangedListener and its children contains a customNotations constructor parameter to pass your custom notations to the Mask instance inside.

Please note, that you won't have autocompletion for any of your custom symbols. For more examples, see below.

A floating point number with two decimal places

Mask: [999999999][.][99]

Custom notations:
Notation('.', ".", true)
Results
1
123
1234.
1234.5
1234.56

An email (please use regular expressions instead)

With optional and mandatory "dots" and "at" symbol.

Mask: [aaaaaaaaaa][d][aaaaaaaaaa][@][aaaaaaaaaa][d][aaaaaaaaaa][D][aaaaaaaaaa]

Custom notations:
Notation('D', ".", false),
Notation('d', ".", true),
Notation('@', "@", false)
Results
d
derh
derh.
derh.a
derh.asd
derh.asd@
derh.asd@h
derh.asd@hello.
[email protected]
[email protected].
[email protected]
[email protected]

An optional currency symbol

Mask: [s][9999]

Custom notations:
Notation('s', "$€", true)
Results
12
$12
918
€918
1000
$1000