Coder Social home page Coder Social logo

phone-formatter's Introduction

Download

Android phone Formatter library

This library is replacement for systems PhoneNumberFormattingTextWatcher . It's a copy of this class rewritten to Kotlin with mutable countryCode property.

Purpose

This library was written because in a lot of applications we have this kind of input for phone number

alt text

We have separated input (Spinner in this example) for phone prefix and separated input for the rest of the phone number.

If we initialize PhoneNumberFormattingTextWatcher without specifying country code, it will be formatted by system's locale country. That is not right because for example if I have phone in English (US) and I choose Czech phone prefix +420, the number is still formatted by US rules. It should be formatted according to Czech rules instead.

That is the reason why our own modification of this Formatter was created. When user changes phone prefix, formatter needs to be notified with new country code and it will be automatically reformatted.

Usage

Gradle dependency

    implementation "cz.ackee:phoneformatter:x.x.x"
    private val textFormatter = PhoneNumberFormatter(Locale.getDefault().country)
    ...
    editPhone.addTextWatcher(textFormatter)

    // then when prefix is changed
    someInput.onPrefixChanged { country ->
        textFormatter.countryCode = country
    }

There is multiple solutions to have mapping from phone prefixes to country code:

  1. Have a list of prefixes in the app along with the country code
val prefixes = mapOf(
    "CZ" to "+420",
    "DE" to "+49",
    ...
)

and then just perform lookup in this map when prefix is changed

  1. Have a list of prefixes without the country association and use LibPhoneNumber library to perform the lookup
input.onPrefixChanged { prefix -> // prefix like +420
       // method getRegionCodeForCountryCode accepts integer like 420 so we need tu strip the plus sign and convert it to integer
       textFormatter.countryCode = PhoneNumberUtil.getInstance().getRegionCodeForCountryCode(prefix.substring(1).toInt())
}

phone-formatter's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.