In this example we create CenteredYellowColumn as customizes Column with yellow background and centered Views.
This Custom Container also has additional parameter title which is used to place additional Text View at the top.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CenteredYellowColumn(title = "People") {
Text("John")
Text("Bob")
Text("Susan")
}
}
}
}
@Composable
fun CenteredYellowColumn(title: String, content: @Composable () -> Unit) {
Surface(color = Color.Yellow) {
Column(
Modifier.fillMaxSize(),
horizontalGravity = Alignment.CenterHorizontally
) {
Text(title, fontWeight= FontWeight.Bold, fontSize = 20.sp)
content()
}
}
}
Output