Created
August 15, 2022 14:58
-
-
Save fanjavaid/d7da3451db77bcc2514dd652ebf78a25 to your computer and use it in GitHub Desktop.
Formatting input to thousand separator Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun ThousandFormatTextField() { | |
var text by remember { | |
mutableStateOf(TextFieldValue("")) | |
} | |
TextField( | |
value = text, | |
onValueChange = { newInput -> | |
val newValue = if (newInput.text.isNotBlank()) { | |
newInput.text | |
.clearThousandFormat() | |
.toLong() | |
.formatThousand() | |
} else newInput.text | |
text = newInput.copy( | |
text = newValue, | |
selection = TextRange(newValue.length) | |
) | |
}, | |
placeholder = { | |
Text(text = "Using textfieldvalue") | |
}, | |
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment