Text in Jetpack Compose

Hello Developers, Welcome to Techpass Master. In this tutorial, We are going to learn, How to Add a Simple Text in Jetpack Compose Using Kotlin with Android Studio.

Text in Jetpack Compose

First Open your Android Studio project and Create a @Composable Function

Simple Text

@Composable
fun SimpleText(name: String) {
    Text(text = "Techpass $name!")
}

Now call this composable function under setContent

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.tooling.preview.Preview
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
                ) {
                    SimpleText("Master")
                }
            }
        }
    }
}

@Composable
fun SimpleText(name: String) {
    Text(text = "Techpass $name!")
}

Now run and test your app

Leave a Reply

Your email address will not be published. Required fields are marked *