Underline Text in Jetpack Compose – TextDecoration Examples

Hello Developers, Welcome to Jetpack Compose tutorials. In this tutorial, We are going to learn How to Underline Text in Jetpack Compose Using Kotlin with Android Studio.

Underline Text in Jetpack Compose

First Open your Android Studio project and Create a Composable Function for Underline Text

UnderlineText @Composable Function

@Composable
fun UnderlinedText() {
    Text(text = "Text with Underline",
        textDecoration = TextDecoration.Underline)
}

The complete example code is given below

package com.techpassmaster.jetpackcomposetextandstyles

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextDecoration
import com.techpassmaster.jetpackcomposetextandstyles.ui.theme.JetpackComposeTextAndStylesTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JetpackComposeTextAndStylesTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    // this function is used for underline text
                    UnderlinedText()
                }
            }
        }
    }
}

@Composable
fun UnderlinedText() {
    Text(text = "Text with Underline",
        textDecoration = TextDecoration.Underline)
}

Now run and test the app you will get the text with an underline.

Underline only part of the text

To underline one word or phrase inside a sentence, build an AnnotatedString and apply a SpanStyle with TextDecoration.Underline to just that part:

@Composable
fun PartlyUnderlinedText() {
    Text(
        text = buildAnnotatedString {
            append("Read our ")
            withStyle(SpanStyle(textDecoration = TextDecoration.Underline)) {
                append("terms and conditions")
            }
            append(" before you continue.")
        }
    )
}

Underline and strikethrough together

Use TextDecoration.combine() to apply more than one decoration at the same time:

Text(
    text = "Underlined and struck through",
    textDecoration = TextDecoration.combine(
        listOf(TextDecoration.Underline, TextDecoration.LineThrough)
    )
)

Underlined, clickable link

For a link inside text (Compose 1.7 and newer), use LinkAnnotation.Url with withLink. The underline comes from TextLinkStyles, and the link opens in the browser when tapped:

Text(
    text = buildAnnotatedString {
        append("Visit ")
        withLink(
            LinkAnnotation.Url(
                url = "https://developer.android.com",
                styles = TextLinkStyles(
                    style = SpanStyle(
                        color = Color(0xFF3D63E6),
                        textDecoration = TextDecoration.Underline
                    )
                )
            )
        ) {
            append("Android Developers")
        }
    }
)

Material 3 note: new projects use Material 3, so import androidx.compose.material3.Text instead of androidx.compose.material.Text. The textDecoration parameter works the same way in both.

You May Also Like ⇣
WhatsApp us📞 Call now