Android Jetpack Compose – Text Maximum Lines

Android Jetpack Compose – Text Maximum Number of Lines

To set maximum number of lines of text that can be displayed in a Text composable in Android Jetpack Compose, you can set the maxLines parameter with the required integer value.

Example

In the following example, we have set the text of the Text composable to a maximum of two lines.

If you do not specify any value for maxLines parameter, the Text composable would display three lines, as there as three lines.

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 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 sample text.\nAnd another line.",
                        maxLines = 2
                    )
                }
            }
        }
    }
}

Screenshot

Android Jetpack Compose - Text Maximum Number of Lines
Text maxLines=2

Summary

In this tutorial, we have seen how to set maximum number of lines of text in Text composable to be displayed, in Android Jetpack Compose, using maxLines parameter of Text composable.