Skip to content
Related Articles

Related Articles

Improve Article

How to Create a Basic Color Picker Tool in Android?

  • Last Updated : 22 Oct, 2020

There are many open-source color picker tools for android applications to choose from. In this discussion, At the end of this article, one will be able to implement the color picker tool in the android application, have a look at the following image to get an overview of the discussion. In this article, it’s been discussed to implement the very basic color picker tool.


A sample GIF 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 Java language.

Sample GIF

Steps to Implement a Color Picker Tool

Step 1: Create a New Project

Step 2: Adding the AmbilWarna color picker library dependency

  • AmbilWarna is an open-source color picker library which can be found here. Which has only one release and this is one of the final releases.
  • Now adding its dependency to the app-level gradle file.

implementation ‘com.github.yukuku:ambilwarna:2.0.1’

  • Make sure the system should be connected to the network (so that it downloads the required files) and after invoking the dependency click on the “Sync Now” button.
  • Refer to the following image to locate the app-level gradle file and invoke the dependency.

Gradle file

Step 3: Working with the actvity_main.xml file

  • Next, go to the activity_main.xml file, which represents the UI of the project.
  • Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.


<?xml version="1.0" encoding="utf-8"?>
    <!--Give all widgets, the proper id to handle
         them in>
    <!--GeeksforGeeks Text-->
        android:textStyle="bold" />
    <!--Pick color Button-->
        android:text="Pick Color" />
        android:text="Your picked color is:" />
    <!--sample view to preview selected color by user-->
    <!--by default this has been set to darker gery-->
    <!--this can be overridden after user chose the
         color from color picker-->
    <!--which has been handled in the>
        android:layout_marginTop="8dp" />
    <!--set color button to overwrite the 
        color for GeeksforGeeks text-->
        android:text="Set Color" />

Output UI:

Output UI

Before going to handle the color picker tool dialog functionality, understanding the parts of the dialog box is necessary so that it can become easier while dealing with parts of the dialog box in java code.

Parts of the dialogbox

Step 4: Working with the file

  • Finally, go to the file, and refer to the following code.
  • Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import yuku.ambilwarna.AmbilWarnaDialog;
public class MainActivity extends AppCompatActivity {
    // text view variable to set the color for GFG text
    private TextView gfgTextView;
    // two buttons to open color picker dialog and one to
    // set the color for GFG text
    private Button mSetColorButton, mPickColorButton;
    // view box to preview the selected color
    private View mColorPreview;
    // this is the default color of the preview box
    private int mDefaultColor;
    protected void onCreate(Bundle savedInstanceState) {
        // register the GFG text with appropriate ID
        gfgTextView = findViewById(;
        // register two of the buttons with their
        // appropriate IDs
        mPickColorButton = findViewById(;
        mSetColorButton = findViewById(;
        // and also register the view which shows the
        // preview of the color chosen by the user
        mColorPreview = findViewById(;
        // set the default color to 0 as it is black
        mDefaultColor = 0;
        // button open the AmbilWanra color picker dialog.
                new View.OnClickListener() {
                    public void onClick(View v) {
                        // to make code look cleaner the color
                        // picker dialog functionality are
                        // handled in openColorPickerDialogue()
                        // function
        // button to set the color GFG text
                new View.OnClickListener() {
                    public void onClick(View v) {
                        // as the mDefaultColor is the global
                        // variable its value will be changed as
                        // soon as ok button is clicked from the
                        // color picker dialog.
    // the dialog functionality is handled separately
    // using openColorPickerDialog this is triggered as
    // soon as the user clicks on the Pick Color button And
    // the AmbilWarnaDialog has 2 methods to be overridden
    // those are onCancel and onOk which handle the "Cancel"
    // and "OK" button of color picker dialog
    public void openColorPickerDialogue() {
        // the AmbilWarnaDialog callback needs 3 parameters
        // one is the context, second is default color,
        final AmbilWarnaDialog colorPickerDialogue = new AmbilWarnaDialog(this, mDefaultColor,
                new AmbilWarnaDialog.OnAmbilWarnaListener() {
                    public void onCancel(AmbilWarnaDialog dialog) {
                        // leave this function body as
                        // blank, as the dialog
                        // automatically closes when
                        // clicked on cancel button
                    public void onOk(AmbilWarnaDialog dialog, int color) {
                        // change the mDefaultColor to
                        // change the GFG text color as
                        // it is returned when the OK
                        // button is clicked from the
                        // color picker dialog
                        mDefaultColor = color;
                        // now change the picked color
                        // preview box to mDefaultColor

Output: Run on Emulator

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!

My Personal Notes arrow_drop_up
Recommended Articles
Page :