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

https://github.com/devapro/numberwatcher

TextWatcher class for constraining and formatting input in EditText
https://github.com/devapro/numberwatcher

Last synced: 3 days ago
JSON representation

TextWatcher class for constraining and formatting input in EditText

Awesome Lists containing this project

README

          

# TextWatcher for number inputs

- Validate user input
- Replace "," to "."
- Get only valid number string to listener
- Adjusting numbers after decimal point (set 0 for integer)
- Adding any string to the input end
- set max positive value
- set min negative value
- enable or disable negative values fo input
- add text mask (for example ##/##/##)

## Additional I created some useful extension functions

you can find it in EditTextExtensions.kt

- addTextListener - for easier adding text change listener (don't use with NumberWatcher)
- setActionDoneListener - for listen "done" event
- setNumberWatcher - for easier adding NumberWatcher

## How to use NumberWatcher:

You can see more examples in MainActivity.kt or NumberWatcherTest.kt

### For input only integer values
```kotlin
val integersInput = findViewById(R.id.integersInput)
val integersInputWatcherBuilder = NumberWatcher.Builder()
integersInputWatcherBuilder.numbersAfterDecimalPoint = 0 // because we need integers
integersInputWatcherBuilder.maxValue = 1000f // optional, for example max value 1000
integersInput.addTextChangedListener(integersInputWatcherBuilder.build(integersInput){
Log.d("integersInput", it)
})
```

### For input float with fixed numbers after decimal point
```kotlin
val floatsInput = findViewById(R.id.floatsInput)
val floatsInputWatcherBuilder = NumberWatcher.Builder()
floatsInputWatcherBuilder.numbersAfterDecimalPoint = 2 // optional, for example we need only to numbers after decimal point
floatsInputWatcherBuilder.maxValue = 1000f // optional, for example max value 1000
floatsInput.addTextChangedListener(floatsInputWatcherBuilder.build(floatsInput){
Log.d("floatsInput", it)
})
```

### For adding a string to the end of the input
```kotlin
val withEndStringInput = findViewById(R.id.withEndString)
val withEndStringInputWatcherBuilder = NumberWatcher.Builder()
withEndStringInputWatcherBuilder.numbersAfterDecimalPoint = 2 // optional, for example we need only to numbers after decimal point
withEndStringInputWatcherBuilder.maxValue = 100f // optional, for example max value 100
withEndStringInputWatcherBuilder.additional = "%" // optional, for example add % to the end of input
withEndStringInput.addTextChangedListener(withEndStringInputWatcherBuilder.build(withEndStringInput){
Log.d("withEndStringInput", it)
})
```

### If you want to allow users to enter values between -100 and 100
```kotlin
val negativeValuesInput = findViewById(R.id.negativeValuesInput)
val negativeValuesInputWatcherBuilder = NumberWatcher.Builder()
// optional, for example we need only to numbers after decimal point
negativeValuesInputWatcherBuilder.numbersAfterDecimalPoint = 2
negativeValuesInputWatcherBuilder.maxValue = 100f // optional, for example max value 100
negativeValuesInputWatcherBuilder.minValue = -100f // optional, for example min value -100
negativeValuesInputWatcherBuilder.allowNegativeValues = true
// optional, for example add % to the end of input
negativeValuesInputWatcherBuilder.additional = "%"
negativeValuesInput.addTextChangedListener(negativeValuesInputWatcherBuilder.build(negativeValuesInput) {
Log.d("negativeValuesInput", it)
})
```

### If you want to add a mask
```kotlin
val withMaskInput = findViewById(R.id.withMaskInput)
val withMaskInputWatcherBuilder = NumberWatcher.Builder()
// optional, for example we need only to numbers after decimal point
withMaskInputWatcherBuilder.numbersAfterDecimalPoint = 2
withMaskInputWatcherBuilder.maxValue = 1000000f // optional, for example max value 100
withMaskInputWatcherBuilder.minValue = -2000000f // optional, for example min value -200
withMaskInputWatcherBuilder.allowNegativeValues = true
// optional, for example add % to the end of input
withMaskInputWatcherBuilder.additional = "%"
withMaskInputWatcherBuilder.inputMask = "##/##/##"
withMaskInput.addTextChangedListener(withMaskInputWatcherBuilder.build(withMaskInput) {
Log.d("withMaskInput", it)
})
```