Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Pick Color From Image with TouchEvents in Android

  • Last Updated : 25 Aug, 2021

In this article, we are going to learn how to get touched pixel color on an image. A Screenshot is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

Step by Step Implementation

Step 1:  To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language. 

Step 2:  Set any image in the layout

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--Image form which we will pick color-->
        android:contentDescription="Image to pick color"
        app:layout_constraintTop_toTopOf="parent" />
            android:text="Picked Color: "
            android:textSize="20sp" />
        <!-- color name in Hexadecimal -->
            android:textSize="20sp" />
        <!-- show the picked color -->
            app:layout_constraintTop_toBottomOf="@id/pickColorImage" />

Step 3: 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.


import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    lateinit var image: ImageView
    lateinit var bitmap: Bitmap
    lateinit var colorView: View
    lateinit var colorString: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        image = findViewById(
        colorView = findViewById(
        colorString = findViewById(
        image.isDrawingCacheEnabled = true
        // on touch listener on image view
        image.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_DOWN || event.action == MotionEvent.ACTION_MOVE) {
                bitmap = image.drawingCache
                // get touched pixel
                val pixel = bitmap.getPixel(event.x.toInt(), event.y.toInt())
                // get RGB values from the touched pixel
                val r =
                val g =
                val b =
                // color name in Hexadecimal(#RRGGBB)
                colorString.text = "#${Integer.toHexString(pixel)}"
                // fill the color in the view
                colorView.setBackgroundColor(Color.rgb(r, g, b))

Now, run the app


Source Code: Click Here

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!