Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cottacush/currencyedittext

💰 A library to dynamically format your EditTexts to take currency inputs
https://github.com/cottacush/currencyedittext

android-library currency-formatter currencyedittext edittext kotlin textwatcher

Last synced: 4 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# CurrencyEditText

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

[![ci](https://github.com/CottaCush/CurrencyEditText/actions/workflows/ci.yml/badge.svg)](https://github.com/CottaCush/CurrencyEditText/actions/workflows/ci.yml)
[![Maven Central](https://img.shields.io/maven-central/v/com.cottacush/CurrencyEditText.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.cottacush%22%20AND%20a:%22CurrencyEditText%22)

## Gradle Dependency

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

```groovy
implementation 'com.cottacush:CurrencyEditText:'
```
For versions, kindly head over to the [releases page](https://github.com/CottaCush/CurrencyEditText/releases)

## Usage

Add the `CurrencyEditText` to your layout.
```xml

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

```xml

```
or programmatically:
```kotlin
currencyEditText.setCurrencySymbol("₦", useCurrencySymbolAsHint = true)
```

### Locale
The `CurrencyEditText` uses the default `Locale` if no locale is specified. `Locale` can be specified programmatically via
```kotlin
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](https://docs.oracle.com/javase/tutorial/i18n/locale/create.html#factory).

```xml

```
or programmatically via

```kotlin
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.

```xml

```
or programmatically:
```kotlin
currencyEditText.setMaxNumberOfDecimalDigits(3)
```

## Getting the input value

Numeric values for the editText can be gotten as shown below.
```kotlin
currencyEditText.getNumericValue()
```
If you need a `BigDecimal` to continue your monetary calculations right away, you can get it by
```kotlin
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:

```kotlin
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.