Open In App

Color Gradient in Android using Jetpack Compose

Improve
Improve
Like Article
Like
Save
Share
Report

In computer graphics, a color gradient specifies a range of position-dependent colors, usually used to fill a region. More than one color is displayed in a gradient where there is a color transition between any two colors. Most commonly, this transition could be vertical, horizontal, or radial. In this article, we will show you how you could create different Color Gradients in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jccolorgradient
  
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Creating a Simple Scaffold 
            // Layout for the application
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Gradient Color Example", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // Declaring 2 Colors
                        val colorGreen = Color.Green
                        val colorRed = Color.Red
  
                        // Creating a Horizontal Gradient Color
                        val gradientGreenRed = Brush.horizontalGradient(0f to colorGreen, 1000f to colorRed)
  
                        // Creating a sample Row and setting is 
                        // background with above Gradient Color
                        Row(
                            Modifier
                                .fillMaxWidth()
                                .height(100.dp)
                                .background(gradientGreenRed)) {
                            Text(text = "Horizontal Gradient Green to Red", fontSize = 20.sp)
                        }
  
                        // Adding a Space of height 100dp
                        Spacer(modifier = Modifier.height(100.dp))
  
                        // Declaring 2 Colors
                        val colorGray = Color.Gray
                        val colorWhite = Color.White
  
                        // Creating a Vertical Gradient Color
                        val gradientGrayWhite = Brush.verticalGradient(0f to colorGray, 1000f to colorWhite)
  
                        // Creating a sample Row and setting is 
                        // background with above Gradient Color
                        Row(
                            Modifier
                                .fillMaxWidth()
                                .height(100.dp)
                                .background(gradientGrayWhite)) {
                            Text(text = "Vertical Gradient Gray to White", fontSize = 20.sp)
                        }
  
                        // Adding a Space of height 100dp
                        Spacer(modifier = Modifier.height(100.dp))
  
                        // Declaring 4 Colors
                        val colorCyan = Color.Cyan
                        val colorBlack = Color.Black
                        val colorBlue = Color.Blue
                        val colorMagenta = Color.Magenta
  
                        // Creating a Radial Gradient Color
                        val gradientRadial = Brush.radialGradient(listOf(colorCyan, colorBlack, colorBlue, colorMagenta))
  
                        // Creating a sample Row and setting is 
                        // background with above Gradient Color
                        Row(
                            Modifier
                                .fillMaxWidth()
                                .height(100.dp)
                                .background(gradientRadial)) {
                            Text(text = "Radial Gradient", fontSize = 20.sp)
                        }
                    }
                }
            )
        }
    }
}


Output:

You can see that we have created horizontal, vertical, and radial color gradients as shown below.

Output

 



Last Updated : 26 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads