Open In App

How to Create a Basic Color Picker Tool in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Ambilwarna

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




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
 
    <!--Give all widgets, the proper id to handle
         them in MainActivity.java-->
 
    <!--GeeksforGeeks Text-->
    <TextView
        android:id="@+id/gfg_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:text="GeeksforGeeks"
        android:textSize="42sp"
        android:textStyle="bold" />
 
    <!--Pick color Button-->
    <Button
        android:id="@+id/pick_color_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:text="Pick Color" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:textSize="18sp"
        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 MainActivity.java-->
    <View
        android:id="@+id/preview_selected_color"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="center"
        android:background="@android:color/darker_gray"
        android:layout_marginTop="8dp" />
 
    <!--set color button to overwrite the
        color for GeeksforGeeks text-->
    <Button
        android:id="@+id/set_color_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:text="Set Color" />
 
</LinearLayout>


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 MainActivity.java file

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

Java




import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
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;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // register the GFG text with appropriate ID
        gfgTextView = findViewById(R.id.gfg_heading);
 
        // register two of the buttons with their
        // appropriate IDs
        mPickColorButton = findViewById(R.id.pick_color_button);
        mSetColorButton = findViewById(R.id.set_color_button);
 
        // and also register the view which shows the
        // preview of the color chosen by the user
        mColorPreview = findViewById(R.id.preview_selected_color);
 
        // set the default color to 0 as it is black
        mDefaultColor = 0;
 
        // button open the AmbilWanra color picker dialog.
        mPickColorButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // to make code look cleaner the color
                        // picker dialog functionality are
                        // handled in openColorPickerDialogue()
                        // function
                        openColorPickerDialogue();
                    }
                });
 
        // button to set the color GFG text
        mSetColorButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    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.
                        gfgTextView.setTextColor(mDefaultColor);
                    }
                });
    }
 
    // 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() {
                    @Override
                    public void onCancel(AmbilWarnaDialog dialog) {
                        // leave this function body as
                        // blank, as the dialog
                        // automatically closes when
                        // clicked on cancel button
                    }
 
                    @Override
                    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
                        mColorPreview.setBackgroundColor(mDefaultColor);
                    }
                });
        colorPickerDialogue.show();
    }
}


Kotlin




import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import yuku.ambilwarna.AmbilWarnaDialog;
 
class MainActivity : AppCompatActivity() {
    // text view variable to set the color for GFG text
    private var gfgTextView: TextView? = null
 
    // two buttons to open color picker dialog and one to
    // set the color for GFG text
    private var mSetColorButton: Button? = null
    private var mPickColorButton: Button? = null
 
    // view box to preview the selected color
    private var mColorPreview: View? = null
 
    // this is the default color of the preview box
    private var mDefaultColor = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // register the GFG text with appropriate ID
        gfgTextView = findViewById(R.id.gfg_heading)
 
        // register two of the buttons with their
        // appropriate IDs
        mPickColorButton = findViewById(R.id.pick_color_button)
        mSetColorButton = findViewById(R.id.set_color_button)
 
        // and also register the view which shows the
        // preview of the color chosen by the user
        mColorPreview = findViewById(R.id.preview_selected_color)
 
        // set the default color to 0 as it is black
        mDefaultColor = 0
 
        // button open the AmbilWanra color picker dialog.
        mPickColorButton.setOnClickListener(
            object : OnClickListener() {
                fun onClick(v: View?) {
                    // to make code look cleaner the color
                    // picker dialog functionality are
                    // handled in openColorPickerDialogue()
                    // function
                    openColorPickerDialogue()
                }
            })
 
        // button to set the color GFG text
        mSetColorButton.setOnClickListener(
            object : OnClickListener() {
                fun onClick(v: View?) {
                    // as the mDefaultColor is the global
                    // variable its value will be changed as
                    // soon as ok button is clicked from the
                    // color picker dialog.
                    gfgTextView.setTextColor(mDefaultColor)
                }
            })
    }
 
    // 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
    fun openColorPickerDialogue() {
 
        // the AmbilWarnaDialog callback needs 3 parameters
        // one is the context, second is default color,
        val colorPickerDialogue = AmbilWarnaDialog(this, mDefaultColor,
            object : OnAmbilWarnaListener() {
                fun onCancel(dialog: AmbilWarnaDialog?) {
                    // leave this function body as
                    // blank, as the dialog
                    // automatically closes when
                    // clicked on cancel button
                }
 
                fun onOk(dialog: AmbilWarnaDialog?, color: Int) {
                    // 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
                    mColorPreview.setBackgroundColor(mDefaultColor)
                }
            })
        colorPickerDialogue.show()
    }
}
//This code is writeen by Ujjwal Kumar Bhardwaj


Output: Run on Emulator



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