Code Monkey home page Code Monkey logo

currencyedittext's Introduction

CurrencyEditText

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

ci Maven Central

Gradle Dependency

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

implementation 'com.cottacush:CurrencyEditText:<insert-latest-version-here>'

For versions, kindly head over to the releases page

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.

Decimal Places

The maximum number of decimal digits can be specified using the maxNumberOfDecimalDigits attributes in the xml, requiring a minimum value of 1. It has a default value of 2.

   <com.cottacush.android.currencyedittext.CurrencyEditText
            ...
            app:maxNumberOfDecimalDigits="3" />

or programmatically:

   currencyEditText.setMaxNumberOfDecimalDigits(3)

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.

currencyedittext's People

Contributors

efguydan avatar johnitoo avatar mitsinsar avatar rasheedsulayman avatar renovate[bot] avatar thejer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

currencyedittext's Issues

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 }

Suggestion

I hope that the separator and the number of characters to be separated can be customized.

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 ",".

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

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.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-java v4
  • android-actions/setup-android v3
.github/workflows/publish.yml
  • actions/checkout v3
  • actions/setup-java v4
  • android-actions/setup-android v3
gradle
gradle.properties
dependencies.gradle
  • androidx.appcompat:appcompat 1.4.2
  • com.google.android.material:material 1.7.0
  • androidx.core:core-ktx 1.6.0
settings.gradle
spotless.gradle
build.gradle
  • com.android.application 8.0.1
  • com.android.library 8.0.1
  • org.jetbrains.kotlin.android 1.6.21
  • com.diffplug.spotless 6.12.0
  • com.vanniktech.maven.publish 0.25.3
library/build.gradle
  • junit:junit 4.13.2
  • org.mockito:mockito-core 5.3.1
  • org.mockito:mockito-inline 5.2.0
sample/build.gradle
signing/signing.gradle
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.0

  • Check this box to trigger a request for Renovate to run again on this repository

decimal places

it is not accepting decimal places. I click on the dot or comma and nothing happens, the number remains whole.

Bug

Pressing the backspace key cannot delete characters continuously, only one character can be deleted.

Request: Allow immediate typing if current value == 0 or 0.00

When the user clicks on a field that has already been pre-populated with a 0 value, it'd be nice if it allowed typing the new value immediately. Right now, a user has to delete all of the zeros before they begin typing a new value.

As a workaround I'm leaving it empty currently, but this behavior diverges from how I have this implemented in the iOS version of my app, so not perfectly ideal.

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?

Bump Library version to 1.0.0

The library has been stable for more than a year now. 0.X.X suggests that it's not stable. We should bump to 1.0.0

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

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.