Monday, September 13, 2021

jetpack compose - Box alignment

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold(){
        Scaffold(
            topBar = {TopAppBar(
                title = {Text(
                    "Compose - Box Alignment",
                    color = Color.White)},
                backgroundColor = Color(0xFF333399)) },
            content = { MainContent()}
        )
    }


    @Composable
    fun MainContent(){
        Column(
            Modifier.padding(8.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ){
            Box(
                Modifier.fillMaxWidth().height(100.dp)
                    .background(color = Color(0xFFDE5D83))
                    .padding(8.dp),
                contentAlignment = Alignment.Center
            ){ Text("Center") }

            Box(
                Modifier.fillMaxWidth().height(100.dp)
                    .background(color = Color(0xFFF88379))
                    .padding(8.dp),
                contentAlignment = Alignment.BottomEnd
            ){ Text("Bottom End") }

            Box(
                Modifier.fillMaxWidth().height(100.dp)
                    .background(color = Color(0xFFFFBCD9))
                    .padding(8.dp),
                contentAlignment = Alignment.TopCenter
            ){ Text("Top Center") }


            Box(
                Modifier.fillMaxWidth().height(100.dp)
                    .background(color = Color(0xFFE4D00A))
                    .padding(8.dp),
                contentAlignment = Alignment.BottomStart
            ){ Text("Bottom Start") }
        }
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        //GetScaffold()
    }
}