material-components-android
material-components-android copied to clipboard
[TextField] Add option to allow suffix flow with the text
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.
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.
Added a relevant link in the description.
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.
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!
@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 Did you figure out a way to get this done?