Last active
December 1, 2025 12:22
-
-
Save rubywai/9d68cac87466a0901b67ad5d0403798e to your computer and use it in GitHub Desktop.
@6 Scaffold Composable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.rubylearner.mycomposelesson | |
| import android.os.Bundle | |
| import android.widget.Toast | |
| import androidx.activity.ComponentActivity | |
| import androidx.activity.compose.setContent | |
| import androidx.activity.enableEdgeToEdge | |
| import androidx.compose.foundation.background | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.fillMaxWidth | |
| import androidx.compose.foundation.layout.height | |
| import androidx.compose.foundation.layout.offset | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.foundation.layout.size | |
| import androidx.compose.foundation.layout.width | |
| import androidx.compose.foundation.shape.CircleShape | |
| import androidx.compose.foundation.shape.RoundedCornerShape | |
| import androidx.compose.material.icons.Icons | |
| import androidx.compose.material.icons.filled.Close | |
| import androidx.compose.material.icons.filled.Notifications | |
| import androidx.compose.material.icons.filled.Person | |
| import androidx.compose.material3.Badge | |
| import androidx.compose.material3.BadgedBox | |
| import androidx.compose.material3.CenterAlignedTopAppBar | |
| import androidx.compose.material3.ExperimentalMaterial3Api | |
| import androidx.compose.material3.FabPosition | |
| import androidx.compose.material3.FloatingActionButton | |
| import androidx.compose.material3.Icon | |
| import androidx.compose.material3.IconButton | |
| import androidx.compose.material3.Scaffold | |
| import androidx.compose.material3.Text | |
| import androidx.compose.material3.TopAppBarDefaults | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.graphics.Brush | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.unit.dp | |
| import androidx.compose.ui.unit.sp | |
| import com.rubylearner.mycomposelesson.ui.theme.MyComposeLessonTheme | |
| //Scaffold related compose | |
| class MainActivity : ComponentActivity() { | |
| @OptIn(ExperimentalMaterial3Api::class) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| enableEdgeToEdge() | |
| setContent { | |
| MyComposeLessonTheme { | |
| Scaffold( | |
| topBar = { | |
| CenterAlignedTopAppBar( | |
| colors = TopAppBarDefaults.centerAlignedTopAppBarColors() | |
| .copy( | |
| containerColor = Color.Red, | |
| titleContentColor = Color.White, | |
| navigationIconContentColor = Color.White, | |
| actionIconContentColor = Color.White, | |
| ), | |
| title = { | |
| Text("Compose Lesson") | |
| }, | |
| navigationIcon = { | |
| IconButton( | |
| onClick = { | |
| Toast.makeText( | |
| this@MainActivity, | |
| "Close", | |
| Toast.LENGTH_SHORT | |
| ).show() | |
| } | |
| ) { | |
| Icon( | |
| Icons.Default.Close, | |
| contentDescription = "Close", | |
| ) | |
| } | |
| }, | |
| actions = { | |
| BadgedBox( | |
| badge = { | |
| Badge { Text("99") } | |
| } | |
| ) { | |
| IconButton( | |
| onClick = { | |
| } | |
| ) { | |
| Icon( | |
| Icons.Default.Notifications, | |
| contentDescription = "Notification", | |
| ) | |
| } | |
| } | |
| Text( | |
| "Setting", | |
| color = Color.White, | |
| ) | |
| } | |
| ) | |
| }, | |
| floatingActionButton = { | |
| FloatingActionButton( | |
| onClick = { | |
| } | |
| ) { | |
| Icon( | |
| Icons.Default.Person, | |
| contentDescription = "Add" | |
| ) | |
| } | |
| }, | |
| floatingActionButtonPosition = FabPosition.Start, | |
| bottomBar = { | |
| Box( | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .height(100.dp) | |
| .background(Color.Red) | |
| ) | |
| }, | |
| ) { | |
| Box(modifier = Modifier.padding(it)) { | |
| MyCompose() | |
| } | |
| } | |
| } | |
| } | |
| } | |
| @Composable | |
| fun MyCompose() { | |
| Box( | |
| modifier = Modifier | |
| .size(300.dp) | |
| .background( | |
| brush = Brush.sweepGradient( | |
| colors = listOf( | |
| Color.Red, | |
| Color.Blue, | |
| Color.Black | |
| ) | |
| ), | |
| shape = RoundedCornerShape( | |
| topStart = 20.dp, | |
| bottomStart = 50.dp | |
| ), | |
| ) | |
| .padding(bottom = 50.dp), | |
| contentAlignment = Alignment.Center | |
| ) { | |
| Box( | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .height(50.dp) | |
| .background(Color.Red) | |
| ) | |
| Box( | |
| modifier = Modifier | |
| .width(50.dp) | |
| .height(50.dp) | |
| .offset(y = 25.dp) | |
| .background( | |
| Color.Green, | |
| shape = CircleShape, | |
| ) | |
| ) | |
| Text( | |
| "Box Lesson", | |
| fontSize = 30.sp, | |
| color = Color( | |
| // 0 -255 | |
| red = 255, | |
| green = 255, | |
| blue = 255, | |
| alpha = 255, | |
| ), | |
| modifier = Modifier | |
| .align(alignment = Alignment.BottomCenter) | |
| ) | |
| Text( | |
| "Hello world", | |
| color = Color.White, | |
| ) | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment