Giter Site home page Giter Site logo

jakeaaron / multicurrencyformatter Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 3.73 MB

๐Ÿ’ธ A small Android library that dynamically reformats user input (from an EditText) for displaying currency values in any locale or currency.

License: MIT License

Kotlin 100.00%
android edittext currency money kotlin textformatter

multicurrencyformatter's Introduction

codecov

MultiCurrencyFormatter

A small Android library that dynamically reformats user input (from an EditText) for displaying currency values in any locale or currency. Java's DecimalFormat is used for formatting the numbers derived from user input.

Requirements

  • Android 4.1 (API 16)

Installation

Add Jitpack to your project build.gralde file

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Then add this dependency to your app build.gradle file.

dependencies {
    implementation 'com.github.jakeaaron:MultiCurrencyFormatter:1.0.0-alpha.1'
}

Configuration

The MultiCurrencyFormatter allows configuration for the following fields/behaviors:

  1. Currency: Java Currency class used for formatting currency text values
  2. Currency symbol: Custom symbol to override the symbol associated with the Currency
  3. Locale: Locale used for formatting currency text values
  4. Resize text automatically
  5. Superscript the currency symbol

The following code snippet shows how to configure each field/behavior:

MultiCurrencyFormatter.newInstance(viewLifecycleOwner, editText)
    .setCurrency(Currency.getInstance(Locale.JAPAN))
    .setLocale(Locale.US)
    .setSymbol("๐Ÿ’ธ") 
    .setAutoResize(true)
    .setSymbolSuperscript(true)

Usage

Run the sample project to see the MultiCurrencyFormatter in action.

Basic Usage

The MultiCurrencyFormatter requires a LifecycleOwner and an EditText to create a new instance. This is all that is required to start formatting currency text.

MultiCurrencyFormatter.newInstance(viewLifecycleOwner, editText)

MultiCurrencyFormatter listens for the LifecycleOwner#onDestroy event to clear out its reference to the EditText to avoid leaking its context. This means that the consumer doesn't need to worry about manually clearing out the view reference. When MultiCurrencyFormatter is instantiated, it registers the internal CurrencyTextWatcher with the EditText in order to listen to text input changes.

By default, the MultiCurrencyFormatter uses the default locale associated with the device to parse and format values. The locale dictates the Currency value, which then dictates the currency symbol. This means that in most cases, changing the locale will end up doing the correct thing in relation to the Currency and symbol. However, there is additional functionality in case more granular control is needed.

Setting Formatter Attributes

In order to change the Currency, Symbol, or Locale used by the formatter, use the corresponding methods on MultiCurrencyFormatter instance:

MultiCurrencyFormatter.newInstance(viewLifecycleOwner, editText)
    .setLocale(Locale.CHINA)
    .setSymbol("ยฅ")
    .setCurrency(Currency.getInstance(Locale.CHINA))

or

MultiCurrencyFormatter.newInstance(viewLifecycleOwner, editText)
    .setCurrencyFormatter(
        CurrencyFormatter.getInstance(
            currency,
            symbol,
            locale
        )
    )

This is useful for updating the formatter in response to a user event (like selecting a new currency or locale from a menu/list, i.e. see sample project).

Reading and Writing Currency Values

MultiCurrencyFormatter.textValue holds the formatted String information associated with the EditText, including the currency symbol.

MultiCurrencyFormatter.numberValue holds the BigDecimal value associated with the parsed EditText.text value.

MultiCurrencyFormatter.setAmount sets the text value.

For example:

MultiCurrencyFormatter.newInstance(viewLifecycleOwner, editText)
    .setLocale(Locale.US)
    .setAmount("50.00")
    
MultiCurrencyFormatter.textValue // $50.00
MultiCurrencyFormatter.numberValue // BigDecimal("50.00")

Advanced Usage

Custom TextWatcher

If you need additional callbacks into the EditText's TextWatcher use the addTextChangeListener method.

For example:

val editText = EditText(this)
val formatter = CurrencyFormatter.getInstance(Locale.ITALY)
val multiCurrencyFormatter = MultiCurrencyFormatter.newInstance(
    this,
    editText,
    currencyFormatter = formatter
)
val customListener = object : TextWatcher {
    override fun afterTextChanged(s: Editable?) {
        // do something important
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }
}
multiCurrencyFormatter.addListener(customListener)

You may use the removeListener call to remove the listener, however, all listeners will get cleared on the LifecycleOwner#onDestroy event.

Underlying DecimalFormat

If you need access to the underlying DecimalFormat instance, it can be accessed from the CurrencyTextWatcher instance:

val multiCurrencyFormatter = MultiCurrencyFormatter.newInstance(viewLifecycleOwner, editText)
val decimalFormat = multiCurrencyFormatter.currencyTextWatcher.formatter.underlyingDecimalFormat

If you just need access to the underlying DecimalFormats decimalFormatSymbols, use the extension field MultiCurrencyFormatter.decimalFormatSymbols. This is useful for accessing the decimalSeparator, for instance.

How to test the software

Run the tests using an IDE like Intellij or Android Studio. Learn more.

Getting help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

Open source licensing info

LICENSE

Credits and references

  1. Cash App
  2. Stack Overflow Q/A
  3. CurrencyEditText
  4. How to Format 30+ Currencies from Countries All Over the World
  5. Location Based Currency Formatting in Java
  6. Multi-currency support in Java

multicurrencyformatter's People

Contributors

jakeaaron avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

multicurrencyformatter's Issues

Values less than 0 cause a crash

Typing "0." or just "." causes a crash with java.lang.NumberFormatException: For input string: ""

I believe the error is in CurrencyTexyWatcherImpl line 57, it should be >= instead of >

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.