Giter Site home page Giter Site logo

maskformatter's Introduction

MaskFormatter

An Android Library to format Strings usings masks. Can be used with TextWatcher.

Min SDK version is 7.

Gradle

compile 'com.github.rtoshiro.mflibrary:mflibrary:1.0.0'
    repositories {
        mavenCentral()
    }

Basic Usage

To format and limit an EditText input, like dates, phone numbers... you can use MaskTextWatcher and SimpleMaskFormatter.

SimpleMaskFormatter comes with 5 pre defined patterns you can use.

  1. N - for numbers.
  2. L - for letters.
  3. L - for numbers and letters.
  4. l - for lowercase letters.
  5. U - for uppedcase letters.

MaskTextWatcher is used to control the input of EditText. So we have to add the watcher in EditText using EditText.addTextChangedListener method.

MaskTextWatcher needs two parameters. The first one is a TextView (or EditText as EditText extends TextView) and the second one is a MaskFormatter object.

Example to format dates like MM/DD/YYYY:

SimpleMaskFormatter smf = new SimpleMaskFormatter("NN/NN/NNNN");
MaskTextWatcher mtw = new MaskTextWatcher(myEditText, msf)
myEditText.addTextChangedListener(mtw);

In our SimpleMaskFormatter, we limit the usage to numbers only.

Custom Patterns

We can define our own pattern using MaskFormatter.

In our example, we are changing it to limit the numbers of our mask string.

For months, we are going to limit the first character from 0 to 1 and the second character from 0 to 9.

For day, we are going to limit the first character from 0 to 3 and the second character from 0 to 9.

We need to create 3 MaskPattern objects:

MaskPattern mp03 = new MaskPattern("[0-3]");
MaskPattern mp09 = new MaskPattern("[0-9]");
MaskPattern mp01 = new MaskPattern("[0-1]");

Then, we need to create a new MaskFormmater:

MaskFormatter mf = new MaskFormatter("[0-1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]");

And register our patterns in MaskFormatter object:

mf.registerPattern(mp01);
mf.registerPattern(mp03);
mf.registerPattern(mp09);

We can change the pattern representation using our own elements:

MaskFormatter mf = new MaskFormatter("19/39/9999");

mf.registerPattern("1", mp01);
mf.registerPattern("3", mp03);
mf.registerPattern("9", mp09);

maskformatter's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.