Until recently, I had always used XML’s to create my screens, and I knew how to make certain TextViews clickable with those XML’s.
Now, I’m transitioning to exclusively use Jetpack Compose, and no longer depend on XML.
What I’m currently trying to do is have one part of a text have a keyword that (if clicked) will let you go to another screen on the app.
For example, if my text says "Click here to go to the second screen
", then when you click on "here" it should take you to the other screen. I looked at resources online for Compose, which have suggested using ClickableText
, but this makes the whole sentence clickable while just making "here" a different color, which is not what I want.
For compose, what would be the best way for me to have a Text which can have the sentence above but only have one word ("here") clickable and do the click acion?
2
Answers
You can achieve above thing by
buildAnnotatedString
. Define your text here and pass in clickable textonly
here
text is clickable , Now when you click onhere
a toast will show , you can modify according to your requirementThe correct answer is already set, i’ll just try to expand it and make it more reusable, you can do something like this and create your own composable:
First, a text that can be highlighted:
Then a Highlighted text that can be clicked:
With that you will be able to tell wich words/sentences you want to be clickable and the actions it should call when clicked as well as highlight sentences, in case you need for filtering uses for instance.
Using as a clickable text:
Using as a highlight tool:
Hope it helps.