0

I'm using an OutlinedTextField for the user to input a dollar amount. It works as desired but I would like to add a $ in front of what they are inputting, whether it's one digit or five.

val patternPool: Regex = Regex("^\\d{1,5}\$")

 OutlinedTextField(
            value = siteValues.valueExtraMoney.value,
            onValueChange = { if (it.isEmpty() || it.matches(sitePatterns.patternPool)) siteValues.valueExtraMoney.value = it },
            modifier = Modifier
                .weight(1f)
                .fillMaxHeight(),
            textStyle = TextStyle(fontSize = fonts.fontSizeText, textAlign = TextAlign.Center),
            label = {
                Text(
                    text ="Extra money taken out of prize pool",
                    modifier = Modifier.fillMaxWidth(),
                    fontSize = fonts.fontSizeEntry,
                    textAlign = TextAlign.Center
                )
            },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            shape = RectangleShape,
            colors = TextFieldDefaults.outlinedTextFieldColors(textColor = Color.Black)
        )

I've tried valueExtraMoney.value = "$$it". That outputs a $ plus what ever digit is inputed. But now the string doesn't satisfy the regex and I can't alter the input anymore. I tried playing with the regex to allow a $ but it still gets frozen.

JD74
  • 257
  • 1
  • 8
  • 1
    You can use a [VisualTransformation](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/VisualTransformation) – gpunto Mar 05 '23 at 10:11

0 Answers0