Android Jetpack Compose – Text Center Align

Android Jetpack Compose – Text Center Align

To center align text in Text composable in Android Jetpack Compose, you can set the textAlign parameter of the Text composable with TextAlign.Center (androidx.compose.ui.text.style.TextAlign) value.

Example

In the following example, we center aligned the text of the Text composable using textAlign parameter.

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.text.style.TextAlign
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 some more",
                        fontSize = 25.sp,
                        textAlign = TextAlign.Center
                    )
                }
            }
        }
    }
}

Screenshot

Android Jetpack Compose - Text Center Align
Text textAlign=TextAlign.Center

Summary

In this tutorial, we have seen how to center align text in Text composable in Android Jetpack Compose, using textAlign parameter of Text composable.