Friday, September 24, 2021

jetpack compose - Box gradient background color

Compose Box Gradient Background Color
The Box is a layout widget of the android jetpack compose library. The Box is a single-child container. The Box layout is helpful to align the child element in a specific position.

The following jetpack compose tutorial demonstrates to us how we can set a gradient background color to a Box layout. The Box constructor has no specific argument to set or change its background color. So how do we set a background color to it?

As with other layout widgets, we can set a background color to a Box widget by using its ‘modifier’ argument’s ‘background’ element. The modifier’s ‘background()’ function element has several constructors to specify a background color or brush for the widget.

The ’background()’ element has a constructor that accepts a ‘brush’ argument. The ‘brush’ argument data type is ‘Brush’. So we can pass a Brush instance to the Box widget to draw its background. We can create any type of gradient brush to draw a gradient background to the Box surface.

There are several types of gradient brushes such as horizontal gradient, vertical gradient, sweep gradient, linear gradient, and radial gradient brush. In this example kotlin code, we create two brushes for the two Box widgets, one is a linear gradient brush and another one is a radial gradient brush. Here we simply pass a list of colors to create a linear gradient background and a radial gradient background for Box widgets. The Brush constructors also allow us to customize it using various arguments.

This jetpack compose tutorial code is written in an android studio IDE. Copy the code and run it on an emulator device or a real device to test how we display a gradient background color to the Box widget surface. We also displayed a screenshot of this tutorial’s emulator screen at the bottom of this tutorial.
MainActivity.kt

package com.cfsuman.jetpackcompose

import android.annotation.SuppressLint
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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

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

        setContent {
            GetScaffold()
        }
    }


    @SuppressLint("UnusedMaterialScaffoldPaddingParameter")
    @Composable
    fun GetScaffold(){
        Scaffold(
            topBar = {TopAppBar(
                title = {Text(
                    "Compose - Box Gradient Background",
                    color = Color.White)},
                backgroundColor = Color(0xFF58427C)) },
            content = {MainContent()},
            backgroundColor = Color(0xFFEDEAE0)
        )
    }


    @Composable
    fun MainContent(){
        Column(
            modifier = Modifier
                .fillMaxSize(),
            verticalArrangement = Arrangement.SpaceEvenly
        ){
            Box(Modifier
                .padding(12.dp)
                .fillMaxWidth()
                .height(250.dp)
                .clip(RoundedCornerShape(12.dp))
                .background(
                    Brush.linearGradient(
                    listOf(
                        Color(0xFF9C27B0),
                        Color(0xFFFF9800)
                    )
                ))
            )

            Box(Modifier
                .padding(12.dp)
                .fillMaxWidth()
                .height(250.dp)
                .clip(RoundedCornerShape(12.dp))
                .background(Brush.radialGradient(
                    listOf(
                        Color(0xFFE30022),
                        Color(0xFF00BFFF),
                        Color(0xFFDFFF00)
                    )
                ))
            )
        }
    }
}