Sunday, March 7, 2021

jetpack compose - Box layout example

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp

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

        setContent {
            MainContent()
        }
    }


    @Composable
    fun MainContent(){
        Box(
            Modifier
                .fillMaxSize()
                .padding(16.dp)
        ) {
            Button(onClick = {
                // do something
            },
                modifier = Modifier.align(Alignment.TopCenter)
            ) {
                Text(text = "Top Center" )
            }

            Button(onClick = { },
                colors = ButtonDefaults.textButtonColors(
                    backgroundColor = Color(0xFF9FA91F),
                    contentColor = Color(0xFFFFF8DC)
                ),
                modifier = Modifier.align(Alignment.BottomStart)
            ) {
                Text(text = "Bottom Start" )
            }

            Button(onClick = { },
                colors = ButtonDefaults.textButtonColors(
                    backgroundColor = Color(0xFFC32148),
                    contentColor = Color(0xFFFFFF99)
                ),
                modifier = Modifier.align(Alignment.BottomEnd)
            ) {
                Text(text = "Bottom End" )
            }
        }
    }


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