Android Jetpack Compose – Text line height

Android Jetpack Compose – Text line height

Line height specifies the height of each line in the paragraph displayed in the Text composable.

To set a specific line height for a Text composable in Android Jetpack Compose, you can set the lineHeight parameter of the Text composable with the required TextUnit value. For example, lineHeight = 50.sp sets the line height to fifty scaled pixels.

Example

In the following example, we have set the lineHeight parameter of the Text composable to 50.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\nThis is some text\nAnd another text",
                        fontSize = 25.sp,
                        lineHeight = 50.sp
                    )
                }
            }
        }
    }
}

Screenshot

Android Jetpack Compose - Text line height - 50.sp
Text lineHeight = 50.sp

Now, let us change the line height to 100.sp, and observe the output.

Text(
    text = "Hello Android\nThis is some text\nAnd another text",
    fontSize = 25.sp,
    lineHeight = 100.sp
)

Screenshot

Android Jetpack Compose - Text line height - 100.sp
Text lineHeight = 100.sp

Summary

In this tutorial, we have seen how to set the line height for Text composable in Android Jetpack Compose, using lineHeight parameter of Text composable.