Color Picker in Android using Jetpack Compose
Last Updated : 16 Aug, 2022
Color Picker is seen in many paint applications and is used for drawing some components and coloring them. In these applications, we can get to see different color pickers which are used to pick the color and paint that specific component. In this article, we will take a look at How to implement Color Picker in Android using Jetpack Compose. A sample video is given below to get an idea about what we are going to do in this article.
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: Adding dependency in build.gradle file
Navigate to app > build.gradle and add the below dependency in the dependencies section.
implementation "com.github.skydoves:colorpicker-compose:1.0.0"
Now sync your project to install the dependency within the android application.
Step 3: Adding a new color to the Color.kt file
Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.
Kotlin package com.example.newcanaryproject.ui.theme import androidx.compose.ui.graphics.Color val Purple200 = Color(0xFF0F9D58) val Purple500 = Color(0xFF0F9D58) val Purple700 = Color(0xFF3700B3) val Teal200 = Color(0xFF03DAC5) // on below line we are adding different colors. val greenColor = Color(0xFF0F9D58)
Step 4: 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.example.newcanaryproject import android.app.Activity import android.content.Context import android.net.Uri import android.os.Bundle import android.util.DisplayMetrics import android.util.Log import android.view.WindowManager import android.widget.Chronometer import android.widget.MediaController import android.widget.Toast import android.widget.VideoView import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Add import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.* import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.imageResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.* import com.example.newcanaryproject.ui.theme.* import com.github.skydoves.colorpicker.compose.* import dev.shreyaspatil.easyupipayment.EasyUpiPayment import dev.shreyaspatil.easyupipayment.listener.PaymentStatusListener import dev.shreyaspatil.easyupipayment.model.PaymentApp import dev.shreyaspatil.easyupipayment.model.TransactionDetails import java.text.SimpleDateFormat import java.util.* class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { NewCanaryProjectTheme { // on below line we are specifying background color for our application Surface( // on below line we are specifying modifier and color for our app modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background ) { // on the below line we are specifying the theme as the scaffold. Scaffold( // in scaffold we are specifying the top bar. topBar = { // inside top bar we are specifying background color. TopAppBar(backgroundColor = greenColor, // along with that we are specifying title for our top bar. title = { // in the top bar we are specifying tile as a text Text( // on below line we are specifying // text to display in top app bar. text = "GFG", // on below line we are specifying // modifier to fill max width. modifier = Modifier.fillMaxWidth(), // on below line we are specifying // text alignment. textAlign = TextAlign.Center, // on below line we are specifying // color for our text. color = Color.White ) }) }) { // on below line we are // calling color picker method colorPicker() } } } } } } @Composable fun colorPicker() { // on below line we are creating a variable for controller val controller = rememberColorPickerController() // on below line we are creating a column, Column( // on below line we are adding a modifier to it, modifier = Modifier .fillMaxSize() // on below line we are adding a padding. .padding(all = 30.dp) ) { // on below line we are adding a row. Row( // on below line we are adding a modifier modifier = Modifier.fillMaxWidth(), // on below line we are adding horizontal // and vertical alignment. horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically ) { // on below line we are adding a alpha tile. AlphaTile( // on below line we are // adding modifier to it modifier = Modifier .fillMaxWidth() // on below line // we are adding a height. .height(60.dp) // on below line we are adding clip. .clip(RoundedCornerShape(6.dp)), // on below line we are adding controller. controller = controller ) } // on below line we are // adding horizontal color picker. HsvColorPicker( // on below line we are // adding a modifier to it modifier = Modifier .fillMaxWidth() .height(450.dp) .padding(10.dp), // on below line we are // adding a controller controller = controller, // on below line we are // adding on color changed. onColorChanged = {} ) // on below line we are adding a alpha slider. AlphaSlider( // on below line we // are adding a modifier to it. modifier = Modifier .fillMaxWidth() .padding(10.dp) .height(35.dp), // on below line we are // adding a controller. controller = controller, // on below line we are // adding odd and even color. tileOddColor = Color.White, tileEvenColor = Color.Black ) // on below line we are // adding a brightness slider. BrightnessSlider( // on below line we // are adding a modifier to it. modifier = Modifier .fillMaxWidth() .padding(10.dp) .height(35.dp), // on below line we are // adding a controller. controller = controller, ) } }
Now run your application to see the output of it.
Output:
Similar Reads
Image Color Picker in Android using Jetpack Compose Many android applications require functionality in which the user should be able to pick the color from the image which is present within the android application. In this article, we will take a look at How to implement Image Color Picker in Android applications using Jetpack Compose. We will be abl
5 min read
Pie Chart in Android using Jetpack Compose Pie Charts are used in many Android applications to display a huge quantity of data in a simple and easy format. This is seen in applications where a huge quantity of data is to be handled. In this article, we will take a look at How to Create a Pie Chart in Android using Jetpack Compose. A sample v
7 min read
Date Picker in Android using Jetpack Compose In Android, a Date Picker is a widget used to select a date from the calendar. When a Date Picker is implemented in an application, the user can select a year, a month, and a day, combining to form date from a calendar-like-looking box. This data is generally needed and collected in applications tha
2 min read
Time Picker in Android using Jetpack Compose In Android, a Time Picker is a modern UI element, used in applications like Alarm and Reminders, that requires users to select a particular time for performing a particular task. It is a user interface control for selecting the time in either 24-hour format or 12-hour (AM/PM) mode. In this article,
3 min read
Contact Picker in Android using Jetpack Compose Many applications require a functionality where users can pick a specific contact from their contact list and display that contact within the android application to perform some action on that contact such as sending an SMS or making a call. In this article, we will take a look at How to create a co
6 min read