Implement Markdown Text in Android using Jetpack Compose
Last Updated :
24 Mar, 2022
Markdown is a third-party git library that can be used to identify particular types of regex (Regular Expressions) that specifies a search pattern in the text. For example, if you write a passage and mention a website and a telephone number in it, Markdown Text would detect these patterns and create a hyperlink around them to open them in their respective applications. In simple words, it auto-generates clickable links on them.
So in this article, we will show you how you could implement Markdown Text in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Add Jitpack in settings.gradle file
Navigate Gradle Scripts > settings.gradle and add Jitpack as shown below.
Kotlin
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "JCMarkDown"
include ':app'
|
Step 3: Add Markdown Dependency in app build.gradle file
Navigate to Gradle Scripts > build.gradle (Module) and add the below dependency in the dependencies{…}
Kotlin
implementation 'com.github.jeziellago:compose-markdown:0.2.6'
|
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.geeksforgeeks.jcmarkdown
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import dev.jeziellago.compose.markdowntext.MarkdownText
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContent {
MainContent()
}
}
}
@Composable
fun MainContent() {
Scaffold(
topBar = { TopAppBar(title = { Text( "GFG | Markdown Text" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
content = { MyContent() }
)
}
@Composable
fun MyContent(){
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
MarkdownText(
"Phone: 1800 258 4458 " +
"Email: feedback@geeksforgeeks.org" ,
fontSize = 20 .sp
)
}
}
@Preview (showBackground = true )
@Composable
fun DefaultPreview() {
MainContent()
}
|
Output:
You can see that Markdown Text correctly identifies the website, phone number, and email id that we provided in the text and converts them into hyperlinks as shown in the below video.
Share your thoughts in the comments
Please Login to comment...