Thursday, November 4, 2021

jetpack compose - Background brush

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.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.unit.dp


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedObjectState: Bundle?) {
        super.onCreate(savedObjectState)
        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold() {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = {
                        Text(
                            text = "Compose - Background Brush"
                        )
                    },
                    backgroundColor = Color(0xFF1ca9c9),
                )
            },
            content = { MainContent() },
        )
    }


    @Composable
    fun MainContent(){
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(12.dp),
            modifier = Modifier.padding(12.dp)
        ) {
            Box(modifier = Modifier
                .background(
                    brush = SolidColor(Color.LightGray),
                    shape = RectangleShape,
                    alpha = 0.5F
                )
                .fillMaxWidth()
                .weight(1F)
                .padding(12.dp)
            ){
                Text(text = "Background solid color brush," +
                        " rectangle shape, alpha 0.5F")
            }

            Box(modifier = Modifier
                .background(
                    brush = SolidColor(Color.Blue),
                    shape = RoundedCornerShape(24.dp),
                    alpha = 0.25F
                )
                .fillMaxWidth()
                .weight(2F)
                .padding(12.dp)
            ){
                Text(text = "Background solid color brush," +
                        " rounded rectangle shape, alpha 0.25F")
            }

            Box(modifier = Modifier
                .background(
                    brush = Brush.horizontalGradient(
                        listOf(Color.Blue,Color.Red)),
                    shape = RoundedCornerShape(24.dp),
                    alpha = 0.25F
                )
                .fillMaxWidth()
                .weight(3F)
                .padding(12.dp)
            ){
                Text(text = "Background horizontal gradient color brush," +
                        " rounded rectangle shape, alpha 0.25F")
            }
        }
    }
}