# Hands on Jetpack Compose VisualTransformation to create a phone number formatter

DevFeed: [Hands on Jetpack Compose VisualTransformation to create a phone number formatter](<https://devfeed.tech/articles/hands-on-jetpack-compose-visualtransformation-to-create-a-phone-number-formatter-25943.md>)

Original publisher: [Read original article](<https://medium.com/google-developer-experts/hands-on-jetpack-compose-visualtransformation-to-create-a-phone-number-formatter-99b0347fc4f6?source=rss-cbfc736ddcd3------2>)

Author: Julien Salvi

Published: 2021-12-21T15:05:42Z

Content type: tutorial

Language: en

Sources: [Stories by Julien Salvi on Medium](<https://devfeed.tech/sources/stories-by-julien-salvi-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Compose](<https://devfeed.tech/topics/compose.md>), [Design system](<https://devfeed.tech/topics/design-system.md>), [ui](<https://devfeed.tech/topics/ui.md>), [Google](<https://devfeed.tech/topics/google.md>)

Tags: [aircall](<https://devfeed.tech/tags/aircall.md>), [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [component](<https://devfeed.tech/tags/component.md>), [compose](<https://devfeed.tech/tags/compose.md>), [constructor](<https://devfeed.tech/tags/constructor.md>), [design-system](<https://devfeed.tech/tags/design-system.md>), [filter](<https://devfeed.tech/tags/filter.md>), [function](<https://devfeed.tech/tags/function.md>), [google](<https://devfeed.tech/tags/google.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [mapping](<https://devfeed.tech/tags/mapping.md>), [parameter](<https://devfeed.tech/tags/parameter.md>), [transformation](<https://devfeed.tech/tags/transformation.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This tutorial explains how to use Jetpack Compose's VisualTransformation to format phone-number input for international display. It covers the filter() function, TransformedText, and OffsetMapping for mapping positions between original and transformed text.

## Source excerpt

At Aircall, we are currently rewriting our design system with Jetpack Compose, the new UI framework made by Google. One of our UI component is a text input that formats a phone number to its international format according to its origin country. With the View UI toolkit, we can use a PhoneNumberFormattingTextWatcher, which is available since API 1, to format the input text. But no equivalent has been implemented with Jetpack Compose so far. In this article we are going to cover how we managed to create a reliable alternative for Compose with VisualTransformation. VisualTransformation explained First, let's have a look at the VisualTransformation interface to understand how it works under the hood and how you can use it to format the input text of a TextField as you wish. https://medium.com/media/bb42609ef6212c4b0f63f06f83c0534c/href In order to filter the text, you will need to implement the filter() function which takes the original text to transform as a parameter and returns a TransformedText where all the magic happens! If we take a look at the constructor it takes a text (which will be transformed) and an OffsetMapping which provides a bidirectional mapping between the original text and the new one. Which means that we'll need to implement the transformation from and to the original. Let's have a look at the password transformation that the framework provides to see the OffsetMapping in action: https://medium.com/media/5e50cd5af8fd0d3acd9300d03665735b/href Here, the implementation is pretty straightforward as we just need to transform the input text where every character will be replaced by a mask, so the text will have the same length. The transformed text will contain the mapped text and the offsetMapping will handle the transformation. Here, as we don't have any extra character to be displayed in the returned text, the two functions will return the original offset. The two transformations can become a bit more complex if we need to display additional characte