material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[TextField] Add option to allow suffix flow with the text

Open mahozad opened this issue 6 years ago • 6 comments
trafficstars

Description Finally, new prefix and suffix attributes have been added for text fields.
The suffix is fixed at the end of the text field and cannot be made to flow with the input text.

The solution Add the functionality and an option (attribute) to text field so that the developer can specify whether the suffix should be fixed or it should flow at the end of the input text.

Alternatives Currently, this feature can be implemented without using suffixText attribute.
It requires a text listener that appends the suffix when the text is changed.

See the related stack overflow post.

mahozad avatar Nov 09 '19 13:11 mahozad

Is there another workaround to temporary fix this issue? I've tried to manipulate layoutParams of com.google.android.material.R.id.textinput_suffix_text but it didn't worked.

TurKurT656 avatar May 18 '21 09:05 TurKurT656

Added a relevant link in the description.

mahozad avatar Dec 11 '21 08:12 mahozad

This can be done fairly easily in Compose like this:

@Composable
fun SuffixTest() {
    var text by remember { mutableStateOf("") }
    TextField(
        text,
        singleLine = true,
        visualTransformation = SuffixTransformer(" $"),
        onValueChange = { text = it }
    )
}
class SuffixTransformer(val suffix: String) : VisualTransformation {
    override fun filter(text: AnnotatedString): TransformedText {
        val result = text + AnnotatedString(suffix)
        return TransformedText(result, OffsetMapping.Identity)
    }
}

The above component probably can be used in Views too. See this post

Here is the related SO post. Also, thanks to Gabriele Mariotti for this answer on Stack Overflow.

mahozad avatar Nov 23 '22 08:11 mahozad

This can be done fairly easily in Compose like this:

@Composable
fun SuffixTest() {
    var text by remember { mutableStateOf("") }
    TextField(
        text,
        singleLine = true,
        visualTransformation = SuffixTransformer(" $"),
        onValueChange = { text = it }
    )
}
class SuffixTransformer(val suffix: String) : VisualTransformation {
    override fun filter(text: AnnotatedString): TransformedText {
        val result = text + AnnotatedString(suffix)
        return TransformedText(result, OffsetMapping.Identity)
    }
}

The above component probably can be used in Views too. See this post

Here is the related SO post. Also, thanks to Gabriele Mariotti for this answer on Stack Overflow.

Thank you for that, worked wonders!

loofv avatar Mar 16 '23 16:03 loofv

@mahozad Thank you for this answer/solution to be done with Jetpack, but still, I'm looking for the complete solution that I can do in XML with kotlin, if there's any excellent example like your question on SO that I can use please let me know..

vivekAtHorizon avatar Jul 06 '23 06:07 vivekAtHorizon

@vivekAtHorizon Did you figure out a way to get this done?

devanshc21 avatar Apr 30 '24 17:04 devanshc21