Wednesday, September 15, 2021

jetpack compose - Column border

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.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
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.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 - Column Border",
                    color = Color.White)},
                backgroundColor = Color(0xFF333399)) },
            content = {Column(
                Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.SpaceEvenly,
                horizontalAlignment = Alignment.CenterHorizontally
            )
            {MainContent()}},
            backgroundColor = Color(0xFFEDEAE0)
        )
    }


    @Composable
    fun MainContent(){
        Column(
            Modifier
                .background(Color(0xFFED872D))
                .border(2.dp,Color.Black)
                .fillMaxWidth(0.9f)
                .height(150.dp)
                .padding(12.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ){
            Text("Column Border")
        }

        Column(
            Modifier
                .border(2.dp,Color.Green, shape = RoundedCornerShape(16.dp))
                .background(Color(0xFFED872D), RoundedCornerShape(16.dp))
                .fillMaxWidth(0.9f)
                .height(150.dp)
                .padding(12.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ){
            Text("Column Border With Shape")
        }

        Column(
            Modifier
                .border(
                    5.dp,
                    brush = Brush.horizontalGradient(
                        listOf(Color.Red,Color.Blue)
                    ),
                    shape = RoundedCornerShape(16.dp)
                )
                .fillMaxWidth(0.9f)
                .height(150.dp)
                .padding(12.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ){
            Text("Column Gradient Border")
        }
    }


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