Android Jetpack Compose – Text Letter Spacing

Android Jetpack Compose – Text Letter Spacing

Letter spacing is the amount of space added between two letters in the text.

To set a letter spacing for the text in Text composable in Android Jetpack Compose, you can set the letterSpacing parameter of the Text composable with the required TextUnit value. For example, letterSpacing = 5.sp sets the letter spacing to five scaled pixels.

Example

In the following example, we have set the letterSpacing parameter of the Text composable to 5.sp.

MainActivity.kt

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Text(
                        text = "Hello Android",
                        fontSize = 25.sp,
                        letterSpacing = 5.sp
                    )
                }
            }
        }
    }
}

Screenshot

Android Jetpack Compose - Text Letter Spacing - 5.sp
Text letterSpacing=5.sp

Now, let us change the letter spacing to 10.sp, and observe the output.

Text(
    text = "Hello Android",
    fontSize = 25.sp,
    letterSpacing = 10.sp
)

Screenshot

Android Jetpack Compose - Text Letter Spacing - 10.sp
Text letterSpacing=10.sp

Summary

In this tutorial, we have seen how to set the letter spacing for Text composable in Android Jetpack Compose, using letterSpacing parameter of Text composable.