roughly Highlighting Textual content Enter with Jetpack Compose will lid the most recent and most present opinion relating to the world. retrieve slowly consequently you perceive competently and accurately. will bump your information skillfully and reliably
We lately launched a brand new function in Buffer, referred to as Concepts. With Concepts, you may retailer all of your greatest concepts, tweak them till they’re prepared, and put them straight into the Buffer queue. Now that Concepts has launched on our net and cellular apps, we’ve got time to share some learnings from creating this function. On this weblog publish, we’ll dive into how we added help for URL highlighting in Concepts Composer on Android, utilizing Jetpack Compose.
We began adopting Jetpack Compose into our app in 2021, utilizing it as a regular to construct all of our new options, whereas steadily adopting it into present components of our app. We constructed the whole Concepts function with Jetpack Compose, so together with sooner function growth and higher predictability inside our UI state, we had loads of alternatives to additional discover Compose and study extra about how one can obtain sure necessities in our software.
Inside the Concepts composer, we help dynamic hyperlink highlighting. Which means if you happen to sort a URL within the textual content space, the hyperlink can be highlighted; Tapping this hyperlink will show an “Open Hyperlink” popup, which can open the hyperlink within the browser when clicked.

On this weblog publish, we’ll deal with the implementation of hyperlink highlighting and the way this may be achieved in Jetpack Compose utilizing the TextField
composable
For the Concepts composer, we’re utilizing the TextField
composable to help textual content enter. This composable incorporates one argument, visualTransformation
used to use visible modifications to entered textual content.
TextField(
...
visualTransformation = ...
)
This argument requires a VisualTransformation
The implementation that’s used to use the visible transformation to the entered textual content. If we have a look at the supply code for this interface, we’ll discover a filter perform that takes the content material of the TextField and returns a TransformedText
reference containing the modified textual content.
@Immutable
enjoyable interface VisualTransformation
enjoyable filter(textual content: AnnotatedString): TransformedText
In terms of this modified textual content, we’re required to supply the implementation that creates a brand new AnnotatedString
reference with our modifications utilized. This modified content material is then packaged within the TransformedText
write and return to TextField
for the composition.
In order that we are able to outline and apply transformations to the content material of our TextField
we have to begin by creating a brand new implementation of the VisualTransformation
interface for which we’ll create a brand new class, UrlTransformation
. This class will implement the VisualTransformation
argument, along with taking a single argument within the type of Colour
. We outline this argument in order that we are able to go a theme coloration reference to be utilized inside our logic, since we’ll be exterior of composable scope and will not have entry to our composable theme.
class UrlTransformation(
val coloration: Colour
) : VisualTransformation
With this class outlined, we now must implement the filter perform from the VisualTransformation
Interface. Inside this perform we’re going to return an occasion of the TransformedText
class: We are able to bounce into the supply code of this class and see that two properties are required when creating an occasion of this class.
/**
* The remodeled textual content with offset offset mapping
*/
class TransformedText(
/**
* The remodeled textual content
*/
val textual content: AnnotatedString,
/**
* The map used for bidirectional offset mapping from unique to remodeled textual content.
*/
val offsetMapping: OffsetMapping
)
Each arguments are required, so we’ll want to produce a price for every when instantiating the TransformedText
class.
- textual content – this would be the modified model of the textual content that’s provided to the filter perform
- displacement task – based on the documentation, that is the map used for bidirectional displacement mapping from unique to remodeled textual content
class UrlTransformation(
val coloration: Colour
) : VisualTransformation
override enjoyable filter(textual content: AnnotatedString): TransformedText
return TransformedText(
...,
OffsetMapping.Id
)
For him offsetMapping
argument, we merely go the OffsetMapping.Id
worth – that is the predefined default worth used for the OffsetMapping
interface, used for when it may be used for textual content transformation that doesn’t change the variety of characters. In terms of the textual content argument, we’ll want to write down some logic that may take the present content material, apply the highlighting, and return it as new. AnnotatedString
reference to go to our TransformedText
reference. For this logic, we’re going to create a brand new perform, buildAnnotatedStringWithUrlHighlighting
. It will take two arguments: the textual content to be highlighted, together with the colour to make use of for the spotlight.
enjoyable buildAnnotatedStringWithUrlHighlighting(
textual content: String,
coloration: Colour
): AnnotatedString
From this perform, we have to return a AnnotatedString
reference, which we’ll create utilizing buildAnnotatedString
. Inside this perform, we’ll begin by utilizing the add operation to set the textual content material of the AnnotatedString
.
enjoyable buildAnnotatedStringWithUrlHighlighting(
textual content: String,
coloration: Colour
): AnnotatedString
return buildAnnotatedString
append(textual content)
Subsequent, we’ll must take the content material of our string and apply the highlighting to no matter URLs are current. Earlier than we are able to do that, we have to establish the URLs within the string. URL detection can fluctuate relying on the use case, so to maintain issues easy, let’s write some pattern code that finds the URLs in a given piece of textual content. This code will take the given string and filter the URLs, offering a listing of URL strings because of this.
textual content?.cut up("s+".toRegex())?.filter phrase ->
Patterns.WEB_URL.matcher(phrase).matches()
Now that we all know what URLs are within the string, we’ll want to use highlighting to them. This can be within the type of an annotated string model, which is utilized by the addStyle operation.
enjoyable addStyle(model: SpanStyle, begin: Int, finish: Int)
When calling this perform, we have to go the SpanStyle
that we need to apply, together with the beginning and finish index to which this model must be utilized. We’ll begin by calculating this begin and end fee; To maintain issues easy, we’ll assume that there are solely distinctive URLs in our string.
textual content?.cut up("s+".toRegex())?.filter phrase ->
Patterns.WEB_URL.matcher(phrase).matches()
.forEach
val startIndex = textual content.indexOf(it)
val endIndex = startIndex + it.size
Right here we find the beginning index utilizing the indexOf
perform, which can give us the beginning index of the given URL. We are going to then use this beginning index and the size of the URL to calculate the ending index. We are able to then go these values to the corresponding arguments for the addStyle
perform.
textual content?.cut up("s+".toRegex())?.filter phrase ->
Patterns.WEB_URL.matcher(phrase).matches()
.forEach
val startIndex = textual content.indexOf(it)
val endIndex = startIndex + it.size
addStyle(
begin = startIndex,
finish = endIndex
)
Subsequent, we should present the SpanStyle
which we need to apply to the given index vary. Right here we need to merely spotlight the textual content utilizing the offered coloration, so we’ll go the colour worth of our perform arguments as the colour argument to the SpanStyle
perform.
textual content?.cut up("s+".toRegex())?.filter phrase ->
Patterns.WEB_URL.matcher(phrase).matches()
.forEach
val startIndex = textual content.indexOf(it)
val endIndex = startIndex + it.size
addStyle(
model = SpanStyle(
coloration = coloration
),
begin = startIndex,
finish = endIndex
)
With this in place, we now have a whole perform that may take the textual content offered and spotlight any URL utilizing the Colour
reference.
enjoyable buildAnnotatedStringWithUrlHighlighting(
textual content: String,
coloration: Colour
): AnnotatedString
return buildAnnotatedString
append(textual content)
textual content?.cut up("s+".toRegex())?.filter phrase ->
Patterns.WEB_URL.matcher(phrase).matches()
.forEach
val startIndex = textual content.indexOf(it)
val endIndex = startIndex + it.size
addStyle(
model = SpanStyle(
coloration = coloration,
textDecoration = TextDecoration.None
),
begin = startIndex, finish = endIndex
)
Then we should bounce again to our UrlTransformation
class and go the results of the buildAnnotatedStringWithUrlHighlighting
perform name for the TransformedText
plot.
class UrlTransformation(
val coloration: Colour
) : VisualTransformation
override enjoyable filter(textual content: AnnotatedString): TransformedText
return TransformedText(
buildAnnotatedStringWithUrlHighlighting(textual content, coloration),
OffsetMapping.Id
)
now that our UrlTransformation
implementation is full, we are able to instantiate this and go the reference to the visualTransformation
argument of the TextField
composable Right here we’re utilizing the specified coloration from our MaterialTheme
reference, which can be used when highlighting URLs in our TextField
contents.
TextField(
...
visualTransformation = UrlTransformation(
MaterialTheme.colours.secondary)
)
With the above in place, we now have dynamic URL highlighting help inside our TextField
composable Which means now at any time when the person inserts a URL into an concept composer, we establish it as a URL by highlighting it with our theme’s secondary coloration.

On this publish, we’ve got realized how we are able to apply dynamic URL highlighting to the content material of a TextField
composable Within the subsequent publish, we’ll discover how we add the “Open Hyperlink” popup when a URL is tapped contained in the composer enter space.
I want the article roughly Highlighting Textual content Enter with Jetpack Compose provides acuteness to you and is beneficial for rely to your information