💰 A library to dynamically format your EditTexts to take currency inputs

Overview

CurrencyEditText

A library to dynamically format your EditTexts to take currency inputs.

Build Status Download

Gradle Dependency

Add the dependency to your app's build.gradle:

implementation 'com.cottacush:CurrencyEditText:0.0.7'

Usage

Add the CurrencyEditText to your layout.

   <com.cottacush.android.currencyedittext.CurrencyEditText
            ...
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:ems="10"
            android:id="@+id/editText"/>

That's all for basic setup. Your editText should automatically format currency inputs.

Customisation

Currency Symbol

You can specify the currency symbol using the currencySymbol and useCurrencySymbolAsHint attributes in xml. The formatted currency value will be prepended with the currencySymbol value. The currencySymbol value can also be used as hint, as described by the useCurrencySymbolAsHint attribute.

   <com.cottacush.android.currencyedittext.CurrencyEditText
            ...
            app:currencySymbol="₦"
            app:useCurrencySymbolAsHint="true"/>

or programmatically:

   currencyEditText.setCurrencySymbol("₦", useCurrencySymbolAsHint = true)

Locale

The CurrencyEditText uses the default Locale if no locale is specified. Locale can be specified programmatically via

   currencyEditText.setLocale(locale)

Locales can also be specified using locale-tags. The locale tag method requires API 21 and above. Instructions on how to construct valid Locale and locale-tags can be found here.

   <com.cottacush.android.currencyedittext.CurrencyEditText
            ...
            app:localeTag="en-NG"/>

or programmatically via

   currencyEditText.setLocale("en-NG") //Requires API level 21 and above.

Getting the input value

Numeric values for the editText can be gotten as shown below.

   currencyEditText.getNumericValue()

If you need a BigDecimal to continue your monetary calculations right away, you can get it by

   currencyEditText.getNumericValueBigDecimal()

Using the formatter directly

If you'd like to use the library with any EditText widget, you can attach your EditText with the CurrencyInputWatcher class:

   editText.addTextChangedListener(CurrencyInputWatcher(editText,"₦", Locale.getDefault()))

License

Copyright (c) 2019 Cotta & Cush Limited.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Add custom logic to truncate extra decimal digits

    Add custom logic to truncate extra decimal digits

    There is an issue with using RoundingMode.DOWN to truncate extra decimal digits (to prevent RoundingMode.UP) This Pull Request adds a custom helper method to truncate those extra decimal digits; tested extensively with Unit Tests.

    Fixes #29

    bug 
    opened by efguydan 6
  • Basic information formatting number as currency

    Basic information formatting number as currency

    It would be interesting if it was delimited correctly depending on how the android device is configured and also if you want to show another type of currency. I hope it helps

    Extract info decimal formater

            val nf: NumberFormat = NumberFormat.getCurrencyInstance(Locale.getDefault())
    
            if (nf is DecimalFormat) {
                val sym: DecimalFormatSymbols = nf.decimalFormatSymbols
    
                Log.d(TAG, "currency: " + sym.currency)
                Log.d(TAG, "currencySymbol: " + sym.currencySymbol)
                Log.d(TAG, "decimalSeparator: " + sym.decimalSeparator)
                Log.d(TAG, "digit: " + sym.digit)
                Log.d(TAG, "exponentSeparator: " + sym.exponentSeparator)
                Log.d(TAG, "groupingSeparator: " + sym.groupingSeparator)
                Log.d(TAG, "infinity: " + sym.infinity)
                Log.d(TAG, "internationalCurrencySymbol: " + sym.internationalCurrencySymbol)
                Log.d(TAG, "minusSign: " + sym.minusSign)
                Log.d(TAG, "monetaryDecimalSeparator: " + sym.monetaryDecimalSeparator)
                Log.d(TAG, "naN: " + sym.naN)
                Log.d(TAG, "patternSeparator: " + sym.patternSeparator)
                Log.d(TAG, "perMill: " + sym.perMill)
                Log.d(TAG, "percent: " + sym.percent)
                Log.d(TAG, "zeroDigit: " + sym.zeroDigit)
            }
    

    Set my devices in Spanish/Spain

    Decimal Format detail

    D/: currency: EUR
    D/: currencySymbol: €
    D/: decimalSeparator: ,
    D/: digit: #
    D/: exponentSeparator: E
    D/: groupingSeparator: .
    D/: infinity: ∞
    D/: internationalCurrencySymbol: EUR
    D/: minusSign: -
    D/: monetaryDecimalSeparator: ,
    D/: naN: NaN
    D/: patternSeparator: ;
    D/: perMill: ‰
    D/: percent: %
    D/: zeroDigit: 0
    

    Print currency format

    nf.format(2536) //2.536,00 €
    nf.format(2536.3321F) //2.536,33 €
    nf.format(999999999999999) //999.999.999.999.999,00 €
    nf.format(0) //0,00 €
    nf.format(-31) //-31,00 €
    nf.format(-31.83F) //-31,83 €
    

    Set my devices in Enghish/USA

    Decimal Format detail

    D/FirstFragment: currency: USD
    D/FirstFragment: currencySymbol: $
    D/FirstFragment: decimalSeparator: .
    D/FirstFragment: digit: #
    D/FirstFragment: exponentSeparator: E
    D/FirstFragment: groupingSeparator: ,
    D/FirstFragment: infinity: ∞
    D/FirstFragment: internationalCurrencySymbol: USD
    D/FirstFragment: minusSign: -
    D/FirstFragment: monetaryDecimalSeparator: .
    D/FirstFragment: naN: NaN
    D/FirstFragment: patternSeparator: ;
    D/FirstFragment: perMill: ‰
    D/FirstFragment: percent: %
    D/FirstFragment: zeroDigit: 0
    

    Print currency format

    nf.format(2536) //$2,536.00
    nf.format(2536.3321F) //$2,536.33
    nf.format(999999999999999) //$999,999,999,999,999.00
    nf.format(0) //$0.00
    nf.format(-31) //-$31.00
    nf.format(-31.83F) //-$31.83
    

    Devices in English/USA

    represent a currency different from that established by the system

     nf.currency = Currency.getInstance("EUR")
    

    result view that the euro symbol is displayed at the beginning, as is the system set by the user

    nf.format(2536) //€2,536.00
    nf.format(2536.3321F) //€2,536.33
    nf.format(999999999999999) //€999,999,999,999,999.00
    nf.format(0) //€0.00
    nf.format(-31) //-€31.00
    nf.format(-31.83F) //-€31.83
    

    Extra

    Get available currencies

    listCurrencies = Currency.getAvailableCurrencies()
        .filter { !it.displayName.contains("(") }
        .sortedBy { it.currencyCode }
        .map { it.displayName }
    
    wontfix 
    opened by webserveis 3
  • Display blank in the EditText

    Display blank in the EditText

    First, focus in front of the currency selected symbol TIM图片20200217095717

    Then enter the number 1, 2 TIM图片20200217095722InkedTIM图片20200217095725_LI

    Will be displayed blank in the EditText and the cursor is not displayed in front of the text. @codedentwickler

    bug 
    opened by tcqq 3
  • Some amounts change while typing

    Some amounts change while typing

    An example:

    val amount = "515.809"
    currencyEditText.setText(amount)
    

    A text visible in a field is expected to be 515.809 and yet it becomes 515.808. I get the same results when trying to enter 515.809 manually.

    bug 
    opened by akumaigorodski 2
  • Configurable Maximum Decimal Digits Number

    Configurable Maximum Decimal Digits Number

    This Pull Request makes the maximum amount of decimal digits number configurable. It exposes a method and an XML attribute to configure it. Tests are also included to ensure functionality.

    opened by efguydan 2
  • Increase max number of decimal places

    Increase max number of decimal places

    Currently it is hardcoded to 2 in CurrencyInputWatcher companion object but in a project I'm working on this number needs to be 3. Can this be made configurable?

    enhancement 
    opened by akumaigorodski 2
  • After set Text Currency sign is not visible

    After set Text Currency sign is not visible

    If we set value to Edit Text etPrice.setText("123") then currency symbol is not visible. But its set in xml file like app:currencySymbol="$"

    bug 
    opened by pravin-umardand 2
  • Format EditText output values with the supplied locale.

    Format EditText output values with the supplied locale.

    This PR formats output values from the CurrencyEditText using the supplied Locale. The previous implementation wrongfully formats output values using an implicit locale that assumes that the decimal symbol will always be ".". This caused issues with locales/countries that don't have "." as the decimal symbol.

    Finally, the PRs adds support for getting user input as a BigDecimal

    opened by rasheedsulayman 2
  • Bug for Persian, Arabic decimal character

    Bug for Persian, Arabic decimal character

    In this line: hasDecimalPoint = s.toString().contains(decimalFormatSymbols.decimalSeparator.toString()) It will check Persian, Arabic or any language specific decimals, but if developer used numeric keyboard decimal char will be "," or ".". So have to add these two characters as well. SO if we set locale to Persian or Arabic locale from phone resources.configuration.locales[0]

    And enter 5.488 Result will be like this: Screen Shot 2022-02-18 at 13 42 10

    wontfix 
    opened by mahdit8 1
  • Fix: Rounding Error On Display During Entry

    Fix: Rounding Error On Display During Entry

    This Pull Request fixes Double's problem with accurately storing 1.0E23 in memory, storing 9.99999999999E22 instead. This is fixed by changing the parsed number during entry to BigDecimal from Double. BigDecimal doesn't have a problem with storing 1.0E23 or any other number in memory accurately, fixing this problem.

    Another plus that comes with this fix is; Double has a limit of 1.0E308, which returns infinity after this point. BigDecimal can store as many numbers as you can type.

    Fixes #17

    opened by efguydan 1
  • Pre-user Interaction setText() method calls

    Pre-user Interaction setText() method calls

    This PR fixes #14 by adding the text watcher as soon as the edittext is initialized. It also improves setText() method calls by moving the cursor to the end of the edit text after the method call.

    bug enhancement 
    opened by efguydan 0
  • Jetpack Compose Support.

    Jetpack Compose Support.

    Investigate how easy it is to support the TextField in Jetpack compose. Check if the formatting logic can be properly extracted and used for both the View-based EditText and Compose's TextVField.

    Ideally, the composing support should be exposed as an additional artefact/dependency on top of the core-library logic. We might need to do some refactoring on the existing package structure.

    opened by rasheedsulayman 1
  • Rounding error on display during entry

    Rounding error on display during entry

    Very large amount resulted in rounding error in display. Entered 1 followed by zeros...eventually saw $ 99,999,999,999,999,990,000,000 when value should have been $ 100,000,000,000,000,000,000,000

    Using Android Studio 4.2 Beta 6 Build #AI-202.7660.26.42.7188722, built on March 5, 2021 Runtime version: 11.0.8+10-b944.6916264 x86_64 VM: OpenJDK 64-Bit Server VM by N/A macOS 10.15.7

    And AVD: Name: Nexus_10_API_27 CPU/ABI: Google APIs Intel Atom (x86) Path: /Users/michaelhampton/.android/avd/Nexus_10_API_27.avd Target: google_apis [Google APIs] (API level 27) Skin: nexus_10 SD Card: 100M hw.dPad: no hw.lcd.height: 1600 runtime.network.speed: full hw.accelerometer: yes hw.device.name: Nexus 10 vm.heapSize: 192 skin.dynamic: yes hw.device.manufacturer: Google hw.lcd.width: 2560 hw.gps: yes hw.initialOrientation: Portrait image.androidVersion.api: 27 hw.audioInput: yes image.sysdir.1: system-images/android-27/google_apis/x86/ tag.id: google_apis showDeviceFrame: yes hw.camera.back: virtualscene hw.mainKeys: no AvdId: Nexus_10_API_27 hw.camera.front: emulated hw.lcd.density: 320 avd.ini.displayname: Nexus 10 API 27 hw.arc: false hw.gpu.mode: auto hw.device.hash2: MD5:813203ac93d1d63cd91f729c376b1f3e hw.ramSize: 1536 hw.trackBall: no PlayStore.enabled: false fastboot.forceColdBoot: no hw.battery: yes hw.cpu.ncore: 4 hw.sdCard: yes tag.display: Google APIs runtime.network.latency: none hw.keyboard: yes hw.sensors.proximity: no disk.dataPartition.size: 800M hw.sensors.orientation: yes avd.ini.encoding: UTF-8 hw.gpu.enabled: yes

    bug pinned 
    opened by michael-reed-hampton 3
  • Does not work with French/European locales

    Does not work with French/European locales

    When using it from a US locale, it works perfectly. You can choose when to use the decimal etc... But when using for a locale like European where they use the "," instead of the ".", you can not choose the "," (or even the ".") to denote decimal place.

    Similarly, when displaying the price, it does not seem correctly format it for the locale, using "." instead of ",".

    bug enhancement pinned 
    opened by StainlessStlRat 2
Releases(v1.0.0)
Owner
Cotta & Cush Limited
We are web, mobile and data engineering experts.
Cotta & Cush Limited
Adding locale aware thousands separator dynamically as the user types

Locale aware, dynamic thousands separator for EditText Adding locale aware thousands separator dynamically as the user types Features: Add thousands s

Abhilash Dhondalkar 0 Oct 3, 2021
A module designed to encapsulate the use of an Android EditText field for gathering currency information from a user. Supports all ISO-3166 compliant locales/currencies.

CurrencyEditText CurrencyEditText is an extension of Android's EditText view object. It is a module designed to provide ease-of-use when using an Edit

Josh Kitchens 335 Dec 25, 2022
Add text masking functionality to Android EditText. It will prevent user from inserting not allowed signs, and format input as well.

MaskFormatter MaskFormatter adds mask functionality to your EditText. It will prevent user from inserting not allowed signs, and format input as well.

Azimo Labs 161 Nov 25, 2022
A simpler way to style your TextViews

BabushkaText BabushkaText is a custom TextView which lets you customize the styling of parts of your text via Spannables, but without the hassle of ha

Henrique Boregio 752 Dec 29, 2022
An extension of Android's TextView, EditText and Button that let's you use the font of your choice

AnyTextView (deprecated) Note: AnyTextView is no longer being maintained. I recommend replacing AnyTextView with the Calligraphy library instead. Frus

Hans Petter Eide 165 Nov 11, 2022
A simpler way to style your TextViews

BabushkaText BabushkaText is a custom TextView which lets you customize the styling of parts of your text via Spannables, but without the hassle of ha

Henrique Boregio 753 Jun 7, 2022
An easy approach on how to create your country code picker for the edit text.

Country-Code-Picker Original Article on Dev.to Click below ?? App's Overview In this article, I am going to demonstrate how to create a simple country

Siddharth Singh 5 Mar 10, 2022
Irineu A. Silva 2 Feb 17, 2022
A library to show emoji in TextView, EditText (like WhatsApp) for Android

Discontinued This projected is discontinued. Please consider using other alternative, i.e EmojiCompat. Contact me if you want to continue working on a

Hieu Rocker 3.6k Jan 5, 2023
Android library contain custom realisation of EditText component for masking and formatting input text

Masked-Edittext Masked-Edittext android library EditText widget wrapper add masking and formatting input text functionality. Install Maven <dependency

Evgeny Safronov 600 Nov 29, 2022
library to implement and render emojis For Android

Release Notes SuperNova-Emoji SuperNova-Emoji is a library to implement and render emojis. Minimum SDK Level: 9 (2.3) Contact Java Usage To use defaul

Hani Al-momani 360 Jan 3, 2023
A single EditText instead of a classical form. Library that implements flavienlaurent's singleinputform

material-singleinputform A single EditText instead of a classical form. This Library is a library implementation of flavienlaurent's "Single input for

Jan Heinrich Reimer 200 Nov 14, 2022
Form validation and feedback library for Android. Provides .setText for more than just TextView and EditText widgets. Provides easy means to validate with dependencies.

android-formidable-validation Form validation and feedback library for Android. Provides .setText for more than just TextView and EditText widgets. Pr

Linden 147 Nov 20, 2022
Lightweight android library for highlighting sections of a textview, with optional callbacks.

Linker Lightweight android library for highlighting Strings inside of a textview (ignoring case), with optional callbacks. Language: Java MinSDK: 17 J

Josh Gainey 35 Apr 30, 2022
Awesome Android Typeahead library - User mention plugin, UI widget for auto complete user mention using the at sign (@) like Twitter or Facebook.

android-typeahead Awesome Android Typeahead library - User mention plugin, UI widget for auto complete user mention using the at sign (@) like Twitter

Arab Agile 11 Jun 4, 2019
Library project with a custom view that implements the Float Label pattern

AndroidFloatLabel This repository contains an Android library project for Android 4.0+ with a custom view that implements the Float Label pattern (htt

Ian G. Clifton 475 Dec 27, 2022
A single EditText instead of a classical form. Library that implements flavienlaurent's singleinputform

material-singleinputform A single EditText instead of a classical form. This Library is a library implementation of flavienlaurent's "Single input for

Jan Heinrich Reimer 200 Nov 14, 2022
User input masking library repo.

More GIFs [~3 MB] Migration Guide: v.6 This update brings breaking changes. Namely, the autocomplete flag is now a part of the CaretGravity enum, thus

red_mad_robot 1.2k Dec 20, 2022
A TextView library that allows the user to increase/decrease font size with a two finger gesture by the user.

PinchZoomTextView Library This library allows you to have a TextView that will grow/shrink the font size using gestures from the user. Usage To have a

null 311 Nov 23, 2022