2

In Android using jetpack-compose, is there currently a way to display a text containing links in a @Composable Text?

In legacy TextView, we used Markwon with linkify plugin. Markwon creates a Spanned object that we can set into the TextView's text.

Is there a way to proceed with the same with @Composable Text? Or do we have to use a legacy TextView embedded within a @Composable AndroidView?

Thank you

gpo
  • 3,388
  • 3
  • 31
  • 53
  • https://stackoverflow.com/questions/66130513/linkify-with-compose-text and https://stackoverflow.com/questions/65567412/jetpack-compose-text-hyperlink-some-section-of-the-text/65656351#65656351 – Gabriele Mariotti Apr 26 '21 at 15:03

2 Answers2

4

I think this library can help you: https://github.com/jeziellago/compose-markdown

Add the repository to the project's build.gradle.

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' } // add this
    }
}

Then, add the dependency to the module's build.gradle

implementation 'com.github.jeziellago:compose-markdown:0.2.0'

and finally, you can use the library as follows:

MarkdownText(
    markdown = "Click [here](http://www.google.com) or http://www.stackoverflow.com."
)

In this sample, both links are detected.

nglauber
  • 18,674
  • 6
  • 70
  • 75
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/28842451) – stateMachine Apr 26 '21 at 21:27
  • @eldesgraciado edited for a better answer. – nglauber Apr 27 '21 at 01:12
0

Still used the old Markwon and used the AndroidView Composeable to display an legacy TextView in my case, you can pass around a Spanned object and set on the TextView. The Linkify Plugin should work too

Kitesurfer
  • 3,438
  • 2
  • 29
  • 47