How to Create Gradient Background in Android Jetpack Compose

Color gradients are helpful in enhancing the beauty of your mobile app. Let’s learn how to create a background with the color gradient in Jetpack Compose.

The color gradient things are part of the graphics. The Brush class comes in handy to create linear gradient, horizontal gradient, vertical gradient, radial gradient, etc.

Linear Gradient in Jetpack Compose

You can create a linear gradient using Brush and its linearGradient function. You also have options to choose multiple colors and offsets.

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Example()
                }
            }
        }
    }
}

@Composable
fun Example() {
    val gradient = Brush.linearGradient(
        0.0f to Color.Red,
        500.0f to Color.Blue,
        start = Offset.Zero,
        end = Offset.Infinite
    )
    Box(modifier = Modifier.background(gradient))

}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApplicationTheme {
        Example()
    }
}

See the output given below.

Horizontal Gradient in Jetpack Compose

You can create horizontal gradient background using Brush and the function horizontalGradient.

@Composable
fun Example() {
    val gradient = Brush.horizontalGradient(
        0.0f to Color.Red,
        1.0f to Color.Blue,
        startX = 0.0f,
        endX = 1000.0f
    )
    Box(modifier = Modifier.background(gradient))
}

Now, you can see the horizontal gradient of red and blue colors with the given offsets.

Vertical Gradient in Jetpack Compose

The Brush class and its verticalGradient functions help you to show vertical gradient background.

@Composable
fun Example() {
    val gradient = Brush.verticalGradient(
        0.0f to Color.Red,
        1.0f to Color.Blue,
        startY = 0.0f,
        endY = 1500.0f
    )
    Box(modifier = Modifier.background(gradient))
}

The output will have a vertical gradient background with red and blue colors.

Radial Gradient in Jetpack Compose

A radial gradient is a gradient that radiates from an origin.

@Composable
fun Example() {
    val gradient = Brush.radialGradient(
        0.0f to Color.Red,
        1.0f to Color.Blue,
        radius = 1500.0f,
        tileMode = TileMode.Repeated
    )
    Box(modifier = Modifier.background(gradient))
}

Following is the output.

That’s how you create different gradient backgrounds in Jetpack Compose.

2 thoughts on “How to Create Gradient Background in Android Jetpack Compose”

Leave a Comment