Image Color Picker in Android using Jetpack Compose
Last Updated :
16 Aug, 2022
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 able to pick the colors from the image within the android application. 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
// image color picker function
imageColorPicker()
}
}
}
}
}
}
@Composable
fun imageColorPicker() {
// on below line we are creating a variable
// for controller and initializing it.
val controller = rememberColorPickerController()
// on below line we are creating a column
// and adding a modifier to it.
Column(modifier = Modifier.fillMaxSize()) {
// on below line we are adding a spacer to it.
Spacer(modifier = Modifier.height(30.dp))
// on below line we are creating an image color picker.
ImageColorPicker(
// on below line we are adding a modifier to it.
modifier = Modifier
.fillMaxWidth()
// on below line we are adding height.
.height(300.dp)
// on below line we are adding a passing.
.padding(20.dp),
// on below line we are adding our image to pick color from
paletteImageBitmap = ImageBitmap.imageResource(R.drawable.img1),
// on below line we are
// adding a controller to it.
controller = controller,
// on below line we are adding a palette scale as fit.
paletteContentScale = PaletteContentScale.FIT,
// on below line we are checking for color change.
onColorChanged = { colorEnvelope: ColorEnvelope -> }
)
// on below line we are adding a spacer.
Spacer(modifier = Modifier.height(12.dp))
// on below line we are adding a row.
Row(
// on below line we
// are adding a modifier to it
modifier = Modifier
.fillMaxWidth()
// on below line we are
// adding a padding to it.
.padding(horizontal = 30.dp),
// on below line we are adding a
// 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 a modifier
modifier = Modifier
.fillMaxWidth()
// on below line
// we are adding a height
.height(60.dp)
// on below line we are
// adding a clip for round edges.
.clip(RoundedCornerShape(6.dp)),
// on below line we are adding a controller.
controller = controller
)
}
// on below line we are
// adding an alpha slider
AlphaSlider(
// on below line we are
// adding a modifier.
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(35.dp),
// on below line we are
// adding a controller
controller = controller,
)
// on below line we are
// creating a brightness slider.
BrightnessSlider(
// on below line we are
// adding modifier to
// add padding, height
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(35.dp),
// on below line we are
// adding a controller.
controller = controller,
)
}
}
Now run your application and see the output of it.
Output:
Similar Reads
Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho
11 min read
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
States of a Process in Operating Systems In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. Please refer Process in Operating Sys
11 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Kotlin Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read
Top 50 Flutter Interview Questions and Answers for 2025 Flutter is an open-source, cross-platform application development framework. It was developed by Google in 2017. It is used to build applications for Android, iOS, Linux, Mac, Windows, and the web. Flutter uses the Dart programming language. It provides a simple, powerful, efficient, and easy-to-und
15+ min read